最近学习了Qt的布局控件,用到了QGridLayout、QHBoxLayout和QVBoxLayout类。使用布局控件可以帮我们自动排列各种控件,方便我们对控件的管理。下面是写出来的登记信息的界面(看上去很丑,但是主要是练习布局的使用哦,我使用的是Qt的4.7.1版本)
下面是源代码:
//infotable.h
#ifndef INFOTABLE_H
#define INFOTABLE_H
#include <QtGui/QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QFrame>
#include <QComboBox>
#include <QTextCodec>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPixmap>
#include <QFont>
class InfoTable : public QWidget
{
Q_OBJECT
public:
InfoTable(QWidget *parent = 0);
~InfoTable();
private:
QLabel *titleLab;//标题
QLabel *nameLab;//姓名
QLineEdit *nameEdit;
QLabel *photoLab;//头像
QLabel *sexLab;//性别
QLabel *ageLab;//年龄
QLabel *eduLab;//学历
QLineEdit *eduEdit;
QLabel *marryLab;//婚否
QLabel *phoneLab;//手机
QLineEdit *phoneEdit;
QLabel *mailLab;//E-mail
QLineEdit *mailEdit;
QLabel *jobLab;//从事行业
QLineEdit *jobEdit;
QLabel *companyLab;//单位
QLineEdit *companyEdit;
QLabel *addrLab;//居住地
QLineEdit *addrEdit;
QLabel *evaluateLab;//自我评价
QTextEdit *evaluateEdit;
QLabel *remarkLab;//备注
QLineEdit *remarkEdit;
QPushButton *upPhotoBtn;//上传头像
QPushButton *saveBtn;//保存
QPushButton *exitBtn;//退出
QRadioButton *sexChoose1, *sexChoose2;//性别单选框
QRadioButton *marryChoose1, *marryChoose2;//婚否单选框
QComboBox *ageChoose;//年龄选择列表
QGridLayout *mainLayout;//主布局
};
#endif // INFOTABLE_H
//infotable.cpp
#include "infotable.h"
InfoTable::InfoTable(QWidget *parent)
: QWidget(parent)
{
//先定义一系列的控件
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
setWindowTitle(tr("个人档案登记"));
titleLab = new QLabel(tr("个人档案"));
nameLab = new QLabel(tr("姓名:"));
nameEdit = new QLineEdit;
photoLab = new QLabel;
sexLab = new QLabel(tr("性别:"));
ageLab = new QLabel(tr("年龄:"));
eduLab = new QLabel(tr("学历:"));
eduEdit = new QLineEdit;
marryLab = new QLabel(tr("婚否:"));
phoneLab = new QLabel(tr("手机:"));
phoneEdit = new QLineEdit;
mailLab = new QLabel(tr("E-mail:"));
mailEdit = new QLineEdit;
jobLab = new QLabel(tr("从事行业:"));
jobEdit = new QLineEdit;
companyLab = new QLabel(tr("单位:"));
companyEdit = new QLineEdit;
addrLab = new QLabel(tr("居住地:"));
addrEdit = new QLineEdit;
evaluateLab = new QLabel(tr("自我评价:"));
evaluateEdit = new QTextEdit;
nameEdit = new QLineEdit;
remarkLab = new QLabel(tr("备注:"));
upPhotoBtn = new QPushButton(tr("上传图像"));
saveBtn = new QPushButton(tr("保存"));
exitBtn = new QPushButton(tr("退出"));
sexChoose1 = new QRadioButton(tr("男"));
sexChoose2 = new QRadioButton(tr("女"));
marryChoose1 = new QRadioButton(tr("已婚"));
marryChoose2 = new QRadioButton(tr("未婚"));
ageChoose = new QComboBox;
//设置标题格式
QFont font;
font.setBold(true);//字体加粗
font.setPointSize(30);//设置字号
titleLab->setFont(font);
//设置性别单选框
QHBoxLayout *sexlayout = new QHBoxLayout;
sexlayout->addWidget(sexChoose1);
sexlayout->addWidget(sexChoose2);
//设置婚否单选框
QHBoxLayout *marrylayout = new QHBoxLayout;
marrylayout->addWidget(marryChoose1);
marrylayout->addWidget(marryChoose2);
//设置年龄下拉列表
ageChoose->addItem("20");
ageChoose->addItem("21");
ageChoose->addItem("22");
//设置头像
QVBoxLayout *photoLayout = new QVBoxLayout;
QPixmap photo("head.png");
photoLab->resize(photo.width(), photo.height());
photoLab->setPixmap(photo);
photoLayout->addWidget(photoLab);
photoLayout->addWidget(upPhotoBtn);
//设置备注标签的形状
remarkLab->setFrameStyle(QFrame::Panel | QFrame::Sunken);
//设置底部按钮
QHBoxLayout *bottomLayout = new QHBoxLayout;
bottomLayout->addStretch();
bottomLayout->addWidget(saveBtn);
bottomLayout->addStretch();
bottomLayout->addWidget(exitBtn);
bottomLayout->addStretch();
//设置主布局mainLayout,加入各部分的子控件
mainLayout = new QGridLayout(this);
mainLayout->addWidget(titleLab, 0, 0, 1, 5, Qt::AlignHCenter);
mainLayout->addLayout(photoLayout, 0, 4, 8, 1);
mainLayout->addWidget(nameLab, 3, 0);
mainLayout->addWidget(nameEdit, 3, 1);
mainLayout->addWidget(phoneLab, 3, 2);
mainLayout->addWidget(phoneEdit, 3, 3);
mainLayout->addWidget(sexLab, 4, 0);
mainLayout->addLayout(sexlayout, 4, 1);
mainLayout->addWidget(mailLab, 4, 2);
mainLayout->addWidget(mailEdit, 4, 3);
mainLayout->addWidget(ageLab, 5, 0);
mainLayout->addWidget(ageChoose, 5, 1);
mainLayout->addWidget(jobLab, 5, 2);
mainLayout->addWidget(jobEdit, 5, 3);
mainLayout->addWidget(eduLab, 6, 0);
mainLayout->addWidget(eduEdit, 6, 1);
mainLayout->addWidget(companyLab, 6, 2);
mainLayout->addWidget(companyEdit, 6, 3);
mainLayout->addWidget(marryLab, 7, 0);
mainLayout->addLayout(marrylayout, 7, 1);
mainLayout->addWidget(addrLab, 7, 2);
mainLayout->addWidget(addrEdit, 7, 3);
mainLayout->addWidget(evaluateLab, 8, 0);
mainLayout->addWidget(evaluateEdit, 8, 1, 1, 4);
mainLayout->addWidget(remarkLab, 9, 0, 1, 5);
mainLayout->addLayout(bottomLayout, 10, 0, 1, 5);
//设置各行的比例
mainLayout->setColumnStretch(0, 1);
mainLayout->setColumnStretch(1, 3);
mainLayout->setColumnStretch(2, 1);
mainLayout->setColumnStretch(3, 3);
//调整整体的布局
mainLayout->setSpacing(10);//设置各控件的间隔
mainLayout->setMargin(20);//设置边距
mainLayout->setSizeConstraint(QLayout::SetFixedSize);//固定窗口大小
}
InfoTable::~InfoTable()
{
}
//main.cpp
#include <QtGui/QApplication>
#include "infotable.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
InfoTable w;
w.show();
return a.exec();
}
总结:
使用布局管理可以很方便地设置各控件的位置,可以自动调整布局,在设计界面的过程中可以节省一定的时间。