新浪微博 有道云笔记 麦库 EverNote Pocket Instapaper 更多

用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按钮可以展开详细信息对话框,





posted @ 2012-11-10 20:05  iTeaTime(技术清谈)  阅读(431)  评论(0编辑  收藏  举报