Qt Layout on QMainWindow

后端 未结 4 2030

I designed a QMainWindow with QtCreator\'s designer. It consists of the default central widget (a QWidget) which contains a QVBo

4条回答
  •  执笔经年
    2020-12-14 00:25

    If you want to do it with code instead of using QtCreator, you could set the layout in a QWidget and then set the QWidget as the central widget of the main window like this:

    #include 
    #include 
    #include 
    #include "mainwindow.h"
    
    MainWindow::MainWindow() {  
    
            // Set layout
            QHBoxLayout *layout = new QHBoxLayout;
            layout->addWidget(myWidget1);
            layout->addWidget(myWidget2);
    
            // Set layout in QWidget
            QWidget *window = new QWidget();
            window->setLayout(layout);
    
            // Set QWidget as the central layout of the main window
            setCentralWidget(window);
    
    }
    

提交回复
热议问题