2011-05-05 30 views
2

我需要一些幫助,我需要爲我的應用程序設置佈局,但我不知道如何設置佈局上的QMainWindow上QMAIN窗口QT添加布局..使用代碼

這裏是一個部分我對於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); 

} 

回答

4

對於QMainWindow,您使用setCentralWidget(QWidget*),與所有其他QWidget子類。

的原因是QMainWindow已經擁有自己的佈局,其中包括菜單欄,狀態欄,停靠窗口等,所以你創建另一個QWidget並設置佈局,以你想要的佈局,然後作出這樣的地方QWidgetQMainWindow的中心部件。

這個矛盾已經得到了我幾次......但它是有道理的,一旦你瞭解正在發生的事情。