Qt之ui在程序中的使用——(2)多继承法

简介:

thirdDialog.h

  1. #ifndef THIRDDIALOG_H  
  2. #define THIRDDIALOG_H  
  3.   
  4. #include <QtGui>  
  5. #include "ui_third.h"  
  6.   
  7. class thirdDialog:public QDialog,private Ui::Third  
  8. {  
  9.     Q_OBJECT  
  10. public:  
  11.     thirdDialog(QWidget *parent=0);  
  12.     ~thirdDialog();  
  13. };  
  14.   
  15. #endif  

thirdDialog.cpp

  1. #include "thirdDialog.h"  
  2.   
  3. thirdDialog::thirdDialog(QWidget *parent)  
  4. {  
  5.     setupUi(this);  
  6. }  
  7.   
  8. thirdDialog::~thirdDialog()  
  9. {  
  10.   
  11. }  

maindialog.h

  1. #ifndef MAINDIALOG_H  
  2. #define MAINDIALOG_H  
  3.   
  4. #include <QtGui>  
  5. #include "ui_first.h"  
  6. #include "ui_second.h"  
  7. #include "thirdDialog.h"  
  8.   
  9.   
  10. class MainDialog : public QDialog  
  11. {  
  12.     Q_OBJECT  
  13.   
  14. public:  
  15.     MainDialog(QWidget *parent = 0, Qt::WFlags flags = 0);  
  16.     ~MainDialog();  
  17. private:  
  18.     Ui::First firstUi;  
  19.     Ui::Second secondUi;  
  20.   
  21.     private slots:  
  22.         void on_btnChild_clicked();  
  23.       
  24. };  
  25.   
  26. #endif // MAINDIALOG_H  

maindialog.cpp

  1. #include "maindialog.h"  
  2.   
  3. MainDialog::MainDialog(QWidget *parent, Qt::WFlags flags)  
  4.     : QDialog(parent, flags)  
  5. {  
  6.     QTabWidget *tabWidget = new QTabWidget(this);  
  7.   
  8.     QDialog *w1 = new QDialog;  
  9.     firstUi.setupUi(w1);  
  10.     QWidget *w2 = new QWidget;  
  11.     secondUi.setupUi(w2);  
  12.   
  13.     tabWidget->addTab(w1,tr("First Tab"));  
  14.     tabWidget->addTab(w2,tr("Second Tab"));  
  15.     tabWidget->resize(300,300);  
  16.   
  17.     connect(firstUi.btnClose,SIGNAL(clicked()),this,SLOT(close()));  
  18.     connect(secondUi.btnChild,SIGNAL(clicked()),this,SLOT(on_btnChild_clicked()));  
  19. }  
  20.   
  21. MainDialog::~MainDialog()  
  22. {  
  23.   
  24. }  
  25.   
  26. void MainDialog::on_btnChild_clicked()  
  27. {  
  28.     thirdDialog *dlg = new thirdDialog;  
  29.     dlg->exec();  
  30. }  

分析:彩虹

多继承方式可直接对ui界面上的控件或函数进行操作,代码编写更简洁;

而是用单继承方式,在操作ui页面上的控件时需加上ui对象前缀,编写代码较为麻烦。

但,对于程序中所需ui页面较多时,使用单继承法则要灵活的多。。


作者: 韩兆新
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:  [02]Qt语言基础
标签:  Qt学习笔记

本文转自韩兆新博客博客园博客,原文链接:http://www.cnblogs.com/hanzhaoxin/archive/2012/11/13/2767503.html,如需转载请自行联系原作者
目录
相关文章
|
Web App开发 存储 Linux
Linux(33)Rockchip RK3568 Ubuntu22.04上通过SSH运行Qt程序和关闭Chrome的密钥提示
Linux(33)Rockchip RK3568 Ubuntu22.04上通过SSH运行Qt程序和关闭Chrome的密钥提示
1062 0
【QT】读写.ini配置文件的程序实现
【QT】读写.ini配置文件的程序实现
232 0
|
算法 数据可视化 程序员
【Qt UI】调色板QPalette类在Qt编程中的应用
【Qt UI】调色板QPalette类在Qt编程中的应用
572 0
|
算法 API C++
【Qt UI】QT 窗口/控件置顶方法详解
【Qt UI】QT 窗口/控件置顶方法详解
1082 0
|
算法 前端开发 C++
【Qt UI相关】Qt设置窗体或控件的背景色透明
【Qt UI相关】Qt设置窗体或控件的背景色透明
1813 0
|
存储 测试技术 UED
Qt中实现界面回放的艺术:从理论到代码“ (“The Art of Implementing UI Playback in Qt: From Theory to Code
Qt中实现界面回放的艺术:从理论到代码“ (“The Art of Implementing UI Playback in Qt: From Theory to Code
274 1
|
Linux iOS开发 开发者
Qt问题(二):无法定位程序输入点于动态链接库
动态链接库(Dynamic Link Library,简称DLL)是一种可执行文件格式,常见于Windows操作系统中,而在Linux和macOS等其他操作系统中,相似的概念通常被称为共享库(Shared Library)。动态链接库允许程序在运行时加载所需的代码和数据,而不是在编译时静态链接到应用程序中。这种方式带来了几个重要的优点:
1170 3
|
开发框架 自然语言处理 Linux
Qt:构建强大跨平台应用程序的框架
Qt:构建强大跨平台应用程序的框架
|
12月前
|
C语言 Android开发 C++
基于MTuner软件进行qt的mingw编译程序的内存泄漏检测
本文介绍了使用MTuner软件进行Qt MinGW编译程序的内存泄漏检测的方法,提供了MTuner的下载链接和测试代码示例,并通过将Debug程序拖入MTuner来定位内存泄漏问题。
250 4
基于MTuner软件进行qt的mingw编译程序的内存泄漏检测

推荐镜像

更多
  • qt