Thử đặt hộp màu hồng thành QWidget bằng QHBoxLayout (thay vì chỉ tạo bố cục). Lý do là QLayouts không cung cấp chức năng để thực hiện các kích thước cố định, nhưng QWidgets làm.
// first create the four widgets at the top left,
// and use QWidget::setFixedWidth() on each of them.
// then set up the top widget (composed of the four smaller widgets):
QWidget *topWidget = new QWidget;
QHBoxLayout *topWidgetLayout = new QHBoxLayout(topWidget);
topWidgetLayout->addWidget(widget1);
topWidgetLayout->addWidget(widget2);
topWidgetLayout->addWidget(widget3);
topWidgetLayout->addWidget(widget4);
topWidgetLayout->addStretch(1); // add the stretch
topWidget->setFixedHeight(50);
// now put the bottom (centered) widget into its own QHBoxLayout
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addStretch(1);
hLayout->addWidget(bottomWidget);
hLayout->addStretch(1);
bottomWidget->setFixedSize(QSize(50, 50));
// now use a QVBoxLayout to lay everything out
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(topWidget);
mainLayout->addStretch(1);
mainLayout->addLayout(hLayout);
mainLayout->addStretch(1);
Nếu bạn thực sự muốn có hai bố cục riêng biệt - một cho hộp màu hồng và một cho các hộp màu xanh - ý tưởng về cơ bản là giống nhau ngoại trừ bạn muốn làm cho hộp màu xanh vào QVBoxLayout riêng của mình, và sau đó sử dụng:
mainLayout->addWidget(topWidget);
mainLayout->addLayout(bottomLayout);