QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
如题,出现这个的原因是,如果你的窗口继承的是QMainwindow,需要设置
setCentralWidget()
具体代码如下
QWidget* widget = new QWidget(mainWindow); widget->setLayout(gridLayout); mainWindow->setCentralWidget(widget);
将初始化的Widget放置在MainWinodw中心即可