当前位置:网站首页>QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])
QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])
2022-06-28 09:42:00 【Come on, Doo】
This article aims to share technology
Doing it qt Under development , It is inevitable to encounter such problems . The parent class triggers an event , It needs to be sent to subclasses .
You know how to use the signal slot at this time , Binding events , Can .
Suppose a situation
A yes B Parent window of ,B yes C Parent window of ,C yes D Parent window of .
Then I want to A Send your message to D.
There is a practice at this time .
That is to say First the A Signals and B Slot binding for . stay B Triggered in the slot function of C The signal of . Finally by C Send a signal to D.
Although this approach is feasible , But it's too complicated .
The key is coming.
If you want to achieve A Can directly and D signal communication .
You need to implement a global singleton class (AppEvent). stay A Class defines the signal ( hypothesis a The signal is sSendTest), And then again appevent Common signals are defined in ( Assuming that sSendPubliceTest), take a The signal of is bound to appevent The signal of , And then D Bound to the appevent Corresponding slot function
How to write it :
connect(this,&A::sSendTest,AppEvent::getInstance(),&AppEvent::sSendPublicTest); //A Sending signal Written in A in
connect(AppEvent::getInstance(),&AppEvent::sSendPublicTest,this,&D::ReceiverTest); //D Receive and process signals , Written in D in
ok, The complete code is as follows .
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "appevent.h"
#include <QLabel>
#include <qdebug.h>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected slots:
void onBtnClicked();
signals:
void sSendTest();
};
class testwgt : public QLabel
{
Q_OBJECT
public:
explicit testwgt(QWidget *parent = nullptr)
{
connect(AppEvent::getInstance(),&AppEvent::sSendTest,this,&testwgt::ReceiverTest);
};
~testwgt(){
};
protected slots:
void ReceiverTest()
{
qDebug()<<"23456";
};
signals:
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <qdebug.h>
#include <QPushButton>
#include <QVBoxLayout>
#include "appevent.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->resize(800,600);
connect(this,&Widget::sSendTest,AppEvent::getInstance(),&AppEvent::sSendTest); // Signals can trigger signals
testwgt *wgt=new testwgt(this);
wgt->setFixedSize(100,100);
wgt->setStyleSheet("background:green");
QPushButton *btn=new QPushButton(tr("Abc"),this);
connect(btn,&QPushButton::clicked,this,&Widget::onBtnClicked);
QVBoxLayout *mainLayout=new QVBoxLayout(this);
mainLayout->addWidget(btn);
mainLayout->addWidget(wgt);
mainLayout->setMargin(0);
this->setLayout(mainLayout);
}
Widget::~Widget()
{
}
void Widget::onBtnClicked()
{
emit sSendTest();
}
appevent.h
#ifndef APPEVENT_H
#define APPEVENT_H
#include <QObject>
#include <QMutex>
#include <QSharedPointer>
class AppEvent : public QObject
{
Q_OBJECT
public:
static AppEvent *getInstance();
private:
explicit AppEvent(QObject *parent = nullptr);
AppEvent& operator =(AppEvent&) = delete;
AppEvent(AppEvent&) = delete;
private:
static AppEvent *m_appEvent_ptr;
static QMutex m_mutex;
protected slots:
signals:
void sSendTest();
};
#endif // APPEVENT_H
appevent.cpp
#include "appevent.h"
#include <qdebug.h>
AppEvent *AppEvent::m_appEvent_ptr=nullptr;
QMutex AppEvent::m_mutex;
AppEvent *AppEvent::getInstance()
{
if (nullptr == m_appEvent_ptr){
m_mutex.lock();
if (nullptr == m_appEvent_ptr){
m_appEvent_ptr = new AppEvent;
}
m_mutex.unlock();
}
return m_appEvent_ptr;
}
AppEvent::AppEvent(QObject *parent) : QObject(parent)
{
}
A picture of the day 
边栏推荐
- Explain final, finally, and finalize
- Matplotlib属性及注解
- JVM系列(2)——垃圾回收
- [ybtoj advanced training guidance] class roll call [string hash]
- TCP实战案例之即时通信、BS架构模拟
- Summary of PMP learning experience
- Wechat applet development log
- redis5.0的槽点迁移,随意玩(单机迁移集群)
- Boundary value analysis method for learning basic content of software testing (2)
- 1. Kimball dimension modeling of data warehouse: what is a fact table?
猜你喜欢

Matplotlib attribute and annotation

Prototype chain JS

Test cases for learning the basic content of software testing (II)

104. maximum depth of binary tree

PMP Exam key summary VI - chart arrangement

全链路业务追踪落地实践方案

PMP考试重点总结八——监控过程组(2)

线程的生命周期

P2394 yyy loves Chemistry I

Data visualization makes correlation analysis easier to use
随机推荐
异常
PMP考试重点总结七——监控过程组(1)
股票 停牌
DBeaver安装与使用教程(超详细安装与使用教程)
Redis5.0 slot migration, free play (single machine migration cluster)
分而治之之经典Hanoi
This article explains in detail the difficult problems and solutions faced by 3D cameras
Thread lifecycle
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
Wechat applet development log
Custom exception classes and exercises
Resource scheduling and task scheduling of spark
买卖股票费用计算
On the influence of small program on the digitalization of media industry
Dolphin scheduler uses system time
构造方法绝不是在new()之后就立马执行!!!!!
全链路业务追踪落地实践方案
1182: group photo effect
Two interview demo
标识符的命名规则和规范