当前位置:网站首页>Qt 中发送自定义事件
Qt 中发送自定义事件
2022-06-24 07:03:00 【litanyuan】
背景
事件是用户和应用软件间产生的一个交互操作,由用户操作产生或者系统内部产生,通过事件循环对事件进行处理,事件也可以用来在对象间进行信息交互。
Qt 中的事件都继承自 QEvent 类,通过继承 QEvent 类可以实现自定义事件类型。
子类化 QEvent
①..h文件
#pragma once
#include <QEvent>
#include <QString>
class CustomEvent : public QEvent
{
public:
CustomEvent(const QString & m_str);
~CustomEvent();
public:
static int type;//自定义的事件类型
private:
QString text;//事件对象携带的数据
public:
QString getEventString() const {
return text; }
};
②..cpp文件
#include "CustomEvent.h"
int CustomEvent::type = QEvent::registerEventType();//注册自定义类型
CustomEvent::CustomEvent(const QString & m_str)
: QEvent(QEvent::Type(type)), text(m_str)
{
}
CustomEvent::~CustomEvent()
{
}
子类化 QObject
①..h文件
#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;//事件分发器
private:
void testFunc(const QString & str);
};
②..cpp文件
#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;
}
发布自定义事件
①.sendEvent
事件发送后会阻塞,直到事件处理完成;可以发送栈事件对象或者堆事件对象。
②.postEvent
事件发送后立即返回不会阻塞,事件发送到事件队列中等待处理;仅可以发送堆事件对象,事件处理后由 Qt 自己销毁。
③.代码示例
QtClassDemo * demo = new QtClassDemo(this);
demo->setObjectName("demo");
for (int i = 0; i < 5; i++)
{
qApp->postEvent(demo, new CustomEvent("hello"));
}


边栏推荐
- Qt源码分析--QObject(2)
- QPS, TPS, concurrent users, throughput relationship
- [micro services ~nacos] Nacos service providers and service consumers
- ZUCC_ Principles of compiling language and compilation_ Experiment 01 language analysis and introduction
- Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022
- New technology practice, encapsulating the permission application library step by step with the activity results API
- Introduction to NC machine tool programming [G-code]
- The article takes you to understand the security of Windows operating system and protect your computer from infringement
- Review SGI STL secondary space configurator (internal storage pool) | notes for personal use
- "Adobe international certification" Photoshop software, about drawing tutorial?
猜你喜欢

List of Li Bai's 20 most classic poems

ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar

MATLAB Camera Calibrator相机标定

Maya re deployment
![Fundamentals of 3D mathematics [17] inverse square theorem](/img/59/bef931d96883288766fc94e38e0ace.png)
Fundamentals of 3D mathematics [17] inverse square theorem

Qt导出PDF文件的两种方法

Vscode install the remote -wsl plug-in to connect to the local WSL

OpenCV to realize the basic transformation of image

io模型初探

权限模型 DAC ACL RBAC ABAC
随机推荐
New technology practice, encapsulating the permission application library step by step with the activity results API
win11在cmder中使用vim查看内容的时候空白
2021-06-24: find the length of the longest non repeating character substring in a string.
常用日期格式符与Qt获取当前时间的办法
Tencent conference API - get rest API & webhook application docking information
2021-03-11 comp9021 class 8 notes
Redis cluster data skew
Variable declaration and some special variables in shell
How to implement approval function in Tekton
Promise的使用场景
Understanding of the concept of "quality"
[untitled]
将mysql的数据库导出xxx.sql,将xxx.sql文件导入到服务器的mysql中。项目部署。
ZUCC_ Principles of compiling language and compilation_ Big job
ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler
JS to get the last element of the array
Markdown to realize text link jump
[xinliu-s6 new model +sa 3-star Xinghai] the new two-way server of the third generation chip was launched and the product was updated~
Markdown 实现文内链接跳转
2022 mobile crane driver special operation certificate examination question bank and online simulation examination