博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt 实现可扩展对话框
阅读量:6965 次
发布时间:2019-06-27

本文共 3277 字,大约阅读时间需要 10 分钟。

实现效果:


  

  

代码

1.extension.h文件  #ifndef EXTENSION_H #define EXTENSION_H #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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_H
2extension.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("male");     sexComboBox->addItem("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 *gride = new QGridLayout;     gride->addWidget(nameLabel,0,0);     gride->addWidget(nameEdit,0,1);     gride->addWidget(sexLabel,1,0);     gride->addWidget(sexComboBox,1,1);       QHBoxLayout *hbox = new QHBoxLayout;     hbox->addLayout(gride);     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();     } }
3.main.cpp文件 #include
#include"extension.h" int main(int argc,char *argv[]) { QApplication app(argc,argv); Extension exten; exten.show(); return app.exec(); }


     本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208707,如需转载请自行联系原作者

你可能感兴趣的文章
XSD文件读取
查看>>
mariadb安装
查看>>
JSON TO NSDictionary Mac & iOS
查看>>
成长计划
查看>>
opencv工具类小集
查看>>
foobox 4.2(foobar2000 CUI配置)
查看>>
django日志写入文件
查看>>
hana system replication
查看>>
Android MediaPlayer的生命周期
查看>>
shell中的函数、数组、告警系统分析
查看>>
关于我们
查看>>
jQuery之input
查看>>
下载文件 利用NSURLSession下载文件
查看>>
正则表达式(grep,egrep,fgrep)
查看>>
hadoop-namenode启动过程及坏块处理流程
查看>>
Java虚拟机学习 - 对象引用强度
查看>>
shell 检查文件是否被串改
查看>>
linux文件共享
查看>>
TEC-003-sqlmap安装及使用
查看>>
CentOS6.4_X86_64 安装Drupal-7.31必须成功版!
查看>>