Qt中的事件系统

本文介绍了Qt中的事件系统,包括事件的创建、传递和处理过程。详细讲述了如何通过虚函数实现事件处理,以及如何使用事件过滤器拦截特定事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
函数中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值