■ QGridLayout
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
this->setGeometry(0, 0, 800, 480);
gWidget = new QWidget(this);
this->setCentralWidget(gWidget);
gridLayout = new QGridLayout();
QList <QString> list;
list<<"按钮1"<<"按钮2"<<"按钮3"<<"按钮4";
for (int i = 0; i < 4; i++){
pushButton[i] = new QPushButton();
pushButton[i]->setText(list[i]);
pushButton[i]->setMinimumSize(100, 30);
pushButton[i]->setSizePolicy(
QSizePolicy::Expanding,
QSizePolicy::Expanding
);
switch (i) {
case 0:
gridLayout->addWidget(pushButton[i], 0, 0);
break;
case 1:
gridLayout->addWidget(pushButton[i], 0, 1);
break;
case 2:
gridLayout->addWidget(pushButton[i], 1, 0);
break;
case 3:
gridLayout->addWidget(pushButton[i], 1, 1);
break;
default:
break;
}
}
gridLayout->setRowStretch(0, 2);
gridLayout->setRowStretch(1, 3);
gridLayout->setColumnStretch(0, 1);
gridLayout->setColumnStretch(1, 3);
gWidget->setLayout(gridLayout);
}
■ QFormLayout
this->setGeometry(0, 0, 800, 480);
fWidget = new QWidget(this);
fWidget->setGeometry(250, 100, 300, 200);
userLineEdit = new QLineEdit();
passwordLineEdit = new QLineEdit();
formLayout = new QFormLayout();
formLayout->addRow("用户名: ", userLineEdit);
formLayout->addRow("密码 :", passwordLineEdit);
formLayout->setSpacing(10);
formLayout->setMargin(20);
fWidget->setLayout(formLayout);
■
■