佈局嵌套,就像他們會在HTML中做的那樣。有四個垂直佈局(或三個,如果第一行只有進度條)其中有水平佈局
但是使用網格作爲頂部佈局將允許在邊上添加邊距。 當然,你可以通過編程來完成。我已經展示了Designer如何看起來如此,但設計師實際上做的是生成簡單的線性代碼。相反,你可以這樣做。
生成的代碼:
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(504, 377);
gridLayoutWidget = new QWidget(Dialog);
gridLayoutWidget->setObjectName(QString::fromUtf8("gridLayoutWidget"));
gridLayoutWidget->setGeometry(QRect(9, 9, 481, 351));
gridLayout = new QGridLayout(gridLayoutWidget);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setContentsMargins(0, 0, 0, 0);
horizontalLayout_3 = new QHBoxLayout();
horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3"));
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_3->addItem(horizontalSpacer);
progressBar_2 = new QProgressBar(gridLayoutWidget);
progressBar_2->setObjectName(QString::fromUtf8("progressBar_2"));
progressBar_2->setValue(24);
horizontalLayout_3->addWidget(progressBar_2);
horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_3->addItem(horizontalSpacer_2);
gridLayout->addLayout(horizontalLayout_3, 1, 0, 1, 1);
horizontalLayout_2 = new QHBoxLayout();
horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
horizontalSpacer_3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_2->addItem(horizontalSpacer_3);
progressBar_3 = new QProgressBar(gridLayoutWidget);
progressBar_3->setObjectName(QString::fromUtf8("progressBar_3"));
progressBar_3->setValue(24);
horizontalLayout_2->addWidget(progressBar_3);
gridLayout->addLayout(horizontalLayout_2, 2, 0, 1, 1);
horizontalLayout = new QHBoxLayout();
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
progressBar_4 = new QProgressBar(gridLayoutWidget);
progressBar_4->setObjectName(QString::fromUtf8("progressBar_4"));
progressBar_4->setValue(24);
horizontalLayout->addWidget(progressBar_4);
horizontalSpacer_4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer_4);
gridLayout->addLayout(horizontalLayout, 3, 0, 1, 1);
horizontalLayout_4 = new QHBoxLayout();
horizontalLayout_4->setObjectName(QString::fromUtf8("horizontalLayout_4"));
progressBar = new QProgressBar(gridLayoutWidget);
progressBar->setObjectName(QString::fromUtf8("progressBar"));
progressBar->setValue(24);
horizontalLayout_4->addWidget(progressBar);
gridLayout->addLayout(horizontalLayout_4, 0, 0, 1, 1);
這也會起作用,儘管它增加了對小部件的附加限制。這可能是一件好事或壞事,取決於形式構建。帶有跨部件的網格佈局對於將面板放置到佈局中非常有用,該佈局將包含其他小部件 – Swift