当前位置:网站首页>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();
}
运行
源码下载
边栏推荐
- QT creator add JSON based Wizard
- Is Zhou Hongyi, 52, still young?
- Fully automated processing of monthly card shortage data and output of card shortage personnel information
- Two small problems in creating user registration interface
- [programming questions] [scratch Level 2] March 2019 garbage classification
- 备库一直有延迟,查看mrp为wait_for_log,重启mrp后为apply_log但过一会又wait_for_log
- STM32F1与STM32CubeIDE编程实例-旋转编码器驱动
- 去了字节跳动,才知道年薪 40w 的测试工程师有这么多?
- 华为交换机S5735S-L24T4S-QA2无法telnet远程访问
- 52歲的周鴻禕,還年輕嗎?
猜你喜欢
Basic learning of SQL Server -- creating databases and tables with code
[programming problem] [scratch Level 2] December 2019 flying birds
腾讯安全发布《BOT管理白皮书》|解读BOT攻击,探索防护之道
Using Google test in QT
SQL connection problem after downloading (2)
Opengl3.3 mouse picking up objects
【编程题】【Scratch二级】2019.12 绘制十个正方形
DNS 系列(一):为什么更新了 DNS 记录不生效?
STM32F1與STM32CubeIDE編程實例-旋轉編碼器驅動
Notice on organizing the second round of the Southwest Division (Sichuan) of the 2021-2022 National Youth electronic information intelligent innovation competition
随机推荐
Introduction knowledge system of Web front-end engineers
Operating system principle --- summary of interview knowledge points
什么是负载均衡?DNS如何实现负载均衡?
某马旅游网站开发(登录注册退出功能的实现)
When creating body middleware, express Is there any difference between setting extended to true and false in urlencoded?
Reptile practice (VIII): reptile expression pack
ROS从入门到精通(九) 可视化仿真初体验之TurtleBot3
Jouer sonar
【史上最详细】信贷中逾期天数统计说明
腾讯安全发布《BOT管理白皮书》|解读BOT攻击,探索防护之道
Robomaster visual tutorial (1) camera
Binder核心API
Use filters to count URL request time
爬虫实战(八):爬表情包
Emotional post station 010: things that contemporary college students should understand
Development of a horse tourism website (realization of login, registration and exit function)
80%的人答错,苹果logo上的叶子到底朝左还是朝右?
How to learn a new technology (programming language)
RPA云电脑,让RPA开箱即用算力无限?
商品的设计等整个生命周期,都可以将其纳入到产业互联网的范畴内