Qt: Can't set layout in QMainWindow

前端 未结 1 1984
抹茶落季
抹茶落季 2021-02-05 12:10

I am trying to set my layout (using setLayout()) in my mainwindow. It does not show anything on launch:

class MainWindow : public QMainWindow
{
             


        
相关标签:
1条回答
  • 2021-02-05 12:34

    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.

    0 讨论(0)
提交回复
热议问题