zoukankan      html  css  js  c++  java
  • 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
    


  • 相关阅读:
    基于term vector深入探查数据
    oracle 行转列
    oracle 统计成绩
    最全最新个税计算公式---今天你税了吗?
    .net反混淆脱壳工具de4dot的使用
    使用ILSpy软件反编译.Net应用程序的方法及注意事项
    EmguCV使用Stitcher类来拼接图像
    从睡姿就可以看出你的性格,据说非常准,快存!
    分享几个.NET WinForm开源组件,纪念逐渐远去的WinForm。。。
    【转载】关于.NET下开源及商业图像处理(PSD)组件
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3151297.html
Copyright © 2011-2022 走看看