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


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();
}

分析:彩虹

多繼承方式可直接對ui界面上的控件或函數進行操作,代碼編寫更簡潔;

而是用單繼承方式,在操作ui頁面上的控件時需加上ui對象前綴,編寫代碼較為麻煩。

但,對於程序中所需ui頁面較多時,使用單繼承法則要靈活的多。。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM