c++ - 在 QMainWindow 中显示 QGridLayout

标签 c++ object user-interface layout qt4

我试图从 QMainWindow 中设置 QGridLayout。据我所知,这段代码看起来有效,但它不起作用。这能做到吗?

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);
}

如果我运行它,我所看到的只是三个按钮在彼此之上......

最佳答案

需要使用Central Widget,因为QMainWindow是整个窗口(包含状态栏、菜单栏等):

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

关于c++ - 在 QMainWindow 中显示 QGridLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23883571/

相关文章:

c++ - 我的 C++ 程序中的一些代码使程序崩溃。我正在实现 BFS 算法

C#继承隐藏继承成员

ios - 尝试设置 UIButton 的约束时出错

ios - 关于在 iPhone 上创建动态 X-Y 图表的建议

java - Swing:滚动到 JScrollPane 的底部,以当前视口(viewport)位置为条件

c++ - 如何将指针保存到重新定义的运算符?

c++ - 如何处理 Win32 多行编辑控件中的 Enter 键?

java - MICO Corba 的直接替代品?

angular - 将静态对象转换为 Observable 和 forkjoin

javascript - 如何连接对象的原型(prototype)链和表示它们的 HTML?