可扩展对话框通常用于用户对界面有不同要求的场合。通常情况下,只出现基本对话窗体;当供高级用户使用或需要更多信息是,可通过某种方式的切换显示完整对话窗体(扩展窗体),切换的工作通常由一个按钮来实现。
可扩展对话框的基本实现方法是利用setSizeConstraint(QLayout::SetFixedSize)
方法是对话框尺寸保持相对固定。其中,最关键的部分有以下两点。
- 在整个对话框的构造函数中调用。
layout->setSizeConstraint(QLayout::SetFixedSize);
这个设置保证了对话框的尺寸保持相对固定,始终保持各个控件组合的默认尺寸。在扩展部分显示时,对话框尺寸根据需要显示的控件被扩展;而在扩展部分影藏时,对话框尺寸又恢复至初始状态。
- 切换按钮的实现。整个窗体可扩展的工作都是在此按钮所连接的槽函数中完成的。
下面通过一个实例来说明它的使用:
一、运行结果
![在这里插入图片描述 学习QT之可扩展对话框_可扩展](//dev-img.mos.moduyun.com/20231020/70f6d336-fbc9-4721-aa2b-5dcdf29feb7a.png)
![在这里插入图片描述 学习QT之可扩展对话框_可扩展_02](//dev-img.mos.moduyun.com/20231020/74f1b729-9787-4ffe-ae3e-0ce4c9cc6ba6.png)
二、实现原理
创建两个Widge
,一个基本窗体,一个详细窗体,都加入到主窗体的布局中,然后将详细窗体隐藏。当点击了详细按钮时,将详细窗体显示出来。
三、详细代码
extensionDlg.h
#ifndef EXTENSIONDLG_H
#define EXTENSIONDLG_H
#include <QDialog>
class ExtensionDlg : public QDialog
{
Q_OBJECT
public:
ExtensionDlg(QWidget *parent = 0);
~ExtensionDlg();
private slots:
void showDetailInfo();
private:
void createBaseInfo(); //实现基本对话框窗体部分
void createDetailInfo(); //实现扩展窗体部分
QWidget *baseWidget; //基本对话框窗体部分
QWidget *detailWidget; //扩展窗体部分
};
#endif // EXTENSIONDLG_H
extensionDlg.cpp
#include "extensiondlg.h"
#include <QVBoxLayout>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QLabel>
ExtensionDlg::ExtensionDlg(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("Extension Dialog"));
createBaseInfo();
createDetailInfo();
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(baseWidget);
layout->addWidget(detailWidget);
//设置窗体的大小固定,不能利用拖拽改变大小,否则当再次单击“详细”按钮时,对话框不能恢复到初始状态
layout->setSizeConstraint(QLayout::SetFixedSize);
layout->setSpacing(10);
}
ExtensionDlg::~ExtensionDlg()
{
}
void ExtensionDlg::createBaseInfo()
{
baseWidget = new QWidget;
QLabel *nameLabel = new QLabel(tr("姓名: "));
QLineEdit *nameLineEdit = new QLineEdit;
QLabel *sexLabel = new QLabel(tr("性别: "));
QComboBox *sexComboBox = new QComboBox;
sexComboBox->insertItem(0,tr("男"));
sexComboBox->insertItem(1,tr("女"));
QGridLayout *LeftLayout = new QGridLayout;
LeftLayout->addWidget(nameLabel,0,0);
LeftLayout->addWidget(nameLineEdit,0,1);
LeftLayout->addWidget(sexLabel);
LeftLayout->addWidget(sexComboBox);
QPushButton *OkBtn = new QPushButton(tr("确定"));
QPushButton *DetailBtn = new QPushButton(tr("详细"));
QDialogButtonBox *btnBox = new QDialogButtonBox(Qt::Vertical);
btnBox->addButton(OkBtn,QDialogButtonBox::ActionRole);
btnBox->addButton(DetailBtn,QDialogButtonBox::ActionRole);
QHBoxLayout *mainLayout = new QHBoxLayout(baseWidget);
mainLayout->addLayout(LeftLayout);
mainLayout->addWidget(btnBox);
connect(DetailBtn,SIGNAL(clicked(bool)),this,SLOT(showDetailInfo()));
}
void ExtensionDlg::createDetailInfo()
{
detailWidget = new QWidget;
QLabel *ageLabel = new QLabel(tr("年龄: "));
QLineEdit *ageLineEdit = new QLineEdit;
ageLineEdit->setText(tr("30"));
QLabel *departmentLabel = new QLabel(tr("部门: "));
QComboBox *depratmentComboBox = new QComboBox;
depratmentComboBox->addItem(tr("研发部"));
depratmentComboBox->addItem(tr("人事部"));
depratmentComboBox->addItem(tr("财务部"));
depratmentComboBox->addItem(tr("业务部"));
QLabel *emailLabel = new QLabel(tr("Email: "));
QLineEdit *emailLineEdit = new QLineEdit;
QGridLayout *mainLayout = new QGridLayout(detailWidget);
mainLayout->addWidget(ageLabel,0,0);
mainLayout->addWidget(ageLineEdit,0,1);
mainLayout->addWidget(departmentLabel,1,0);
mainLayout->addWidget(depratmentComboBox,1,1);
mainLayout->addWidget(emailLabel,2,0);
mainLayout->addWidget(emailLineEdit,2,1);
detailWidget->hide();
}
void ExtensionDlg::showDetailInfo()
{
if(detailWidget->isHidden())
detailWidget->show();
else
detailWidget->hide();
}