Qt: Can't set layout in QMainWindow

c++, qt

Solution

You need to change the last two lines of code to be the following:

QWidget *widget = new QWidget();
widget->setLayout(VBoxLayout);
setCentralWidget(widget);
//VBoxLayout->addWidget(new QLayout);
//setLayout(VBoxLayout);

The `QMainWindow` is a special case. You set the contents of this widget by putting the layout in a new `QWidget` and then setting that as the central widget. See this answer also.

Problem

I am trying to set my layout (using `setLayout()`) in my mainwindow. It does not show anything on launch: ``` class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0) { QVBoxLayout *vBoxLayout = new QVBoxLayout; { QPushButton *pushButton = new QPushButton(tr("A button")); vBoxLayout->addWidget(pushButton); } setLayout(vBoxLayout); } }; ```

Original source