widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QTextEdit>
#include <QComboBox>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFrame>
#include <QPixmap>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private:
//左侧
QLabel *usernamelabel,*namelabel,*sexlabel,*departmentlabel,*agelabel,*otherlabel;
QLineEdit *usernamelinedit,*namelinedit;
QComboBox *sexcombobox;
QTextEdit *departmenttextedit;
QLineEdit *agelineedit;
QGridLayout *leftlayout;
QHBoxLayout *lefthlayout;
//右侧
QLabel * headlabel,*updatelabel,*introductionlabel;
QLabel *headiconlabel;
QTextEdit *introductiontextedit;
QHBoxLayout *toprightlayout;
QVBoxLayout *rightlayout;
//底部
QPushButton *okbtn,*cancelbtn;
QHBoxLayout *buttomlayout;
};
#endif // WIDGET_H
main.cpp:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp:
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("用户信息");
//左侧
usernamelabel = new QLabel(tr("用户名:"));
namelabel = new QLabel(tr("姓名:"));
sexlabel = new QLabel(tr("性别:"));
departmentlabel = new QLabel(tr("部门:"));
agelabel = new QLabel(tr("年龄:"));
otherlabel = new QLabel(tr("备注:"));
usernamelinedit = new QLineEdit;
namelinedit = new QLineEdit;
sexcombobox = new QComboBox;
sexcombobox->addItem(tr("男"));
sexcombobox->addItem(tr("女"));
departmenttextedit = new QTextEdit;
agelineedit = new QLineEdit;
//QFrame类是有框架的窗口部件的基类
otherlabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);//在面板中凹陷显示Sunken(凹陷),Raised(上调)
leftlayout = new QGridLayout();
leftlayout->addWidget(usernamelabel,0,0); //用户名
leftlayout->addWidget(namelabel,1,0); //姓名
leftlayout->addWidget(sexlabel,2,0); //性别
leftlayout->addWidget(departmentlabel,3,0); //部门
leftlayout->addWidget(agelabel,4,0); //年龄
leftlayout->addWidget(otherlabel,5,0,1,2); //备注
leftlayout->addWidget(usernamelinedit,0,1);
leftlayout->addWidget(namelinedit,1,1);
leftlayout->addWidget(sexcombobox,2,1);
leftlayout->addWidget(departmenttextedit,3,1);
leftlayout->addWidget(agelineedit,4,1);
leftlayout->setColumnStretch(0,1); //设置gridlayout每列的占位比,即第一列所占比为1/4
leftlayout->setColumnStretch(1,3);//第二列所占比为3/4
//右侧
headlabel = new QLabel(tr("头像:"));
headiconlabel = new QLabel;
QPixmap icon("1.png");
headiconlabel->setPixmap(icon); //label显示图像
headiconlabel->resize(icon.width(),icon.height()); //设置label大小与图像大小一致
updatelabel = new QLabel(tr("更新"));
introductionlabel = new QLabel(tr("个人说明:"));
introductiontextedit = new QTextEdit();
toprightlayout = new QHBoxLayout();
toprightlayout->setMargin(20); //设置边框
toprightlayout->addWidget(headlabel);
toprightlayout->addWidget(headiconlabel);
toprightlayout->addWidget(updatelabel);
rightlayout = new QVBoxLayout();
rightlayout->setMargin(10); //设置边距
rightlayout->addLayout(toprightlayout);
rightlayout->addWidget(introductionlabel);
rightlayout->addWidget(introductiontextedit);
//底部
okbtn = new QPushButton(tr("确定"));
cancelbtn = new QPushButton(tr("取消"));
buttomlayout = new QHBoxLayout();
buttomlayout->addStretch(); //加入一个占位符
buttomlayout->addWidget(okbtn);
buttomlayout->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(buttomlayout,1,0,1,2);
mainlayout->setSizeConstraint(QLayout::SetFixedSize); //设置layout大小与控件尺寸一致
}
Widget::~Widget()
{
}
设计界面如下: