可扩展对话框

           可扩展对话框通常需要更多信息时,通过某一种方式切换显示出完整的对话窗体,切换通常由一个按钮来实现。  

1.  在整个对话框中的构造函数中调用,这个设置保证了对话框的尺寸相对固定,始终.保持各个组件 的默认尺寸。在扩展部分显示时,对话框尺寸根据需要显示的控件进行扩建调整;而在扩展部分隐藏时,对话框尺寸又恢复至初始状态。      
        layout->setSizeConstraint(QLayout::SetFixedSize);
2.  切换按钮的实现。整个窗体可扩展的工作都是在此按钮所连接的槽函数中完成的。
#ifndef EXTENSIONDLG_H
#define EXTENSIONDLG_H

#include <QDialog>

class ExtensionDlg : public QDialog
{
    Q_OBJECT

public:
    ExtensionDlg(QWidget *parent = nullptr);
    ~ExtensionDlg();

private:
    void  createBaseInfo();   //实现基本对话框的窗体部分
    void  createDetailInfo();  //实现扩展对话框的窗体部分
    QWidget *widgetBass;     //基本对话窗体部分
    QWidget *widgetDetail;   //基本对话窗体部分

private slots:
    void  showDetailInfo();
};
#endif // EXTENSIONDLG_H
#include "extensiondlg.h"
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QLabel>
#include <QComboBox>
#include <QPushButton>
#include <QGridLayout>
#include <QFrame>
#include <QTextEdit>
#include <QVBoxLayout>
ExtensionDlg::ExtensionDlg(QWidget *parent)
    : QDialog(parent)
{

    setWindowTitle(QStringLiteral("Extension Dialog"));
    createBaseInfo();
    createDetailInfo();

    QVBoxLayout *layout=new QVBoxLayout(this);
    layout->addWidget(widgetBass);
    layout->addWidget(widgetDetail);
    layout->setSizeConstraint(QLayout::SetFixedSize);//设置窗体的大小固定,不能利用拖拽改变大小
    layout->setSpacing(10);

}

ExtensionDlg::~ExtensionDlg()
{
}

void ExtensionDlg::createBaseInfo()//基本对话框窗体部分
{

   // widgetBass=new QWidget;
    QLabel *labname=new QLabel(QStringLiteral("姓名"));
    QLineEdit *linename=new  QLineEdit;

    QLabel *labsex=new QLabel(QStringLiteral("性别"));
    QComboBox *sexCombox=new QComboBox;
    sexCombox->insertItem(0,QStringLiteral("女"));
    sexCombox->insertItem(1,QStringLiteral("男"));

    QGridLayout *LeftLayout=new QGridLayout;
    LeftLayout->addWidget(labname,0,0);
    LeftLayout->addWidget(linename,0,1);
    LeftLayout->addWidget(labsex,1,0);
    LeftLayout->addWidget(sexCombox,1,1);

    QPushButton *btnOk=new QPushButton(QStringLiteral("确定"));
    QPushButton *btnDetail=new QPushButton(QStringLiteral("详细"));

    //添加确定和详细两个按钮
    QDialogButtonBox *btnBox=new QDialogButtonBox(Qt::Vertical);
    btnBox->addButton(btnOk,QDialogButtonBox::ActionRole);
    btnBox->addButton(btnDetail,QDialogButtonBox::ActionRole);

    QHBoxLayout *mainLayout=new QHBoxLayout;
    mainLayout->addLayout(LeftLayout);
    mainLayout->addWidget(btnBox);

    //将布局放入窗体中
    widgetBass=new QWidget();
    widgetBass->setLayout(mainLayout);
    connect(btnDetail,SIGNAL(clicked()),this,SLOT(showDetailInfo()));

}


//实现扩展对话框的窗体部分
void ExtensionDlg::createDetailInfo()
{

    QLabel *labage=new QLabel(QStringLiteral("年纪"));
    QLineEdit *lineage=new  QLineEdit;

    QLabel *labdepartment=new QLabel(QStringLiteral("部门"));
    QComboBox *comboxDepartment=new QComboBox;
    comboxDepartment->addItem(QStringLiteral("部门1"));
    comboxDepartment->addItem(QStringLiteral("部门2"));
    comboxDepartment->addItem(QStringLiteral("部门3"));
    comboxDepartment->addItem(QStringLiteral("部门4"));

    QLabel *labEmail=new QLabel(QStringLiteral("Email"));
    QLineEdit *lineEmail=new  QLineEdit;

    QGridLayout *bottomLayout=new QGridLayout;
    bottomLayout->addWidget(labage,0,0);
    bottomLayout->addWidget(lineage,0,1);
    bottomLayout->addWidget(labdepartment,1,0);
    bottomLayout->addWidget(comboxDepartment,1,1);
    bottomLayout->addWidget(labEmail,2,0);
    bottomLayout->addWidget(lineEmail,2,1);

    //将布局放入窗体中
    widgetDetail=new QWidget();
    widgetDetail->setLayout(bottomLayout);
    widgetDetail->hide();

}

void ExtensionDlg::showDetailInfo()
{
    if(widgetDetail->isHidden())
        widgetDetail->show();
    else  widgetDetail->hide();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值