当前位置:网站首页>Qt不同类之间建立信号槽,并传递参数
Qt不同类之间建立信号槽,并传递参数
2022-07-07 22:20:00 【妙为】
Qt系列文章目录
前言
最近遇到这种情况:假设有3个类:A,B,C
A类和B类之间相互传递数据,而C类负责控制A类和B类,换句话说,C类管理着A类和B类。
Qt不同类之间建立信号槽,并传递参数
一.A类:TestA
1.头文件和实现文件
#ifndef TESTA_H
#define TESTA_H
#include <QObject>
class TestA : public QObject
{
Q_OBJECT
public:
explicit TestA(QObject *parent = nullptr);
void emitDataFromTestA();
signals:
void sendDataFromTestA(const char* data);
public slots:
void recvDataAtTestA(const char* data);
};
#endif // TESTA_H
```cpp
#include "TestA.h"
#include <QDebug>
TestA::TestA(QObject *parent) : QObject(parent)
{
}
void TestA::emitDataFromTestA()
{
const char* name = "Data from testA";
emit sendDataFromTestA(name);
}
void TestA::recvDataAtTestA(const char *data)
{
// QString print = data;
// qDebug() << print.toLatin1();
qDebug() << "TestA class recive data:" << data;
}
# 二、B类:TestB
```cpp
#ifndef TESTB_H
#define TESTB_H
#include <QObject>
class TestB : public QObject
{
Q_OBJECT
public:
explicit TestB(QObject *parent = nullptr);
void emitDataFromTestB();
signals:
void sendDataFromTestB(const char* data);
public slots:
void recvDataAtTestB(const char* data);
};
#endif // TESTB_H
#include "TestB.h"
#include <QDebug>
TestB::TestB(QObject *parent) : QObject(parent)
{
}
void TestB::emitDataFromTestB()
{
const char* name = "Data from testB";
emit sendDataFromTestB(name);
}
void TestB::recvDataAtTestB(const char *data)
{
qDebug() << "TestB class recive data:" << data;
}
三 C类:控制A和B的类
#ifndef CONTROL_H
#define CONTROL_H
#include <QObject>
#include "TestA.h"
#include "TestB.h"
class Control : public QObject
{
Q_OBJECT
public:
explicit Control(QObject *parent = nullptr);
~Control();
public:
TestA* m_pTestA;
TestB* m_pTestB;
void controlSendData();
signals:
};
#endif // CONTROL_H
#include "Control.h"
Control::Control(QObject *parent) : QObject(parent)
{
m_pTestA = new TestA;
m_pTestB = new TestB;
QObject::connect(m_pTestA, &TestA::sendDataFromTestA, m_pTestB, &TestB::recvDataAtTestB);
QObject::connect(m_pTestB, &TestB::sendDataFromTestB, m_pTestA, &TestA::recvDataAtTestA);
}
Control::~Control()
{
if(m_pTestA)
{
delete m_pTestA;
m_pTestA = nullptr;
}
if(m_pTestB)
{
delete m_pTestB;
m_pTestB = nullptr;
}
}
void Control::controlSendData()
{
m_pTestA->emitDataFromTestA();
m_pTestB->emitDataFromTestB();
}
四 调用类
#include "MainWindow.h"
#include "TestA.h"
#include "TestB.h"
#include "Control.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// TestA testA;
// TestB testB;
// a.connect(&testA, &TestA::sendDataFromTestA, &testB, &TestB::recvDataAtTestB);
// testA.emitDataFromTestA();
// a.connect(&testB, &TestB::sendDataFromTestB, &testA, &TestA::recvDataAtTestA);
// testB.emitDataFromTestB();
Control control;
control.controlSendData();
MainWindow w;
w.show();
return a.exec();
}
运行
源码下载
边栏推荐
- [研发人员必备]paddle 如何制作自己的数据集,并显示。
- Binary sort tree [BST] - create, find, delete, output
- 从服务器到云托管,到底经历了什么?
- QT and OpenGL: loading 3D models using the open asset import library (assimp) - Part 2
- 玩转Sonar
- Install sqlserver2019
- 第四期SFO销毁,Starfish OS如何对SFO价值赋能?
- 去了字节跳动,才知道年薪 40w 的测试工程师有这么多?
- Su embedded training - Day3
- 搭建ADG过程中复制报错 RMAN-03009 ORA-03113
猜你喜欢
Introduction to programming hardware
When creating body middleware, express Is there any difference between setting extended to true and false in urlencoded?
智慧监管入场,美团等互联网服务平台何去何从
Is 35 really a career crisis? No, my skills are accumulating, and the more I eat, the better
[研发人员必备]paddle 如何制作自己的数据集,并显示。
【测试面试题】页面很卡的原因分析及解决方案
RPA云电脑,让RPA开箱即用算力无限?
The result of innovation in professional courses such as robotics (Automation)
[the most detailed in history] statistical description of overdue days in credit
某马旅游网站开发(登录注册退出功能的实现)
随机推荐
攻防世界Web进阶区unserialize3题解
Cmake learning notes (1) compile single source programs with cmake
Zhou Hongqi, 52 ans, est - il encore jeune?
Two small problems in creating user registration interface
Usage of limit and offset (Reprint)
SQL knowledge summary 004: Postgres terminal command summary
QT creator add JSON based Wizard
limit 与offset的用法(转载)
Handwriting a simulated reentrantlock
Introduction to programming hardware
How to learn a new technology (programming language)
Daily question brushing record (16)
[the most detailed in history] statistical description of overdue days in credit
他们齐聚 2022 ECUG Con,只为「中国技术力量」
【编程题】【Scratch二级】2019.12 绘制十个正方形
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
Reptile practice (VIII): reptile expression pack
哪个券商公司开户佣金低又安全,又靠谱
测试流程不完善,又遇到不积极的开发怎么办?
2022-07-07:原本数组中都是大于0、小于等于k的数字,是一个单调不减的数组, 其中可能有相等的数字,总体趋势是递增的。 但是其中有些位置的数被替换成了0,我们需要求出所有的把0替换的方案数量: