• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Qt之ui在程序中的使用-多继承法介绍

qt 搞代码 4年前 (2022-01-06) 19次浏览 已收录 0个评论

本文将介绍Qt之ui在程序中的使用-多继承法,需要的朋友可以参考

thirdDialog.h

代码如下:
#ifndef THIRDDIALOG来源gao.dai.ma.com搞@代*码网_H
#define THIRDDIALOG_H
#include
#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
#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页面较多时,使用单继承法则要灵活的多。。

以上就是Qt之ui在程序中的使用-多继承法介绍的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Qt之ui在程序中的使用-多继承法介绍
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址