当前位置:网站首页>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();
}
运行
源码下载
边栏推荐
- Is it safe for tongdaxin to buy funds?
- STM32F1与STM32CubeIDE编程实例-旋转编码器驱动
- 测试流程不完善,又遇到不积极的开发怎么办?
- 1293_FreeRTOS中xTaskResumeAll()接口的实现分析
- Using Google test in QT
- DNS 系列(一):为什么更新了 DNS 记录不生效?
- 搭建ADG过程中复制报错 RMAN-03009 ORA-03113
- Visual Studio Deployment Project - Create shortcut to deployed executable
- 【史上最详细】信贷中逾期天数统计说明
- 【推荐系统基础】正负样本采样和构造
猜你喜欢

【编程题】【Scratch二级】2019.03 绘制方形螺旋

Zhou Hongqi, 52 ans, est - il encore jeune?

QT creator add JSON based Wizard

52歲的周鴻禕,還年輕嗎?

【編程題】【Scratch二級】2019.12 飛翔的小鳥

测试流程不完善,又遇到不积极的开发怎么办?

Operating system principle --- summary of interview knowledge points

Basic learning of SQL Server -- creating databases and tables with the mouse

The underlying principles and templates of new and delete
PostGIS learning
随机推荐
C language 005: common examples
DNS 系列(一):为什么更新了 DNS 记录不生效?
Su embedded training - day4
paddle入门-使用LeNet在MNIST实现图像分类方法二
The difference between -s and -d when downloading packages using NPM
Robomaster visual tutorial (0) Introduction
【编程题】【Scratch二级】2019.03 绘制方形螺旋
SQL knowledge summary 004: Postgres terminal command summary
How does the markdown editor of CSDN input mathematical formulas--- Latex syntax summary
A brief history of information by James Gleick
STM32F1與STM32CubeIDE編程實例-旋轉編碼器驅動
[programming questions] [scratch Level 2] March 2019 garbage classification
面试题详解:用Redis实现分布式锁的血泪史
35岁真就成了职业危机?不,我的技术在积累,我还越吃越香了
QT creator add custom new file / Project Template Wizard
Emotional post station 010: things that contemporary college students should understand
快速上手使用本地测试工具postman
他们齐聚 2022 ECUG Con,只为「中国技术力量」
动态库基本原理和使用方法,-fPIC 选项的来龙去脉
Usage of limit and offset (Reprint)