效果图展示:
代码实现:
userinfo.h
//左侧
QLabel * UserNameLabel;
QLabel * NameLabel;
QLabel * SexLabel;
QLabel * DepartmentLabel;
QLabel * AgeLabel;
QLabel * OtherLabel;
QLineEdit * UserNameLineEdit;
QLineEdit * NameLineEdit;
QComboBox * SexComboBox;
QTextEdit * DepartmentTextEdit;
QLineEdit * AgeLineEdit;
QGridLayout * leftLayout;
//右侧
QLabel *HeadLabel;
QLabel * HeadIconLabel;
QPushButton *UpdateHeadBtn;
QHBoxLayout * TopRightLayout;
QLabel * IntroductionLabel;
QTextEdit * IntroductionTextEdit;
QVBoxLayout * RightLayout;
//底部
QPushButton * OkBtn;
QPushButton * CancelBtn;
QHBoxLayout * ButtonLayout;
userinfo.cpp
/*****************左侧*****************************/
UserNameLabel = new QLabel(tr("用户名"));
UserNameLineEdit = new QLineEdit;
NameLabel = new QLabel(tr("姓名"));
NameLineEdit = new QLineEdit;
SexLabel = new QLabel(tr("性别"));
SexComboBox = new QComboBox;//下拉框
SexComboBox->addItem(tr("男"));
SexComboBox->addItem(tr("女"));
DepartmentLabel = new QLabel(tr("部门"));
DepartmentTextEdit = new QTextEdit;
AgeLabel = new QLabel(tr("年龄"));
AgeLineEdit = new QLineEdit;
OtherLabel = new QLabel(tr("备注"));
OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
leftLayout = new QGridLayout();
leftLayout->addWidget(UserNameLabel,0,0);//用户名
leftLayout->addWidget(UserNameLineEdit,0,1);
//姓名
leftLayout->addWidget(NameLabel,1,0);
leftLayout->addWidget(NameLineEdit,1,1);
//性别
leftLayout->addWidget(SexLabel,2,0);
leftLayout->addWidget(SexComboBox,2,1);
//部门
leftLayout->addWidget(DepartmentLabel,3,0);
leftLayout->addWidget(DepartmentTextEdit,3,1);
//年龄
leftLayout->addWidget(AgeLabel,4,0);
leftLayout->addWidget(AgeLineEdit,4,1);
//其它
leftLayout->addWidget(OtherLabel,5,0,1,2);
leftLayout->setColumnStretch(0,1);
leftLayout->setColumnStretch(1,3);
/*******************右侧*********************/
HeadLabel = new QLabel(tr("头像: "));
HeadIconLabel = new QLabel;
QPixmap icon ("321.png");
HeadIconLabel->setPixmap(icon);
HeadIconLabel->resize(icon.width(),icon.height());
UpdateHeadBtn = new QPushButton(tr("更新"));
TopRightLayout = new QHBoxLayout();
TopRightLayout->setSpacing(20);
TopRightLayout->addWidget(HeadLabel);
TopRightLayout->addWidget(HeadIconLabel);
TopRightLayout->addWidget(UpdateHeadBtn);
IntroductionLabel = new QLabel(tr("个人说明"));
IntroductionTextEdit = new QTextEdit;
//QVBoxLayout:垂直布局
RightLayout = new QVBoxLayout();
RightLayout->setMargin(10);
RightLayout->addLayout(TopRightLayout);//???
RightLayout->addWidget(IntroductionLabel);
RightLayout->addWidget(IntroductionTextEdit);
/*******************底部***********************/
OkBtn =new QPushButton(tr("确定"));
CancelBtn = new QPushButton(tr("取消"));
//QHBoxLayout:水平布局
ButtonLayout = new QHBoxLayout();
ButtonLayout ->addStretch();
ButtonLayout->addWidget(OkBtn);
ButtonLayout->addWidget(CancelBtn);
/************************************************/
QGridLayout * mainLayout = new QGridLayout(this);
mainLayout->setMargin(15);
mainLayout->setSpacing(10);
mainLayout->addLayout(leftLayout,0,0);
mainLayout->addLayout(RightLayout,0,1);
mainLayout->addLayout(ButtonLayout,1,0,1,2);
//SizeConstraint:保留布局的调整大小模式
// FixedSize():固定大小
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
布局练习示例