最近,一次編程中,我發現在mainwindow中佈局是不能正常顯示的,這讓我傷透了腦筋,爲什麼不可以呢?又該怎麼在mainwindow中對控件進行佈局呢?
百度是最好的 老師。我在百度上找到了最後的解決方法,就是在mainwindow中定義一個widget 之後,除了菜單欄,狀態欄,工具欄之外,所有的控件都放在widget中。
下面見代碼:
QWidget *centerWindow = new QWidget;
this->setCentralWidget(centerWindow);
button = new QPushButton("One");
button2 = new QPushButton("Two");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button);
layout->addWidget(button2);
centerWindow->setLayout(layout);
調試成功~~~
over~~~