Qt QSplitter、QDockWidget及QStackedWidget综合布局的例子

导航页实现

新建Qt Gui应用,项目名称为“FormApplication”,基类选择“QDialog”,取消“创建界面”复选框的选中状态
新建类“Content”
content.h

#ifndef CONTENT_H
#define CONTENT_H

#include 
#include 
#include 
#include "baseinfo.h"
#include "contact.h"
#include "detail.h"

class Content : public QFrame
{
    Q_OBJECT

public:
    explicit Content(QWidget *parent = nullptr);
    ~Content();

    QStackedWidget *stack;
    QPushButton *AmendBtn;
    QPushButton *CloseBtn;

    BaseInfo *baseInfo;
    Contact *contact;
    Detail *detail;
};
#endif // CONTENT_H

content.cpp

  #include "content.h"

Content::Content(QWidget *parent)
   :QFrame(parent)
{
   stack = new QStackedWidget(this);
   stack->setFrameStyle(QFrame::Panel|QFrame::Raised); //对堆栈窗口的显示风格进行设置
   baseInfo = new BaseInfo();
   contact = new Contact();
   detail = new Detail();

   stack->addWidget(baseInfo);
   stack->addWidget(contact);
   stack->addWidget(detail);

   AmendBtn = new QPushButton(tr("修改"));
   CloseBtn = new QPushButton(tr("关闭"));
   QHBoxLayout *BtnLayout = new QHBoxLayout;
   BtnLayout->addStretch(1);
   BtnLayout->addWidget(AmendBtn);
   BtnLayout->addWidget(CloseBtn);

   QVBoxLayout *RightLayout = new QVBoxLayout(this);
   RightLayout->setMargin(10);
   RightLayout->setSpacing(6);
   RightLayout->addWidget(stack);
   RightLayout->addLayout(BtnLayout);
}

Content::~Content()
{

}

“修改用户基本信息”设计

新建类BaseInfo 基类选择 QWidget
baseinfo.h

#ifndef BASEINFO_H
#define BASEINFO_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

class BaseInfo : public QWidget
{
    Q_OBJECT
public:
    explicit BaseInfo(QWidget *parent = nullptr);
private:
    //左侧
    QLabel *UserNameLabel;
    QLabel *NameLabel;
    QLabel *SexLable;
    QLabel *DepartmentLabel;
    QLabel *AgeLable;
    QLabel *OtherLabel;
    QLineEdit *UserNameLineEdit;
    QLineEdit *NameLineEdit;
    QComboBox *SexComboBox;
    QTextEdit *DepartmentTextEdit;
    QLineEdit *AgeLineEdit;
    QGridLayout *LeftLayout;

    //右侧
    QLabel *HeadLable;
    QLabel *HeadIconLabel;
    QPushButton *UpdateHeadBtn;
    QHBoxLayout *TopRightLayout;

    QLabel *introductionLable;
    QTextEdit *IntroductionTextEdit;
    QVBoxLayout *RightLayout;

signals:

public slots:
};

#endif // BASEINFO_H

baseinfo.cpp

#include "baseinfo.h"

BaseInfo::BaseInfo(QWidget *parent) : QWidget(parent)
{
    /*** 左侧 ***/
    UserNameLabel = new QLabel(tr("用户:"));
    UserNameLineEdit = new QLineEdit;

    NameLabel = new QLabel(tr("姓名:"));
    NameLineEdit = new QLineEdit;

    SexLable = new QLabel(tr("性别:"));
    SexComboBox = new QComboBox;
    SexComboBox->addItem(tr("女:"));
    SexComboBox->addItem(tr("男:"));

    DepartmentLabel = new QLabel(tr("部门:"));
    DepartmentTextEdit = new QTextEdit;

    AgeLable = new QLabel(tr("年龄:"));
    AgeLineEdit = new QLineEdit;

    OtherLabel = new QLabel(("备注:"));
    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(SexLable,2,0);
    LeftLayout->addWidget(SexComboBox,2,1);

    LeftLayout->addWidget(DepartmentLabel,3,0);
    LeftLayout->addWidget(DepartmentTextEdit,3,1);

    LeftLayout->addWidget(AgeLable,4,0);
    LeftLayout->addWidget(AgeLineEdit,4,1);

    LeftLayout->addWidget(OtherLabel,5,0,1,2);
    //设定两列分别占用空间的比例为1:3
    LeftLayout->setColumnStretch(0,1);
    LeftLayout->setColumnStretch(1,3);

     /*** 右侧 ***/
    HeadLable = new QLabel(tr("头像:"));
    HeadIconLabel = new QLabel;
    QPixmap icon("312.png");
    HeadIconLabel->setPixmap(icon);
    HeadIconLabel->resize(icon.width(),icon.height());
    UpdateHeadBtn = new QPushButton(tr("更新"));

    TopRightLayout = new QHBoxLayout();

    TopRightLayout->setSpacing(20);
    TopRightLayout->addWidget(HeadLable);
    TopRightLayout->addWidget(HeadIconLabel);
    TopRightLayout->addWidget(UpdateHeadBtn);

    introductionLable = new QLabel(tr("个人说明:"));
    IntroductionTextEdit = new QTextEdit;

    RightLayout = new QVBoxLayout();
    RightLayout->setMargin(10);
    RightLayout->addLayout(TopRightLayout);
    RightLayout->addWidget(introductionLable);
    RightLayout->addWidget(IntroductionTextEdit);

    QGridLayout *mainlayout = new QGridLayout(this);
    mainlayout->setMargin(15); //设定对话框的边距为15
    mainlayout->setSpacing(10); //设定各个控件之间的间距为10
    mainlayout->addLayout(LeftLayout,0,0);
    mainlayout->addLayout(RightLayout,0,1);
    mainlayout->setSizeConstraint(QLayout::SetFixedSize);//设定最优化显示,并且使用用户无法改变对话框的大小
   } 

“显示用户联系方式”设计

新建类Contact,基类选择QWidget
contact.h

#ifndef CONTACT_H
#define CONTACT_H

#include 
#include 
#include 
#include 
#include 

class Contact : public QWidget
{
    Q_OBJECT
public:
   Contact(QWidget *parent = nullptr);
private:
    QLabel *EmailLabel;
    QLineEdit *EmailLineEdit;
    QLabel *AddrLabel;
    QLineEdit *AddrLineEdit;
    QLabel *CodeLabel;
    QLineEdit *CodeLineEdit;
    QLabel *MoviTelLabel;
    QLineEdit *MoviTelLineEdit;
    QCheckBox *MoviTelCheckBox;
    QLabel *ProTelLabel;
    QLineEdit *ProTelLineEdit;
    QGridLayout *mainLayout;

signals:

public slots:
};

#endif // CONTACT_H

contact.cpp

#include "contact.h"

Contact::Contact(QWidget *parent) : QWidget(parent)
{
    EmailLabel=new QLabel(tr("电子邮件:"));
    EmailLineEdit=new QLineEdit;

    AddrLabel = new QLabel(tr("联系地址:"));
    AddrLineEdit = new QLineEdit;

    CodeLabel = new QLabel(tr("邮政编码:"));
    CodeLineEdit = new QLineEdit;

    MoviTelLabel = new QLabel(tr("移动电话:"));
    MoviTelLineEdit = new QLineEdit;
    MoviTelCheckBox = new QCheckBox(tr("接收留言"));

    ProTelLabel = new QLabel(tr("办公电话:"));
    ProTelLineEdit = new QLineEdit;

    mainLayout = new QGridLayout(this);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(EmailLabel,0,0);
    mainLayout->addWidget(EmailLineEdit,0,1);
    mainLayout->addWidget(AddrLabel,1,0);
    mainLayout->addWidget(AddrLineEdit,1,1);
    mainLayout->addWidget(CodeLabel,2,0);
    mainLayout->addWidget(CodeLineEdit,2,1);
    mainLayout->addWidget(MoviTelLabel,3,0);
    mainLayout->addWidget(MoviTelLineEdit,3,1);
    mainLayout->addWidget(MoviTelCheckBox,3,2);
    mainLayout->addWidget(ProTelLabel,4,0);
    mainLayout->addWidget(ProTelLineEdit,4,1);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);

}

“显示用户的详细资料”设计
新建类Detail,基类选择QWidget
detail.h

#ifndef DETAIL_H
#define DETAIL_H

#include 
#include 
#include 
#include 
#include 
#include 

class Detail : public QWidget
{
    Q_OBJECT
public:
    explicit Detail(QWidget *parent = nullptr);
private:
    QLabel *NationalLabel;
    QComboBox *NationalComboBox;
    QLabel *ProvinceLabel;
    QComboBox *ProvinceComboBox;
    QLabel *CityLable;
    QLineEdit *CityLineEdit;
    QLabel *IntroductLabel;
    QTextEdit *IntroductTextEdit;
    QGridLayout *mainLayout;

signals:

public slots:
};

#endif // DETAIL_H

detail.cpp

#include "detail.h"

Detail::Detail(QWidget *parent) : QWidget(parent)
{
    NationalLabel = new QLabel("国家/地址:");
    NationalComboBox = new QComboBox;
    NationalComboBox->insertItem(0,tr("中国"));
    NationalComboBox->insertItem(1,tr("美国"));
    NationalComboBox->insertItem(2,tr("英国"));

    ProvinceLabel = new QLabel(tr("省份:"));
    ProvinceComboBox = new QComboBox;
    ProvinceComboBox->insertItem(0,tr("11"));
    ProvinceComboBox->insertItem(1,tr("22"));
    ProvinceComboBox->insertItem(2,tr("33"));

    CityLable = new QLabel(tr("城市:"));
    CityLineEdit = new QLineEdit;

    IntroductLabel = new QLabel(tr("个人说明:"));
    IntroductTextEdit = new QTextEdit;

    mainLayout = new QGridLayout(this);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(NationalLabel,0,0);
    mainLayout->addWidget(NationalComboBox,0,1);
    mainLayout->addWidget(ProvinceLabel,1,0);
    mainLayout->addWidget(ProvinceComboBox,1,1);
    mainLayout->addWidget(CityLable,2,0);
    mainLayout->addWidget(CityLineEdit,2,1);
    mainLayout->addWidget(IntroductLabel,3,0);
    mainLayout->addWidget(IntroductTextEdit,3,1);

}

编写主函数

main.cpp

#include "content.h"
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFont font("AR PL KaitiM GB",12);
    a.setFont(font);

    QSplitter *splitterMain = new QSplitter(Qt::Horizontal,0);
    splitterMain->setOpaqueResize(true);
    QListWidget *list = new QListWidget(splitterMain);
    list->insertItem(0,QObject::tr("基本信息"));
    list->insertItem(1,QObject::tr("联系方式"));
    list->insertItem(2,QObject::tr("详细资料"));

    Content *content = new Content(splitterMain);
    QObject::connect(list,SIGNAL(currentRowChanged(int)),content->stack,SLOT(setCurrentIndex(int)));

    splitterMain->setWindowTitle(QObject::tr("修改用户资料"));
    splitterMain->setMinimumSize(splitterMain->minimumSize());
    splitterMain->setMaximumSize(splitterMain->maximumSize());
    splitterMain->show();

    return a.exec();
}

工程结构及效果图

工程结构图
Qt QSplitter、QDockWidget及QStackedWidget综合布局的例子_第1张图片
程序运行效果图
Qt QSplitter、QDockWidget及QStackedWidget综合布局的例子_第2张图片

你可能感兴趣的:(Qt学习笔记)