简体   繁体   English

从QMainWindow内部显示QGridLayout

[英]Displaying a QGridLayout from within a QMainWindow

I tried to set a QGridLayout from within a QMainWindow. 我试图从QMainWindow内设置QGridLayout。 As far as I can tell this code looks valid, but its not working. 据我所知,此代码看起来有效,但无法正常工作。 Can this be done? 能做到吗?

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) 
{
   QGridLayout *layout = new QGridLayout;
   this->setLayout(layout);

   QPushButton *box1 = new QPushButton(this);
   QPushButton *box2 = new QPushButton(this);
   QPushButton *box3 = new QPushButton(this);

   layout->addWidget(box1, 0, 0);
   layout->addWidget(box2, 1, 0);
   layout->addWidget(box3, 2, 0);
}

All I see if I run this is three buttons on top of each other... 我所看到的只是运行三个按钮而已...

You need to use the Central Widget because the QMainWindow is the whole window (containing status bar, menu bar, etc.): 您需要使用中央小部件,因为QMainWindow是整个窗口(包含状态栏,菜单栏等):

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    this->setCentralWidget (new QWidget (this)) ;
    this->centralWidget()->setLayout(new QGridLayout());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM