资源描述
实验目的和要求
1.掌握扩展对话框的设计方法;
2.掌握对话框常用控件的使用方法;
3.要求最后提交完整代码;
实验内容与分析设计
(1)本例实现了一个简单的填写资料的例子,通常情况下填写姓名和性别,在有特殊需要时,还需要填写更多信息则切换到完整对话框体。
(2) 当单击“详细”按钮时,对话框扩展,显示其他更详细的信息,再次单击“详细”按钮,扩展窗口又重新隐藏。
实验步骤与调试过程
1.新建工程-其他项目-空的Qt项目,命名为extension;
2.新建选择-C++—C++源文件,命名为main.cpp;
3.为main.cpp添加代码;
4.新建选择-C++—C++类,类命名为Extension 基类选择QObjecct;
5.为extension.cpp和extension.h添加代码;
6.调试运行。
实验结果
点击Detail按钮可以展开详细信息对话框
疑难小结
扩展后的对话框与扩展前的对话框之间的关联。
主要算法和程序清单
extension.h
#ifndef EXTESTION_H
#define EXTESTION_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
extension.cpp
#include "extension.h"
Extension::Extension(QWidget *parent) :
QDialog(parent)
{
setWindowTitle(tr("Extension"));
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();
}
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();
}
展开阅读全文