Qt4----子例化QDialog(可扩展对话框的使用)
1、linux下安装Qt4请参考如下博文:
2、Qt4工程的创建请参考如下博文:
3、可扩展对话框
通过纯代码的形式,建立工程。点击【Detail】按钮,显示扩展对话框
包括四部分:
工程文件:ExtensionDlg.pro
主程序文件:main.cpp
对话框类:ExtensionDlg.h
实现文件:ExtensionDlg.cpp
4、实例运行效果:
5、代码区:
main()函数
#include <QApplication> #include "ExtensionDlg.h" int main(int argc, char* argv[]) { QApplication app(argc, argv); ExtensionDlg exDlg; exDlg.show(); return app.exec(); }
ExtensionDlg.h文件
#ifndef EXTENSIONDLG_H #define EXTENSIONDLG_H #include <QtGui> class ExtensionDlg:public QDialog { Q_OBJECT //加入Q——OBJECT宏,程序中用到信号/槽等Qt核心机制的,都需要加入此宏 public: ExtensionDlg(); //构造函数 void initBasicInfo(); //初始化基础信息 void initDetailInfo(); //初始化扩展信息 public slots: //声明共有槽 void slot2Extension(); //单击Detail按钮是被触发 private: QWidget* baseWidget; //伸缩前对话框窗体,必须为指针 QWidget* detailWidget; //扩展后对话框窗体,必须为指针 }; #endif // EXTENSIONDLG_H
ExtensionDlg.cpp文件
#include "ExtensionDlg.h" ExtensionDlg::ExtensionDlg() { setWindowTitle(tr("Extension Dialog")); //显示标题 initBasicInfo(); //初始化基本信息窗体 initDetailInfo(); //初始化扩展信息窗体 QVBoxLayout* layout = new QVBoxLayout; //定义一个垂直布局类实体,QHBoxLayout为水平布局类实体 layout->addWidget(baseWidget); //加入baseWidget layout->addWidget(detailWidget); //加入DetailWidget layout->setSizeConstraint(QLayout::SetFixedSize); //设置窗体缩放模式,此处设置为固定大小 layout->setSpacing(6); //窗口部件之间间隔大小 setLayout(layout); //加载到窗体上 } void ExtensionDlg::initBasicInfo() { baseWidget = new QWidget; //实例化baseWidget QLabel* nameLabel = new QLabel(tr("Name")); //定义窗体部件 QLineEdit* nameEdit = new QLineEdit; QLabel* sexLabel = new QLabel(tr("Sex")); QComboBox* sexComboBox = new QComboBox; sexComboBox->addItem(tr("male")); sexComboBox->addItem(tr("female")); QPushButton* okButton = new QPushButton(tr("OK")); QPushButton* detailButton = new QPushButton(tr("Detail")); connect(detailButton, SIGNAL(clicked()), this, SLOT(slot2Extension())); //使用信号/槽机制 QDialogButtonBox* btnBox = new QDialogButtonBox(Qt::Horizontal); //QDialogButtonBox使用方法 btnBox->addButton(okButton, QDialogButtonBox::ActionRole); btnBox->addButton(detailButton, QDialogButtonBox::ActionRole); QFormLayout* formLayout = new QFormLayout; //表单布局方法 formLayout->addRow(nameLabel, nameEdit); formLayout->addRow(sexLabel, sexComboBox); QVBoxLayout* vboxLayout = new QVBoxLayout; //窗体顶级布局,布局本身也是一种窗口部件 vboxLayout->addLayout(formLayout); //顶层窗体加入表单 vboxLayout->addWidget(btnBox); //顶层窗体加入按钮 baseWidget->setLayout(vboxLayout); //加载到窗体上 } void ExtensionDlg::initDetailInfo() { detailWidget = new QWidget; QLabel* ageLabel = new QLabel(tr("Age")); QLineEdit* ageEdit = new QLineEdit; ageEdit->setText(tr("25")); QLabel* deptLabel = new QLabel(tr("Department")); QComboBox* deptComboBox = new QComboBox; deptComboBox->addItem(tr("department 1")); deptComboBox->addItem(tr("department 2")); deptComboBox->addItem(tr("department 3")); deptComboBox->addItem(tr("department 4")); QLabel* addressLabel = new QLabel(tr("Address")); QLineEdit* addressEdit = new QLineEdit; QFormLayout* formLayout = new QFormLayout; formLayout->addRow(ageLabel, ageEdit); formLayout->addRow(deptLabel, deptComboBox); formLayout->addRow(addressLabel, addressEdit); detailWidget->setLayout(formLayout); detailWidget->hide(); //将扩展信息窗口隐藏,hide()是Qt默认槽函数之一 } void ExtensionDlg::slot2Extension() { if(detailWidget->isHidden()) //ishidden()函数判断扩展窗口显隐状态 detailWidget->show(); else detailWidget->hide(); }
ExtensionDlg.pro文件
TEMPLATE = app TARGET = DEPENDPATH += INCLUDEPATH += # Input HEADERS += \ ExtensionDlg.h SOURCES += \ ExtensionDlg.cpp \ main.cpp