当前位置:网站首页>Send custom events in QT
Send custom events in QT
2022-06-24 08:41:00 【litanyuan】
background
An event is an interaction between a user and an application , Generated by user operation or within the system , Process events through event loops , Events can also be used to exchange information between objects .
Qt The events in are inherited from QEvent class , By inheritance QEvent Class can implement custom event types .
Subclass QEvent
①..h file
#pragma once
#include <QEvent>
#include <QString>
class CustomEvent : public QEvent
{
public:
CustomEvent(const QString & m_str);
~CustomEvent();
public:
static int type;// Custom event types
private:
QString text;// Data carried by the event object
public:
QString getEventString() const {
return text; }
};
②..cpp file
#include "CustomEvent.h"
int CustomEvent::type = QEvent::registerEventType();// Register custom types
CustomEvent::CustomEvent(const QString & m_str)
: QEvent(QEvent::Type(type)), text(m_str)
{
}
CustomEvent::~CustomEvent()
{
}
Subclass QObject
①..h file
#pragma once
#include <QObject>
#include "QDebug"
#include "qcoreevent.h"
class QtClassDemo : public QObject
{
Q_OBJECT
public:
QtClassDemo(QObject *parent) : QObject(parent) {
};
~QtClassDemo(){
qDebug() << " ~QtClassDemo" ;
};
protected:
bool event(QEvent * event) override;// Event distributor
private:
void testFunc(const QString & str);
};
②..cpp file
#include "QtClassDemo.h"
#include "CustomEvent.h"
bool QtClassDemo::event(QEvent * event)
{
qDebug() << event->type();
if (event->type() == CustomEvent::type)
{
auto customEvent = static_cast<CustomEvent*>(event);
testFunc(customEvent->getEventString());
return true;
}
return QObject::event(event);
}
void QtClassDemo::testFunc(const QString & str)
{
qDebug() << this->objectName() << str;
}
Publish custom events
①.sendEvent
After the event is sent, it will block , Until the event is processed ; You can send stack event objects or heap event objects .
②.postEvent
Return immediately after the event is sent without blocking , Events are sent to the event queue for processing ; Only heap event objects can be sent , After the event is handled by Qt Destroy yourself .
③. Code example
QtClassDemo * demo = new QtClassDemo(this);
demo->setObjectName("demo");
for (int i = 0; i < 5; i++)
{
qApp->postEvent(demo, new CustomEvent("hello"));
}


边栏推荐
- OpenCV to realize the basic transformation of image
- 2021-03-11 comp9021 class 8 notes
- ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
- Cloudbase database migration scheme
- AUTO PWN
- 常用日期格式符与Qt获取当前时间的办法
- 【力扣10天SQL入门】Day2
- PHP代码加密的几种方案
- [graduation season] Hello stranger, this is a pink letter
- Matlab求解线性方程组Ax=b
猜你喜欢

ZUCC_编译语言原理与编译_实验04 语言与文法
![[micro services ~nacos] Nacos service providers and service consumers](/img/b7/47ecd6979ccfeb270261681d6130be.png)
[micro services ~nacos] Nacos service providers and service consumers

uniapp 热更新后台管理

OpenCV to realize the basic transformation of image

Vscode install the remote -wsl plug-in to connect to the local WSL
![[untitled]](/img/94/792e8363dbfe67770e93b0dcdc8e72.png)
[untitled]

Introduction to RCNN, fast RCNN and fast RCNN

2021-03-16 comp9021 class 9 notes

分布式 | 如何与 DBLE 进行“秘密通话”

Markdown to realize text link jump
随机推荐
Take my brother to do the project. It's cold
Rescue system -- the application of read-write separation
提高INSERT速度
[micro services ~nacos] Nacos service providers and service consumers
App Startup
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
Shell basic operators -- relational operators
[acnoi2022] I have done it, but I can't
[untitled]
Easycvr invokes the interface parameter acquisition method and precautions of device video recording on the page
(PKCS1) RSA 公私钥 pem 文件解析
WPS的JS宏实现图片正文在同一段落的分离方法
How to implement approval function in Tekton
数据库,查询本月借出书的数量,如果高于10本时,显示“本月借出书大于10本”,否则显示“本月借出书小于10本”
Glusterfs replacement failure brick
Variable declaration and some special variables in shell
成为IEEE学生会员
JS scroll div scroll bar to bottom
dataX使用指南
Maya re deployment