一 QLayout类(基本布局)
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QGridLayout>
#include <QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private:
// 1:左边 网格布局、表格布局
QGridLayout *lLayout;
QLabel *UserNumber;//姓名
QLineEdit *UserNumberLineEdit;
QLabel *UserName;
QLineEdit *UserNameLineEdit;
QLabel *UserSex;//性别
QComboBox *UserSexCombobox;
QLabel *UserDepart;//部门
QTextEdit *UserDepartTextEdit;
QLabel *UserAge;//年龄
QLineEdit *UserAgeLineEdit;
// 2:右边 水平布局
QHBoxLayout *toprightlayout;//顶部水平布局
QVBoxLayout *rightlayout;//垂直布局
QLabel *MyselfInfo;
QTextEdit *MyselfInfoTextEdit;
// 3:右边底部按钮
QPushButton *okbutton,*cancelbutton;
QHBoxLayout *buttomLayout;
};
#endif // DIALOG_H
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("员工信息");
// 左边控件
UserNumber=new QLabel("员工编号:");
UserNumberLineEdit=new QLineEdit;
UserName=new QLabel("员工姓名:");
UserNameLineEdit=new QLineEdit;
UserSex=new QLabel("员工性别:");
UserSexCombobox=new QComboBox;
UserSexCombobox->addItem("男");
UserSexCombobox->addItem("女");
UserDepart=new QLabel("所在部门:");
UserDepartTextEdit=new QTextEdit;
UserAge=new QLabel("员工年龄:");
UserAgeLineEdit=new QLineEdit;
// 网格布局
lLayout=new QGridLayout();
lLayout->addWidget(UserNumber,0,0); // 0行0列 员工编号
lLayout->addWidget(UserNumberLineEdit,0,1);// 0行0列
lLayout->addWidget(UserName,1,0);
lLayout->addWidget(UserNameLineEdit,1,1);
lLayout->addWidget(UserSex,2,0);
lLayout->addWidget(UserSexCombobox,2,1);
lLayout->addWidget(UserDepart,3,0);
lLayout->addWidget(UserDepartTextEdit,3,1);
lLayout->addWidget(UserAge,4,0);
lLayout->addWidget(UserAgeLineEdit,4,1);
lLayout->setColumnStretch(0,1);
lLayout->setColumnStretch(1,3);
// 右边上部分 右边控件
toprightlayout=new QHBoxLayout();
toprightlayout->setSpacing(25);
MyselfInfo=new QLabel("个人简历:");
MyselfInfoTextEdit=new QTextEdit;//文本编辑
//右边垂直布局
rightlayout=new QVBoxLayout();
rightlayout->addLayout(toprightlayout);
rightlayout->addWidget(MyselfInfo);
rightlayout->addWidget(MyselfInfoTextEdit);
// 右边下部分
okbutton=new QPushButton("确认");
cancelbutton=new QPushButton("退出");
buttomLayout=new QHBoxLayout();
//布局中添加一个弹性空间,使得在水平方向上,控件之间有一定的间距
buttomLayout->addStretch();
buttomLayout->addWidget(okbutton);
buttomLayout->addWidget(cancelbutton);
//
QGridLayout *mlayout=new QGridLayout(this);
mlayout->setContentsMargins(20, 20, 20, 20);
mlayout->setSpacing(10);
mlayout->addLayout(lLayout,0,0); // 左边
mlayout->addLayout(rightlayout,0,1); // 右上
mlayout->addLayout(buttomLayout,1,0,1,2);
mlayout->setSizeConstraint(QLayout::SetFixedSize);
}
Dialog::~Dialog()
{
}
二、QStackedWidget类(堆栈窗体)
#ifndef STACKEDDLG_H
#define STACKEDDLG_H
#include <QDialog>
#include <QListWidget>
#include <QStackedWidget>
#include <QLabel>
class stackedDlg : public QDialog
{
Q_OBJECT
public:
stackedDlg(QWidget *parent = nullptr);
~stackedDlg();
public:
QStackedWidget *statcks;
QListWidget *qlist; // 列表框控件创建
QLabel *lab1,*lab2,*lab3,*lab4,*lab5;
};
#endif // STACKEDDLG_H
#include "stackeddlg.h"
#include <QHBoxLayout>
stackedDlg::stackedDlg(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("堆栈窗体测试");//窗口标题
qlist=new QListWidget(this);
qlist->insertItem(0,"Linux1");
qlist->insertItem(1,"Linux2");
qlist->insertItem(2,"Linux3");
qlist->insertItem(3,"Linux4");
qlist->insertItem(4,"Linux5");
lab1=new QLabel("Linux1 Qt");
lab2=new QLabel("Linux2 Qt");
lab3=new QLabel("Linux3 Qt");
lab4=new QLabel("Linux4 Qt");
lab5=new QLabel("Linux5 Qt");
statcks=new QStackedWidget(this);
statcks->addWidget(lab1);
statcks->addWidget(lab2);
statcks->addWidget(lab3);
statcks->addWidget(lab4);
statcks->addWidget(lab5);
QHBoxLayout *mlayout=new QHBoxLayout(this);
mlayout->setContentsMargins(10,10,10,10);
mlayout->setSpacing(20);
mlayout->addWidget(qlist);
mlayout->addWidget(statcks,0,Qt::AlignCenter);
mlayout->setStretchFactor(qlist,1);
mlayout->setStretchFactor(statcks,5);
}
stackedDlg::~stackedDlg()
{
}
QStackedwidget 控件相当于一个窗口一样,提供一个空间专门存放一些控件(并且每次只能有一个控件可见,被设置为当前的控件)