文章目录

1、代码
QLabel* nameLabel = new QLabel("名字:(&N)");
QLabel* ageLabel = new QLabel("年龄:(&G)");
QLabel* emailLabel = new QLabel("邮箱:(&M)");
QLabel* menlLabel = new QLabel("门牌号:");
QLineEdit* nameLineEdit = new QLineEdit;
QLineEdit* ageLineEdit = new QLineEdit;
QLineEdit* emailLineEdit = new QLineEdit;
QLineEdit* menlLineEdit = new QLineEdit;
// 设置伙伴关系
// 按住Alt + N 快捷键可以聚焦
nameLabel->setBuddy(nameLineEdit);
ageLabel->setBuddy(ageLineEdit);
emailLabel->setBuddy(emailLineEdit);
// 常用表单布局
QFormLayout* qformLayout = new QFormLayout;
// 添加一行
qformLayout->addRow(nameLabel,nameLineEdit);
qformLayout->addRow(ageLabel,ageLineEdit);
qformLayout->addRow(emailLabel,emailLineEdit);
qformLayout->addRow(menlLabel,menlLineEdit);
QLabel* sexlLabel = new QLabel("性别:");
QRadioButton* menbtn = new QRadioButton;
QRadioButton* wmenbtn = new QRadioButton;
menbtn->setText("男");
wmenbtn->setText("女");
// 水平布局
QHBoxLayout* sexLayout = new QHBoxLayout;
sexLayout->addWidget(sexlLabel);
sexLayout->addWidget(menbtn);
sexLayout->addWidget(wmenbtn);
// 垂直布局
QVBoxLayout* mainLayout = new QVBoxLayout(&myg);
// 添加空间
QSpacerItem* spaceitem = new QSpacerItem(30,30);
QHBoxLayout* btnshui = new QHBoxLayout;
QSpacerItem* btnspace = new QSpacerItem(300,30);
btnshui->addItem(btnspace);
btnshui->addWidget(sumbtn);
// 添加布局
mainLayout->addLayout(qformLayout);
mainLayout->addLayout(sexLayout);
mainLayout->addLayout(btnshui);
mainLayout->setMargin(10); // 与窗体之间间隙
mainLayout->setSpacing(20); // 空间间的间隙
myg.setLayout(mainLayout);