当前位置:网站首页>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();
}
运行
源码下载
边栏推荐
- ROS from entry to mastery (IX) initial experience of visual simulation: turtlebot3
- The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
- RPA cloud computer, let RPA out of the box with unlimited computing power?
- Stm32f1 and stm32cubeide programming example - rotary encoder drive
- 【测试面试题】页面很卡的原因分析及解决方案
- Robomaster visual tutorial (10) target prediction
- 测试流程不完善,又遇到不积极的开发怎么办?
- Binary sort tree [BST] - create, find, delete, output
- 用語雀寫文章了,功能真心强大!
- 关于组织2021-2022全国青少年电子信息智能创新大赛西南赛区(四川)复赛的通知
猜你喜欢
ROS从入门到精通(九) 可视化仿真初体验之TurtleBot3
[programming problem] [scratch Level 2] 2019.09 make bat Challenge Game
Opengl3.3 mouse picking up objects
Jouer sonar
[programming problem] [scratch Level 2] draw ten squares in December 2019
【推荐系统基础】正负样本采样和构造
Les mots ont été écrits, la fonction est vraiment puissante!
某马旅游网站开发(登录注册退出功能的实现)
【测试面试题】页面很卡的原因分析及解决方案
Installation and configuration of sublime Text3
随机推荐
51与蓝牙模块通讯,51驱动蓝牙APP点灯
【編程題】【Scratch二級】2019.12 飛翔的小鳥
Using Google test in QT
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe
Zhou Hongqi, 52 ans, est - il encore jeune?
1293_FreeRTOS中xTaskResumeAll()接口的实现分析
[研发人员必备]paddle 如何制作自己的数据集,并显示。
在网页中打开展示pdf文件
Use filters to count URL request time
【编程题】【Scratch二级】2019.12 飞翔的小鸟
Solution to the problem of unserialize3 in the advanced web area of the attack and defense world
52歲的周鴻禕,還年輕嗎?
Development of a horse tourism website (optimization of servlet)
爬虫实战(八):爬表情包
2022-07-07:原本数组中都是大于0、小于等于k的数字,是一个单调不减的数组, 其中可能有相等的数字,总体趋势是递增的。 但是其中有些位置的数被替换成了0,我们需要求出所有的把0替换的方案数量:
C# 泛型及性能比较
Problems faced when connecting to sqlserver after downloading (I)
[the most detailed in history] statistical description of overdue days in credit
SQL connection problem after downloading (2)