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


边栏推荐
- ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler
- Understanding of the concept of "quality"
- OpenCV get(propId) 常用的值
- 貸款五級分類
- Promise的使用场景
- Synthesize video through ffmpeg according to m3u8 file of video on the network
- Take my brother to do the project. It's cold
- 贷款五级分类
- Vscode install the remote -wsl plug-in to connect to the local WSL
- leetcode 1268. Search Suggestions System(搜索推荐系统)
猜你喜欢

Permission model DAC ACL RBAC ABAC

Maya re deployment

JUC个人简单笔记

2022 mobile crane driver special operation certificate examination question bank and online simulation examination

Two methods of QT exporting PDF files

ZUCC_编译语言原理与编译_实验04 语言与文法

权限模型 DAC ACL RBAC ABAC

根据网络上的视频的m3u8文件通过ffmpeg进行合成视频

【无标题】

2021-03-11 comp9021 class 8 notes
随机推荐
Blue screen error UNMOUNTABLE boot volume of the solution
RCNN、Fast-RCNN、Faster-RCNN介绍
Fund raising, trading and registration
Chart list Performance Optimization: minimum resource consumption in the visualization area
How to improve the customer retention rate in the operation of independent stations? Customer segmentation is very important!
The JS macro of WPS implements the separation method of picture text in the same paragraph
Ordinary token
13 -- remove invalid parentheses
The reason why the qtimer timer does not work
Export MySQL database to xxx SQL, set xxx The SQL file is imported into MySQL on the server. Project deployment.
05 Ubuntu installing mysql8
487. number of maximum consecutive 1 II ●●
[real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
Promise usage scenarios
Understanding of the concept of "quality"
ZUCC_编译语言原理与编译_实验01 语言分析与简介
Shell array
A preliminary study of IO model
05-ubuntu安装mysql8
Five level classification of loans