当前位置:网站首页>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();
}
运行
源码下载
边栏推荐
- 华为交换机S5735S-L24T4S-QA2无法telnet远程访问
- 哪个券商公司开户佣金低又安全,又靠谱
- Development of a horse tourism website (realization of login, registration and exit function)
- paddle一个由三个卷积层组成的网络完成cifar10数据集的图像分类任务
- 玩转Sonar
- Solution to the problem of unserialize3 in the advanced web area of the attack and defense world
- 商品的设计等整个生命周期,都可以将其纳入到产业互联网的范畴内
- Go learning notes (1) environment installation and hello world
- Reptile practice (VIII): reptile expression pack
- Tools for debugging makefiles - tool for debugging makefiles
猜你喜欢
Install sqlserver2019
Operating system principle --- summary of interview knowledge points
Zhou Hongqi, 52 ans, est - il encore jeune?
每日刷题记录 (十六)
关于组织2021-2022全国青少年电子信息智能创新大赛西南赛区(四川)复赛的通知
【史上最详细】信贷中逾期天数统计说明
[basis of recommendation system] sampling and construction of positive and negative samples
[研发人员必备]paddle 如何制作自己的数据集,并显示。
深潜Kotlin协程(二十二):Flow的处理
“一个优秀程序员可抵五个普通程序员”,差距就在这7个关键点
随机推荐
Using Google test in QT
Automated testing: robot framework is a practical skill that 90% of people want to know
35岁真就成了职业危机?不,我的技术在积累,我还越吃越香了
Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
52岁的周鸿祎,还年轻吗?
Single machine high concurrency model design
How to measure whether the product is "just needed, high frequency, pain points"
Fully automated processing of monthly card shortage data and output of card shortage personnel information
How to add automatic sorting titles in typora software?
【编程题】【Scratch二级】2019.03 绘制方形螺旋
Go learning notes (1) environment installation and hello world
Is it safe for tongdaxin to buy funds?
【编程题】【Scratch二级】2019.12 飞翔的小鸟
SQL connection problem after downloading (2)
[programming problem] [scratch Level 2] draw ten squares in December 2019
去了字节跳动,才知道年薪 40w 的测试工程师有这么多?
【编程题】【Scratch二级】2019.09 绘制雪花图案
Smart regulation enters the market, where will meituan and other Internet service platforms go
[programming problem] [scratch Level 2] March 2019 draw a square spiral