当前位置:网站首页>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
边栏推荐
- LinkedBlockingQueue源码分析-新增和删除
- QT creator add custom new file / Project Template Wizard
- new和delete的底层原理以及模板
- Reading notes 004: Wang Yangming's quotations
- 【編程題】【Scratch二級】2019.12 飛翔的小鳥
- Kubectl's handy command line tool: Oh my Zsh tips and tricks
- Qt添加资源文件,为QAction添加图标,建立信号槽函数并实现
- 动态库基本原理和使用方法,-fPIC 选项的来龙去脉
- 詹姆斯·格雷克《信息简史》读后感记录
- Qt不同类之间建立信号槽,并传递参数
猜你喜欢
35岁真就成了职业危机?不,我的技术在积累,我还越吃越香了
C# 泛型及性能比较
搭建ADG过程中复制报错 RMAN-03009 ORA-03113
Binder核心API
测试流程不完善,又遇到不积极的开发怎么办?
STM32F1與STM32CubeIDE編程實例-旋轉編碼器驅動
Automated testing: robot framework is a practical skill that 90% of people want to know
赞!idea 如何单窗口打开多个项目?
Two small problems in creating user registration interface
某马旅游网站开发(登录注册退出功能的实现)
随机推荐
哪个券商公司开户佣金低又安全,又靠谱
“一个优秀程序员可抵五个普通程序员”,差距就在这7个关键点
paddle一个由三个卷积层组成的网络完成cifar10数据集的图像分类任务
How to measure whether the product is "just needed, high frequency, pain points"
Operating system principle --- summary of interview knowledge points
QT creator add JSON based Wizard
动态库基本原理和使用方法,-fPIC 选项的来龙去脉
每日刷题记录 (十六)
Stm32f1 and stm32cubeide programming example - rotary encoder drive
The difference between -s and -d when downloading packages using NPM
Is 35 really a career crisis? No, my skills are accumulating, and the more I eat, the better
Zhou Hongqi, 52 ans, est - il encore jeune?
赞!idea 如何单窗口打开多个项目?
The difference between get and post
Solution to prompt configure: error: curses library not found when configuring and installing crosstool ng tool
Jouer sonar
[the most detailed in history] statistical description of overdue days in credit
[C language] objective questions - knowledge points
爬虫实战(八):爬表情包
RPA云电脑,让RPA开箱即用算力无限?