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,如需转载请自行联系原作者
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值