Qt之ui在程序中的使用-多繼承法介紹
更新時(shí)間:2012年11月13日 11:35:44 作者:
本文將介紹Qt之ui在程序中的使用-多繼承法,需要的朋友可以參考
thirdDialog.h
#ifndef THIRDDIALOG_H
#define THIRDDIALOG_H
#include <QtGui>
#include "ui_third.h"
class thirdDialog:public QDialog,private Ui::Third
{
Q_OBJECT
public:
thirdDialog(QWidget *parent=0);
~thirdDialog();
};
#endif
thirdDialog.cpp
#include "thirdDialog.h"
thirdDialog::thirdDialog(QWidget *parent)
{
setupUi(this);
}
thirdDialog::~thirdDialog()
{
}
maindialog.h
#ifndef MAINDIALOG_H
#define MAINDIALOG_H
#include <QtGui>
#include "ui_first.h"
#include "ui_second.h"
#include "thirdDialog.h"
class MainDialog : public QDialog
{
Q_OBJECT
public:
MainDialog(QWidget *parent = 0, Qt::WFlags flags = 0);
~MainDialog();
private:
Ui::First firstUi;
Ui::Second secondUi;
private slots:
void on_btnChild_clicked();
};
#endif // MAINDIALOG_H
maindialog.cpp
#include "maindialog.h"
MainDialog::MainDialog(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
QTabWidget *tabWidget = new QTabWidget(this);
QDialog *w1 = new QDialog;
firstUi.setupUi(w1);
QWidget *w2 = new QWidget;
secondUi.setupUi(w2);
tabWidget->addTab(w1,tr("First Tab"));
tabWidget->addTab(w2,tr("Second Tab"));
tabWidget->resize(300,300);
connect(firstUi.btnClose,SIGNAL(clicked()),this,SLOT(close()));
connect(secondUi.btnChild,SIGNAL(clicked()),this,SLOT(on_btnChild_clicked()));
}
MainDialog::~MainDialog()
{
}
void MainDialog::on_btnChild_clicked()
{
thirdDialog *dlg = new thirdDialog;
dlg->exec();
}
分析:
多繼承方式可直接對(duì)ui界面上的控件或函數(shù)進(jìn)行操作,代碼編寫更簡(jiǎn)潔;
而是用單繼承方式,在操作ui頁(yè)面上的控件時(shí)需加上ui對(duì)象前綴,編寫代碼較為麻煩。
但,對(duì)于程序中所需ui頁(yè)面較多時(shí),使用單繼承法則要靈活的多。。
復(fù)制代碼 代碼如下:
#ifndef THIRDDIALOG_H
#define THIRDDIALOG_H
#include <QtGui>
#include "ui_third.h"
class thirdDialog:public QDialog,private Ui::Third
{
Q_OBJECT
public:
thirdDialog(QWidget *parent=0);
~thirdDialog();
};
#endif
thirdDialog.cpp
#include "thirdDialog.h"
thirdDialog::thirdDialog(QWidget *parent)
{
setupUi(this);
}
thirdDialog::~thirdDialog()
{
}
maindialog.h
復(fù)制代碼 代碼如下:
#ifndef MAINDIALOG_H
#define MAINDIALOG_H
#include <QtGui>
#include "ui_first.h"
#include "ui_second.h"
#include "thirdDialog.h"
class MainDialog : public QDialog
{
Q_OBJECT
public:
MainDialog(QWidget *parent = 0, Qt::WFlags flags = 0);
~MainDialog();
private:
Ui::First firstUi;
Ui::Second secondUi;
private slots:
void on_btnChild_clicked();
};
#endif // MAINDIALOG_H
maindialog.cpp
復(fù)制代碼 代碼如下:
#include "maindialog.h"
MainDialog::MainDialog(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
QTabWidget *tabWidget = new QTabWidget(this);
QDialog *w1 = new QDialog;
firstUi.setupUi(w1);
QWidget *w2 = new QWidget;
secondUi.setupUi(w2);
tabWidget->addTab(w1,tr("First Tab"));
tabWidget->addTab(w2,tr("Second Tab"));
tabWidget->resize(300,300);
connect(firstUi.btnClose,SIGNAL(clicked()),this,SLOT(close()));
connect(secondUi.btnChild,SIGNAL(clicked()),this,SLOT(on_btnChild_clicked()));
}
MainDialog::~MainDialog()
{
}
void MainDialog::on_btnChild_clicked()
{
thirdDialog *dlg = new thirdDialog;
dlg->exec();
}
分析:
多繼承方式可直接對(duì)ui界面上的控件或函數(shù)進(jìn)行操作,代碼編寫更簡(jiǎn)潔;
而是用單繼承方式,在操作ui頁(yè)面上的控件時(shí)需加上ui對(duì)象前綴,編寫代碼較為麻煩。
但,對(duì)于程序中所需ui頁(yè)面較多時(shí),使用單繼承法則要靈活的多。。
您可能感興趣的文章:
- Qt實(shí)現(xiàn)圖片移動(dòng)實(shí)例(圖文教程)
- Qt for Android開發(fā)實(shí)例教程
- Qt 實(shí)現(xiàn)桌面雪花飄落代碼
- Qt定時(shí)器和隨機(jī)數(shù)詳解
- qt實(shí)現(xiàn)倒計(jì)時(shí)示例
- Windows下Eclipse+PyDev配置Python+PyQt4開發(fā)環(huán)境
- Qt qml中l(wèi)istview 列表視圖控件(下拉刷新、上拉分頁(yè)、滾動(dòng)軸)
- QTabWidget標(biāo)簽實(shí)現(xiàn)雙擊關(guān)閉的方法(推薦)
- C++ Qt屬性系統(tǒng)詳細(xì)介紹
相關(guān)文章
VC++實(shí)現(xiàn)View內(nèi)容保存為圖片的方法
這篇文章主要介紹了VC++實(shí)現(xiàn)View內(nèi)容保存為圖片的方法,涉及VC++中Bitmap類的save方法相關(guān)使用技巧,需要的朋友可以參考下2016-08-08
輸出1000以內(nèi)的素?cái)?shù)的算法(實(shí)例代碼)
本篇文章是對(duì)輸出1000以內(nèi)的素?cái)?shù)的算法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語(yǔ)言常用庫(kù)函數(shù)的使用及模擬實(shí)現(xiàn)詳解例舉
C語(yǔ)言庫(kù)函數(shù)是把自定義函數(shù)放到庫(kù)里,是別人把一些常用到的函數(shù)編完放到一個(gè)文件里,供程序員使用,下面讓我們一起來(lái)詳細(xì)了解它2022-04-04
動(dòng)態(tài)數(shù)組C++實(shí)現(xiàn)方法(分享)
下面小編就為大家?guī)?lái)一篇?jiǎng)討B(tài)數(shù)組C++實(shí)現(xiàn)方法(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-05-05
詳細(xì)解析C語(yǔ)言中的開方實(shí)現(xiàn)
這篇文章主要介紹了詳細(xì)解析C語(yǔ)言中的開方實(shí)現(xiàn),包括一道要求精度的整數(shù)開方的題目,需要的朋友可以參考下2015-08-08
Matlab實(shí)現(xiàn)四種HSV色輪圖繪制的示例代碼
色輪圖就是色彩相位圖,它完整表現(xiàn)了色相環(huán)360度的全部顏色。本文將利用Matlab語(yǔ)言繪制四種不同的HSV色輪圖,感興趣的可以動(dòng)手嘗試一下2022-07-07
記錄一個(gè)C++在條件查詢時(shí)遇到的問題(推薦)
這篇文章主要介紹了記錄一個(gè)C++在條件查詢時(shí)遇到的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

