当前位置:网站首页>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"));
}


边栏推荐
- Introduction to RCNN, fast RCNN and fast RCNN
- Easynvr and easyrtc platforms use go language to manage projects. Summary of the use of govendor and gomod
- Shell pass parameters
- Promise的使用场景
- Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)
- Promise usage scenarios
- [untitled]
- 更改SSH端口号
- 日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
- Glusterfs replacement failure brick
猜你喜欢

Qt 中发送自定义事件

Pat 1157: school anniversary

A preliminary study of IO model

Two methods of QT exporting PDF files

【关于运维和网工的差别,一文说透】

Base64编码详解及其变种(解决加号在URL变空格问题)

2021-03-04 comp9021 class 6 notes

2021-03-09 comp9021 class 7 Notes

ZUCC_编译语言原理与编译_实验03 编译器入门

The article takes you to understand the security of Windows operating system and protect your computer from infringement
随机推荐
Introduction to NC machine tool programming [G-code]
5分钟,客服聊天处理技巧,炉火纯青
Shell basic operator -- arithmetic operator
String转Base64
Final review and key points of software process and project management
Qmenu response in pyqt
ZUCC_编译语言原理与编译_实验06 07 语法分析 LL 分析
App Startup
Variable declaration and some special variables in shell
【力扣10天SQL入门】Day3
Detailed explanation of etcd backup and recovery principle and actual record of stepping on the pit
ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
Application of tidb in Netease games
【微服务~Nacos】Nacos服务提供者和服务消费者
Two methods of QT exporting PDF files
(pkcs1) RSA public private key PEM file parsing
JUC personal simple notes
DHCP, TFTP Foundation
Promise的使用場景
什么是SRE?一文详解SRE运维体系