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


边栏推荐
- 【力扣10天SQL入门】Day3
- Easycvr invokes the interface parameter acquisition method and precautions of device video recording on the page
- Use cpulimit to free up your CPU
- Catégorie de prêt 5
- Which is the first poem of Tang Dynasty?
- MAYA重新拓布
- 普通token
- Take my brother to do the project. It's cold
- "Adobe international certification" Photoshop software, about drawing tutorial?
- Five level classification of loans
猜你喜欢
随机推荐
os. path. Pits encountered during the use of join()
ZUCC_编译语言原理与编译_实验04 语言与文法
分布式 | 如何与 DBLE 进行“秘密通话”
js中通过key查找和更新对象中指定值的方法
小黑ai4code代码baseline啃食1
JS merge multiple objects and remove duplicates
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
JUC个人简单笔记
Longhorn installation and use
2021-03-16 comp9021 class 9 notes
Application of tidb in Netease games
独立站运营中如何提升客户留存率?客户细分很重要!
RCNN、Fast-RCNN、Faster-RCNN介绍
ZUCC_编译语言原理与编译_大作业
String转Base64
WPS的JS宏实现图片正文在同一段落的分离方法
OpenCV get(propId) 常用的值
QT writing security video monitoring system 36 onvif continuous movement
[acnoi2022] I have done it, but I can't
PAT 1157:校庆



![[micro services ~nacos] Nacos service providers and service consumers](/img/b7/47ecd6979ccfeb270261681d6130be.png)

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


