Qt 实现可扩展对话框

简介:

实现效果:


  


  


代码

1.extension.h文件  #ifndef EXTENSION_H #define EXTENSION_H #include <QApplication> #include <QPushButton> #include <QDialog> #include <QGridLayout> #include <QWidget> #include <QHBoxLayout> #include <QLabel> #include <QLineEdit> #include <QComboBox> #include <QDialogButtonBox> #include <QGridLayout> #include <QDialog>  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<QApplication> #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,如需转载请自行联系原作者




相关文章
|
5月前
|
Linux iOS开发 MacOS
19 QT - 标准文件对话框
19 QT - 标准文件对话框
30 0
|
3月前
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
41 0
|
5月前
15 QT - 对话框QDialog概述
15 QT - 对话框QDialog概述
20 0
|
2月前
|
存储
QT基础入门——QMainWindow与对话框QDialog(三)
QT基础入门——QMainWindow与对话框QDialog(三)
52 0
QT基础入门——QMainWindow与对话框QDialog(三)
|
8月前
QT5基本对话框
QFileDialog类的几个静态函数见上表,用户通过这些函数可以很方便地定制 自己的文件对话框。其中,getOpenFileName()函数返回用户选择的文件名。但是当 用户在选择文件时,如果选择“取消”(Cancel),则返回一个空串。在此仅详细说 明getOpenFileName()静态函数中各个参数的作用,其他文件对话框类中相关的静态函数 的参数有与其类似之处。
29 0
QT5基本对话框
|
4月前
|
数据安全/隐私保护
QT基础教程(对话框2)
QT基础教程(对话框2)
32 0
|
4月前
QT基础教程(对话框1)
QT基础教程(对话框1)
33 0
|
4月前
|
C++
C++ Qt开发:自定义Dialog对话框组件
在之前的文章中笔者已经为大家展示了默认`Dialog`组件的使用方法,虽然内置组件支持对数据的输入,但有时候我们需要一次性输入多个数据,此时如果之使用默认模态对话框似乎不太够用,此时我们需要自己创建一个自定义对话框,需要说明的是此类对话框也是一种窗体,所以可以在其上面放置任何通用组件,以实现更多复杂的开发需求。自定义对话框需要解决的问题是,如何让父窗体与子窗体进行数据交换,要实现数据的交换有两种方式,第一种方式是通过动态加载模态对话框,当用户点击确定后通过`GetValue()`来拿到数据,而第二种方式则是通过发送信号的方式将数据投递给父窗体,这两种方式都可以,读者可根据自身需求来选择不同的通
39 1
C++ Qt开发:自定义Dialog对话框组件
|
4月前
|
数据安全/隐私保护 C++ 开发者
C++ Qt开发:标准Dialog对话框组件
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍标准对话框`QInputDialog`、`QFileDialog `这两种对话框组件的常用方法及灵活运用。在 Qt 中,标准对话框提供了一些常见的用户交互界面,用于执行特定任务,例如获取用户输入、选择文件路径、显示消息等。这些对话框通常具有标准化的外观和行为,使得在不同的平台上能够保持一致性。在一般的开发过程中,标准对话框是开发者常用的工具之一。
50 1
C++ Qt开发:标准Dialog对话框组件
|
5月前
|
API
18 QT - 消息对话框
18 QT - 消息对话框
24 0

推荐镜像

更多