1、实验目的和要求 1.掌握扩展对话框的设计方法; 2.掌握对话框常用控件的使用方法; 3.要求最后提交完整代码; 实验内容与分析设计 (1)本例实现了一个简单的填写资料的例子,通常情况下填写姓名和性别,在有特殊需要时,还需要填写更多信息则切换到完整对话框体。 (2) 当单击“详细”按钮时,对话框扩展,显示其他更详细的信息,再次单击“详细”按钮,扩展窗口又重新隐藏。 实验步骤与调试过程 1.新建工程-其他项目-空的Qt项目,命名为extension; 2.新建选择-C++—C++源文件,命名为main.cpp; 3.为main.cpp添加代码; 4
2、新建选择-C++—C++类,类命名为Extension 基类选择QObjecct;
5.为extension.cpp和extension.h添加代码;
6.调试运行。
实验结果
点击Detail按钮可以展开详细信息对话框
疑难小结
扩展后的对话框与扩展前的对话框之间的关联。
主要算法和程序清单
extension.h
#ifndef EXTESTION_H
#define EXTESTION_H
#include
3、ECT 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::E
4、xtension(QWidget *parent) : QDialog(parent) { setWindowTitle(tr("Extension")); createBaseInfo(); createDetailInfo(); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(baseWidget); layout->addWidget(detailWidget); layout->setSizeConstraint(QLayout
5、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:")); QComboBo
6、x *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(slotExtens
7、ion())); 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); g
8、rid->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::createDeta
9、ilInfo() { 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
10、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);
11、 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 (detailWidg
12、et->isHidden())
detailWidget->show();
else
detailWidget->hide();
}
main.cpp
#include
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818