当前位置:网站首页>QT establish signal slots between different classes and transfer parameters
QT establish signal slots between different classes and transfer parameters
2022-07-08 00:20:00 【Miaowei】
Qt List of articles
List of articles
Preface
Recently encountered this situation : Suppose there is 3 Classes :A,B,C
A Classes and B Passing data between classes , and C Class is responsible for controlling A Classes and B class , let me put it another way ,C Class management A Classes and B class .
Qt Establish signal slots between different classes , And pass the parameters
One .A class :TestA
1. Header file and implementation file
#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;
}
# Two 、B class :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;
}
3、 ... and C class : control A and B Class
#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();
}
Four Calling class
#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();
}
function 
Source download
边栏推荐
- The result of innovation in professional courses such as robotics (Automation)
- Usage of limit and offset (Reprint)
- Using Google test in QT
- Go learning notes (1) environment installation and hello world
- Reading notes 004: Wang Yangming's quotations
- What if the testing process is not perfect and the development is not active?
- 爬虫实战(八):爬表情包
- 52歲的周鴻禕,還年輕嗎?
- Single machine high concurrency model design
- Les mots ont été écrits, la fonction est vraiment puissante!
猜你喜欢

How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?

浪潮云溪分布式数据库 Tracing(二)—— 源码解析

Daily question brushing record (16)

华为交换机S5735S-L24T4S-QA2无法telnet远程访问

赞!idea 如何单窗口打开多个项目?

3年经验,面试测试岗20K都拿不到了吗?这么坑?

如何衡量产品是否“刚需、高频、痛点”

3 years of experience, can't you get 20K for the interview and test post? Such a hole?

Stm32f1 and stm32cubeide programming example - rotary encoder drive

Robomaster visual tutorial (1) camera
随机推荐
哪个券商公司开户佣金低又安全,又靠谱
华泰证券官方网站开户安全吗?
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
Trust orbtk development issues 2022
Single machine high concurrency model design
Smart regulation enters the market, where will meituan and other Internet service platforms go
在网页中打开展示pdf文件
【obs】官方是配置USE_GPU_PRIORITY 效果为TRUE的
C# 泛型及性能比较
Opengl3.3 mouse picking up objects
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
商品的设计等整个生命周期,都可以将其纳入到产业互联网的范畴内
Cause analysis and solution of too laggy page of [test interview questions]
服务器防御DDOS的方法,杭州高防IP段103.219.39.x
【推荐系统基础】正负样本采样和构造
攻防世界Web进阶区unserialize3题解
Introduction knowledge system of Web front-end engineers
【测试面试题】页面很卡的原因分析及解决方案
【转载】解决conda安装pytorch过慢的问题
Robomaster visual tutorial (11) summary