通过实现一个“用户基本资料修改”的功能表来练习使用基本的布局管理,QHBoxLayout, QVBoxLayout, QGridLayout。
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QGridLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private:
/**左侧***/
QLabel *UserNameLabel; QLineEdit *UserNameLineEdit;
QLabel *NameLabel; QLineEdit *NameLineEdit;
QLabel *SexLabel; QComboBox *SexComboBox;
QLabel *DepartmentLabel; QTextEdit *DepartmentTextEdit;
QLabel *AgeLabel; QLineEdit *AgeLineEdit;
QLabel * OtherLabel; QGridLayout *LeftLayout;
/**右侧***/
QLabel *HeadLabel; QLabel *HeadIconLabel;
QPushButton *UpdateHeadBtn; QHBoxLayout *TopRightLayout;
QLabel *IntroductionLabel; QTextEdit *IntroductionTextEdit;
QVBoxLayout *RightLayout;
/**底部***/
QPushButton *OkBtn, *CancelBtn;
QHBoxLayout *ButtonLayout;
};
#endif // WIDGET_H
#include "widget.h"
#include <QPixmap>
#include <QFrame>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("UserInfo"));
setWindowIcon(QIcon("1.jpg"));//设置窗口光标
/**左侧***/
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);//其他//占用1行2列
LeftLayout->setColumnStretch(0, 1);//设置gridlayout每列的占位比例
LeftLayout->setColumnStretch(1, 3);
/**右侧***/
HeadLabel = new QLabel(tr("头像:"));//右上角部分
HeadIconLabel = new QLabel;
QPixmap icon("1.jpg");
HeadIconLabel->setPixmap(icon);//lable显示图像
HeadIconLabel->resize(icon.width(), icon.height());//设置label大小与图像相同
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;
RightLayout = new QVBoxLayout();
RightLayout->setMargin(10);//设置边距
RightLayout->addLayout(TopRightLayout);
RightLayout->addWidget(IntroductionLabel);
RightLayout->addWidget(IntroductionTextEdit);
/**底部***/
OkBtn = new QPushButton(tr("确定"));
CancelBtn = new QPushButton(tr("取消"));
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);//占用1行2列
mainLayout->setSizeConstraint(QLayout::SetFixedSize);//设置layout大小和控件尺寸一致,使窗口不能更改大小
}
Widget::~Widget()
{
}
界面效果图:
图片资源所存放的路径:
------------------------------------------------------------------------------------------------------------