Qt4----子例化QDialog(可扩展对话框的使用)

1、linux下安装Qt4请参考如下博文:

Qt4在linux下的安装


2、Qt4工程的创建请参考如下博文:

Qt4创建工程的几种方法:linux系统


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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值