您可以通過編程創建小部件並通過佈局調整其位置。 例如,它可能看起來像這樣:
QVBoxLayout *topLayout = new QVBoxLayout();
for (int lineNumber = 0; lineNumber < 1024; ++lineNumber)
{
QWidget *oneLineWidget = new QWidget(this);
QHBoxLayout *oneLineWidgetLayout = new QHBoxLayout();
{ //added these brackets just for the ease of reading.
QLabel *labFirst = new QLabel(tr("first label"), oneLineWidget);
QLabel *labSecond = new QLabel(tr("second label"), oneLineWidget);
QPushButton *bFirst = new QPushButton(tr("first button"), oneLineWidget);
QPushButton *bSecond = new QPushButton(tr("second button"), oneLineWidget);
QRadioButton *rbFirst = new QRadioButton(tr("first radiobutton"), oneLineWidget);
QRadioButton *rbSecond = new QRadioButton(tr("second radiobutton"), oneLineWidget);
oneLineWidgetLayout->addWidget(labFirst);
oneLineWidgetLayout->addWidget(labSecond);
oneLineWidgetLayout->addWidget(bFirst);
oneLineWidgetLayout->addWidget(bSecond);
//lets put one radioButton under another.
QVBoxLayout *radioButtonsLayout = new QVBoxLayout();
{
radioButtonsLayout->addWidget(rbFirst);
radioButtonsLayout->addWidget(rbSecond);
}
//and now we can combine layouts.
oneLineWidgetLayout->addLayout(radioButtonsLayout);
}
oneLineWidget->setLayout(oneLineWidgetLayout);
topLayout->addWidget(oneLineWidget);
}
this->setLayout(topLayout);
有不同類型的佈局(QBoxLayout,QGridLayout,QFormLayout,等等),你可以玩的。你可以從QLayout documentation開始。有一個繼承它的類的列表。 我希望它會有所幫助! :) 祝你好運!
P.S.我把它寫在記事本中,並沒有在「現實生活中」進行測試。所以它可能包含一些錯誤。但至少我希望它能指引你朝着正確的方向前進。 – FreeNickname