[英]Adding Layout in QT on QMain WIndow using a code
我需要一些幫助我需要為我的應用程序設置布局,但我不知道如何在 QmainWindow 上設置布局..
這是我的 window.cpp 代碼的一部分:
window::window(QWidget *parent)
: QMainWindow(parent)
{
createFilesTable();
queryopen();
exitButton = createButton("E&xit",SLOT(programout()));
insertButton = createButton("&Add", SLOT(insert()));
editButton = createButton("&Edit", SLOT(edit()));
clearButton = createButton("&Clear", SLOT(clear()));
selectButton = createButton("&Select", SLOT(select()));
QHBoxLayout *buttonsLayout = new QHBoxLayout;
buttonsLayout->addStretch();
buttonsLayout->addWidget(selectButton);
buttonsLayout->addWidget(insertButton);
buttonsLayout->addWidget(editButton);
buttonsLayout->addWidget(clearButton);
buttonsLayout->addWidget(exitButton);
txtid = new QLineEdit;
txtname = new QLineEdit;
txtdesc = new QLineEdit;
label1 = new QLabel("ID:");
label2 = new QLabel("Name:");
label3 = new QLabel("Description:");
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(label1,1,0,1,1);
mainLayout->addWidget(txtid,1,1,1,2);
mainLayout->addWidget(label2,2,0,1,1);
mainLayout->addWidget(txtname,2,1,1,3);
mainLayout->addWidget(label3,3,0,1,1);
mainLayout->addWidget(txtdesc,3,1,1,3);
mainLayout->addLayout(buttonsLayout,4,1,1,3);
mainLayout->addWidget(filesTable,6,0,6,5);
setLayout(mainLayout);
setWindowTitle("Database Connection");
resize(450,300);
}
對於QMainWindow
,您使用setCentralWidget(QWidget*) ,這與所有其他QWidget
子類不同。
原因是QMainWindow
已經有了自己的布局,其中包括菜單欄、狀態欄、停靠區 windows 等位置。所以您創建另一個QWidget
並將其布局設置為您想要的布局,然后將該QWidget
設為中心小部件QMainWindow
。
(這種不一致已經讓我好幾次了......但一旦你了解發生了什么,它就有意義了。 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.