基本水平佈局

水平佈局在其中水平設定物件。

基本程式碼:

#include <QApplication>

#include <QMainWindow>
#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMainWindow window;
    QWidget *widget = new QWidget(&window);
    QHBoxLayout *layout = new QHBoxLayout(widget);

    window.setCentralWidget(widget);
    widget->setLayout(layout);

    
    
    layout->addWidget(new QPushButton("hello world", widget));
    layout->addWidget(new QPushButton("I would like to have a layout !", widget));
    layout->addWidget(new QPushButton("layouts are so great !", widget));

    window.show();

    return a.exec();
}

這將輸出:

StackOverflow 文件