Qt中的事件系统

The Event System
In Qt, events are objects, derived from the abstract QEvent class, that represent things that have happened either within an application or as a result of outside activity that the application needs to know about. Events can be received and handled by any instance of a QObject subclass, but they are especially relevant to widgets. This document describes how events are delivered and handled in a typical application.

事件是应用程序内部事情和应用程序需要知道的外部动作的通称。任何一个QObject的子类实例都可以接收和处理事件。    

How Events are Delivered(事件如何传递)
When an event occurs, Qt creates an event object to represent it by constructing an instance of the appropriate QEvent subclass, and delivers it to a particular instance of QObject (or one of its subclasses) by calling its event() function.This function does not handle the event itself; based on the type of event delivered, it calls an event handler for that specific type of event, and sends a response based on whether the event was accepted or ignored.Some events, such as QMouseEvent and QKeyEvent, come from the window system; some, such as QTimerEvent, come from other sources; some come from the application itself.

当事件发生,Qt会创建一个事件对象来表示它,通过构造一个合适的QEvent子类实例,然后通过调用event()函数传递该事件到QObjcet的子类实例中。该函数不会处理事件本身,它可以调用事件处理,然后发送是否事件被接受或忽略的应答。   

Event Handlers
The normal way for an event to be delivered is by calling a virtual function. For example, QPaintEvent is delivered by calling QWidget::paintEvent(). This virtual function is responsible for reacting appropriately, normally by repainting the widget. If you do not perform all the necessary work in your implementation of the virtual function, you may need to call the base class’s implementation.Note that QWidget::event() is still called for all of the cases not handled, and that the return value indicates whether an event was dealt with; a true value prevents the event from being sent on to other objects.

事件传递的常规方式是调用虚函数。如果不能满足需求则需要重载该函数。注意,事件没有被处理仍然会调用event(),返回值为真则表示事件被传递到了另一个对象。

Event Filters
Sometimes an object needs to look at, and possibly intercept, the events that are delivered to another object. For example, dialogs commonly want to filter key presses for some widgets; for example, to modify Return-key handling.
The QObject::installEventFilter() function enables this by setting up an event filter, causing a nominated filter object to receive the events for a target object in its QObject::eventFilter() function. An event filter gets to process events before the target object does, allowing it to inspect and discard the events as required. An existing event filter can be removed using the QObject::removeEventFilter() function.
When the filter object’s eventFilter() implementation is called, it can accept or reject the event, and allow or deny further processing of the event. If all the event filters allow further processing of an event (by each returning false), the event is sent to the target object itself. If one of them stops processing (by returning true), the target and any later event filters do not get to see the event at all。

有时候,一个事件被传递给另一个对象时可能需要查看或拦截。QObject::installEventFilter()函数可以创建一个事件过滤器。在QObject::eventFilter()
函数中

Qt监听系统唤醒事件通常涉及到处理系统休眠后的唤醒事件,这个事件可以通过Qt事件处理机制来捕获。具体步骤如下: 1. 使用`QEvent`类来处理系统事件。在Qt,所有的事件都可以通过继承`QEvent`类来定义,然后通过事件过滤器来拦截。 2. 重写你的类的`event()`方法。这个方法是处理事件的入口,通过在这个方法判断事件类型,可以实现对特定事件的处理。 3. 为了监听系统唤醒事件,可以使用`QSystemSemaphore`和`QSocketNotifier`,这两个类可以帮助你检测系统唤醒信号。`QSystemSemaphore`可以在系统信号发生时发出通知,而`QSocketNotifier`可以监视文件描述符来处理来自系统的唤醒通知。 4. 你需要实现一个事件循环的机制来监听这些唤醒事件。通常是在你的主窗口或者应用程序类设置事件监听器。 5. 另一种方法是使用操作系统的API来获取系统唤醒事件。例如,在Windows系统,可以通过设置系统事件钩子(SetWindowsHookEx)来监听系统唤醒事件,然后通过`QWinEventHook`类来封装这个钩子。 示例代码片段(不完整)可能如下所示: ```cpp #include <QApplication> #include <QWidget> #include <QEvent> #include <QSystemSemaphore> #include <QSocketNotifier> #include <QThread> // 其他必要的头文件 class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) { // 初始化代码 // 设置事件监听器等 } protected: bool event(QEvent *event) override { if (event->type() == QEvent::KeyPress) { // 处理键盘事件 } return QWidget::event(event); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget widget; widget.show(); return app.exec(); } #include "main.moc" ``` 在这个例子,我们重写了`event()`方法来处理事件。对于系统唤醒事件,你可能需要根据操作系统的API来实现具体的处理逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值