开发者

Adding Layout in QT on QMain WIndow using a code

I need some help I need to set a layout for my application but i don't know how to set layout on QmainWindow..

here is a part of my code for the 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->开发者_StackOverflow中文版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);

}


For a QMainWindow, you use setCentralWidget(QWidget*), unlike all other QWidget subclasses.

The reason is that QMainWindow already has its own layout, which includes places for menu bar, status bar, dock windows, etc. So you create another QWidget and set its layout to the layout you want, then make that QWidget the central widget of the QMainWindow.

(This inconsistency has gotten me a few times ... but it makes sense once you understand what is going on.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜