• 用QtCreator实现可扩展对话框实验


    运行环境为Window XP

    实验目的和要求


    1.掌握扩展对话框的设计方法;
    2.掌握对话框常用控件的使用方法;
    3.要求最后提交完整代码; 

    实验内容与分析设计

    (1)本例实现了一个简单的填写资料的例子,通常情况下填写姓名和性别,在有特殊需要时,还需要填写更多信息则切换到完整对话框体。
      (2) 当单击“详细”按钮时,对话框扩展,显示其他更详细的信息,再次单击“详细”按钮,扩展窗口又重新隐藏。





    实验步骤与调试过程



    第一步:
        Ctrl+N新建工程  其他项目---空的Qt项目   命名为 extention


    步骤二
        Ctrl+N  新建选择  C++——C++源文件,命名为main.c


       

     在mian.c中写入以下代码

    //main.cpp
    #include <QApplication>
    #include "extension.h"
    
    
    int main(int argc, char * argv[])
    {
        QApplication app(argc,argv);
        QFont f("ZYSong18030",12);
        QApplication::setFont(f);
    
    
        QTranslator translator;
        translator.load("extension_zh");
        app.installTranslator(&translator);
    
    
        Extension ex;
        ex.show();
        return app.exec();
    }


    步骤三
        Ctrl+N  新建选择  C++——C++类,类命名为extention 基类写QObjecct或QDialog


        分别在.h .m中添加代码

    //extension.cpp
    #ifndef EXTENSION_H
    #define EXTENSION_H
    
    
    #include <QtGui>
    
    
    class Extension : public QDialog
    {
        Q_OBJECT
    public:
        Extension(QWidget *parent=0);
    
    
        void createBaseInfo();
        void createDetailInfo();
    
    
    public slots:
        void slotExtension();
    
    
    private:
        QWidget *baseWidget;
        QWidget *detailWidget;
    };
    
    
    #endif
    


    //extention.cpp
    #include "extension.h"
    
    
    Extension::Extension(QWidget *parent)
        : QDialog(parent)
    {
        setWindowTitle(tr("Extension Dialog"));
    
    
        createBaseInfo();
        createDetailInfo();
    
    
    
    
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(baseWidget);
        layout->addWidget(detailWidget);
        layout->setSizeConstraint(QLayout::SetFixedSize);
        layout->setSpacing(10);
        setLayout(layout);
    }
    
    
    void Extension::createBaseInfo()
    {
        baseWidget = new QWidget;
    
    
        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(slotExtension()));
    
    
        QDialogButtonBox *btnBox = new QDialogButtonBox(Qt::Vertical);
        btnBox->addButton(okButton,QDialogButtonBox::ActionRole);
        btnBox->addButton(detailButton,QDialogButtonBox::ActionRole);
    
    
        QGridLayout *grid = new QGridLayout;
        grid->addWidget(nameLabel,0,0);
        grid->addWidget(nameEdit,0,1);
        grid->addWidget(sexLabel,1,0);
        grid->addWidget(sexComboBox,1,1);
    
    
        QHBoxLayout *hbox = new QHBoxLayout;
        hbox->addLayout(grid);
        hbox->addStretch();
        hbox->addWidget(btnBox);
        baseWidget->setLayout(hbox);
    }
    
    
    void Extension::createDetailInfo()
    {
        detailWidget = new QWidget;
    
    
        QLabel *label1 = new QLabel(tr("Age:"));
        QLineEdit *ageEdit = new QLineEdit;
        ageEdit->setText("30");
        QLabel *label2 = new QLabel(tr("Department:"));
        QComboBox *deptComboBox = new QComboBox;
        deptComboBox->addItem(tr("dept 1"));
        deptComboBox->addItem(tr("dept 2"));
        deptComboBox->addItem(tr("dept 3"));
        deptComboBox->addItem(tr("dept 4"));
        QLabel *label3 = new QLabel(tr("email:"));
        QLineEdit *edit = new QLineEdit;
    
    
        QGridLayout *grid = new QGridLayout;
        grid->addWidget(label1,0,0);
        grid->addWidget(ageEdit,0,1);
        grid->addWidget(label2,1,0);
        grid->addWidget(deptComboBox,1,1);
        grid->addWidget(label3,2,0);
        grid->addWidget(edit,2,1);
    
    
        detailWidget->setLayout(grid);
        detailWidget->hide();
    }
    
    
    void Extension::slotExtension()
    {
        if (detailWidget->isHidden())
            detailWidget->show();
        else
            detailWidget->hide();
    }
    



    步骤四
        Ctrl+R 运行即可。



    实验结果

    点击 Detail按钮可以展开详细信息对话框,






    作者:
    出处:http://www.cnblogs.com/ChenYilong/(点击RSS订阅)
    本文版权归作者和博客园共有,欢迎转载,
    但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    ASP.NET Web 项目文件类型
    SQL Server 2008数据类型
    哎,终于还是在博客园安家了
    document.evaluate的详细用法
    Prototype1.5.1源代码解读分析4
    Prototype1.5.1源代码解读分析1
    每个.NET 开发人员应该下载的十个必备工具
    #Rgeion #End Region 中的关于折叠所有和不折叠的有的方法
    如何把web站点的所有.aspx.cs文件编译为.dll文件?
    Prototype1.5.1源代码解读分析3
  • 原文地址:https://www.cnblogs.com/ChenYilong/p/2808616.html
Copyright © 2020-2023  润新知