Qt中使用setLayout()函数时报错------Attempting to set QLayout "" on MainWindow "MainWindow",which already has

  在Qt的主窗口(MainWindow)中使用setLayout()函数时,运行时即会报如题所示的错误,这是什么原因呢,我使用“F1”瞅了瞅QMainWindow,发现了如下信息:

A main window provides a framework for building an application's user interface. Qt has QMainWindow and its related classes for main window management. QMainWindow has its own layout to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar. The layout has a center area that can be occupied by any kind of widget. You can see an image of the layout below.

       Qt中使用setLayout()函数时报错------Attempting to set QLayout "" on MainWindow "MainWindow",which already has_第1张图片

大概意思就是说mainwindow的主框架里已经有了一个Layout了,这个Layout包括了菜单栏、工具栏、停驻窗口区、中心窗口区、底部状态栏五部分,对于dev们应该够用了,但是如果实在是要再使用Layout来布局,可以先新建一个窗口,在新建的窗口中使用Layout,然后再使用setCentralWidget()将新建的窗口设成主窗口的Central Widget,示例代码如下:

Qwidget* myWidget = new QWidget();

...    // 对窗口做相应的操作,例如新建自己的Layout取名叫myLayout

myWidget->setLayout(myLayout);

setCentralWidget(myWidget);

你可能感兴趣的:(qt)