Qt之ui在程序中的使用-多继承法介绍
更新时间: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();
}
分析:
多继承方式可直接对ui界面上的控件或函数进行操作,代码编写更简洁;
而是用单继承方式,在操作ui页面上的控件时需加上ui对象前缀,编写代码较为麻烦。
但,对于程序中所需ui页面较多时,使用单继承法则要灵活的多。。
复制代码 代码如下:
#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页面较多时,使用单继承法则要灵活的多。。
相关文章
C++ std::initializer_list 实现原理解析及遇到问题
这篇文章主要介绍了C++ std::initializer_list 实现原理勘误,本文通过源码解析给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-02-02
最新评论