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


边栏推荐
- 成为IEEE学生会员
- Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022
- 2021-03-09 comp9021 class 7 Notes
- 5分钟,客服聊天处理技巧,炉火纯青
- Paper notes: multi label learning dm2l
- QT writing security video monitoring system 36 onvif continuous movement
- 单目双视三维坐标确定
- Shell array
- There was an error checking the latest version of pip
- 2021-03-16 comp9021 class 9 notes
猜你喜欢

Pat 1157: school anniversary

Permission model DAC ACL RBAC ABAC

ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler

中国芯片独角兽公司

2022 tea artist (intermediate) work license question bank and online simulation examination
![Fundamentals of 3D mathematics [17] inverse square theorem](/img/59/bef931d96883288766fc94e38e0ace.png)
Fundamentals of 3D mathematics [17] inverse square theorem

Two methods of QT exporting PDF files

Maya re deployment

ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis

12-- merge two ordered linked lists
随机推荐
利用ngrok做内网穿透
饼状统计图,带有标注线,都可以自行设定其多种参数选项
Common CVM transcribes audio using virtual sound card
Qt 中发送自定义事件
PHP代码加密的几种方案
App Startup
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
ZUCC_编译语言原理与编译_实验06 07 语法分析 LL 分析
Common date formatter and QT method for obtaining current time
LabVIEW finds prime numbers in an array of n elements
The JS macro of WPS implements the separation method of picture text in the same paragraph
中国芯片独角兽公司
[untitled]
[real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
Blue screen error UNMOUNTABLE boot volume of the solution
【力扣10天SQL入门】Day3
Common misconceptions in Tencent conference API - signature error_ code 200003
数据库,查询本月借出书的数量,如果高于10本时,显示“本月借出书大于10本”,否则显示“本月借出书小于10本”
Qmenu response in pyqt
How to implement approval function in Tekton