欢迎访问我的独立博客

可伸缩对话框(含多重继承)

extensionDlg.pro

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += extensionDlg.h
SOURCES += extensionDlg.cpp main.cpp

extensionDlg.h

#ifndef EXTENSIONDLG_H
#define EXTENSIONDLG_H

#include <QtGui>

class ExtensionDlg : public QDialog
{
    Q_OBJECT
public:

    ExtensionDlg();

    void initBasicInfo();
    void initDetailInfo();

public slots:
    void slotExtension();

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;
    layout->addWidget(baseWidget);
    layout->addWidget(detailWidget);
    layout->setSizeConstraint(QLayout::SetFixedSize);
    layout->setSpacing(10);
    setLayout(layout);
}

void ExtensionDlg::initBasicInfo()
{
    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 ExtensionDlg::initDetailInfo()
{
    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 ExtensionDlg::slotExtension()
{
    if (detailWidget->isHidden())
    {
        detailWidget->show();
    }
    else
    {
        detailWidget->hide();
    }
}

 

main.cpp

#include <QApplication>
#include "extensionDlg.h"

int main(int argc, char * argv[])
{
    QApplication app(argc,argv);


    ExtensionDlg exDlg;
    exDlg.show();
    return app.exec();
}

 

程序剖析文档下载地址:

http://ishare.iask.sina.com.cn/f/35025724.html

 

posted @ 2012-12-12 13:55  github.com/starRTC  阅读(436)  评论(0编辑  收藏  举报