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


边栏推荐
- (PKCS1) RSA 公私钥 pem 文件解析
- [acnoi2022] I have done it, but I can't
- 普通token
- Shell array
- Qmenu response in pyqt
- pyQt 中 QMenu 响应
- Common CVM transcribes audio using virtual sound card
- Redis cluster data skew
- Question 4 - datepicker date selector, disabling two date selectors (start and end dates)
- 【关于运维和网工的差别,一文说透】
猜你喜欢

Question 3 - MessageBox pop-up box, modify the default background color

12-- merge two ordered linked lists

Introduction to RCNN, fast RCNN and fast RCNN

Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022

李白最经典的20首诗排行榜

Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)

2021-03-09 comp9021 class 7 Notes

Question 4 - datepicker date selector, disabling two date selectors (start and end dates)

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

5分钟,客服聊天处理技巧,炉火纯青
随机推荐
相机投影矩阵计算
ZUCC_编译语言原理与编译_大作业
Redis cluster data skew
orb slam build bug: undefined reference to symbol ‘_ZN5boost6system15system_categoryEv‘
Micro build low code online "quick registration applet" capability
List of Li Bai's 20 most classic poems
Vscode install the remote -wsl plug-in to connect to the local WSL
13 -- remove invalid parentheses
ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language
Common misconceptions in Tencent conference API - signature error_ code 200003
Review SGI STL secondary space configurator (internal storage pool) | notes for personal use
【力扣10天SQL入门】Day2
How to configure networkpolicy for nodeport in kubernetes
How to implement approval function in Tekton
QPS, TPS, concurrent users, throughput relationship
ZUCC_编译语言原理与编译_实验04 语言与文法
新技术实战,一步步用Activity Results API封装权限申请库
2021-06-24: find the length of the longest non repeating character substring in a string.
[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~
JS scroll div scroll bar to bottom