我有兩條新聞給你:壞和好。
壞消息:QMainWindow
使用QMainWindowLayout
這是Qt私有頭文件的一部分,因此您不能按原樣定製佈局「誠實」。
但你仍然可以做到這一點。有一個QMainWindow::setMenuWidget
方法可以幫助你。如果你的主窗口沒有菜單欄,那麼你可以簡單的寫
setMenuWidget(myCustomWidget);
和myCustomWidget
將被放置在窗口的頂部。
但是,如果你的應用程序中有菜單欄,這並不容易。但即使在這種情況下,您仍然可以在窗口的頂部放置一個由兩部分組成的小部件:上面的自定義小部件和下面的菜單欄。例如,對於QtDesigner生成的窗口,可以完成如下:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel *coolLabel = new QLabel("The coolest text in the world", this);
QWidget *menuWidget = new QWidget;
QVBoxLayout *vLayout = new QVBoxLayout;
menuWidget->setLayout(vLayout);
vLayout->addWidget(coolLabel);
vLayout->addWidget(ui->menuBar);
setMenuWidget(menuWidget);
}
哈哈,這是一個超級把戲!謝謝! – 2014-10-21 18:16:37