QT (D): Events and Event Filters

Original Address: https://houkaifa.com/2019/02/22/LearningNotes-QT-04/

Outline

This introduction QT events and event filters, and make a simple classic spoof applet.
Environment: Win10 + QT Creator4.4.1 + QT5.9.2.
Implementing content: a point less than the visible button.

Here Insert Picture Description


A, QT events and event filters

Event is a good thing, with the event, we can do certain things in certain situations, without being concerned about when this happens.

In QT (a): signals and slots among wrote this passage:

QtGui signals and slots are in development representation of the event, i.e., the signal is the trigger event, the groove is an event handler that is triggered by the signal.

In order to facilitate the rapid build familiarity and make analogies, let it now appears, in fact, there is a big problem.

Signal / slot and Qt is hardly expressed in the form of an event, it can be said to be at most a specific example of the event, wherein the special signal / slot system is actively controllable program may be initiated by an event. In addition to that, there are mouse, keyboard and many other messaging devices, will be relatively passive trigger a variety of events such as.

For example, the left mouse button is pressed, the left mouse button is released, the keyboard and the mouse button down, moves the like, will have a corresponding event. So how these events are spread among the QT? How should listen for specific events?

Event to

The complicated events continuously input into our program, without exception, take the same road:

Here Insert Picture Description

First event into the event filter them, and then release the event posted to the event dispatcher through final distribution event will be distributed to the corresponding event handler to implement the relevant things.

Event Listeners

If you want to listen to an object of a number of events, you can start from the event filter can also start from the event dispatcher.

The difference is that if starting from the event filter, is listening events all events will be filtered object received installed, and start from the event dispatcher, then not be able to get all the events, because some events may in the event and it is intercepted by the filter to release the rejected. The following talk about an event filter.

To filter event on an object, you first need to install an event filter on it:

void QObject::installEventFilter(QObject *filterObj)

Which is described in the official manual as follows:

Installs an event filter filterObj on this object. For example:

monitoredObj->installEventFilter(filterObj)

An event filter is an object that receives all events that are sent to this object. The filter can either stop the event or forward it to this object. The event filter filterObj receives events via its eventFilter() function. The eventFilter() function must return true if the event should be filtered, (i.e. stopped); otherwise it must return false.
If multiple event filters are installed on a single object, the filter that was installed last is activated first.

As can be seen, in the event the filter, we can not only get all the events received event, the event can not be specified returns true released by the filter function, and further such that the corresponding handler indirectly failure.

eventFilter () and event () function is defined in the virtual function QObject class, for filtering and distributing the event. After installation event filter, we only need a corresponding class which is derived class QObject and rewriting it, eventFilter () function is declared as follows:

bool QObject::eventFilter(QObject *watched, QEvent *event)

Which is described in the official manual as follows:

Filters events if this object has been installed as an event filter for the watched object.
In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.

It can be seen, the parameter is watched the object's event filter is mounted , is event parameters of the object received event .

Where the parameter event type is Qt QEvent among all event types of the base class, which includes not only the type of event, also carry the relevant parameters of the event, so the event we can will need when filtering events disposed of.

When the main event loop of Qt (QCoreApplication :: exec ()) to extract the native window system events from the event queue, it will be converted to qEvents, and transmits the converted events to QObjects, and further fed to the object among the installed filters.

Here Insert Picture Description

Second, the small example: Simple small program spoof

Target effect: on screen display label: Do you love to learn it? And two buttons "love" and "do not love", which "do not love" refers to the mouse button is not allowed.

Build Qt Gui project and complete a simple interface to build:

Here Insert Picture Description

"Not love" button (pushButton_2) event filter installed in the main interface class constructor:

Here Insert Picture Description

In the main interface class header file declare an event filter function:

Here Insert Picture Description

Implement an event filter function

Here Insert Picture Description

Which QEvent :: Enter Qt among representatives of the mouse enter event, for example, a button here, the moment when the mouse button is moved, the event is triggered.
QEvent :: Enter data type Type, on behalf of the event type is defined in the enumeration QEvent class, which QEvent :: Enter defined in this enumeration:
Here Insert Picture Description
other type of event can be in the official manual of the class QEvent easy to find.

F5 debugging effect:

Here Insert Picture Description

Published 26 original articles · won praise 24 · views 30000 +

Guess you like

Origin blog.csdn.net/iSunwish/article/details/87886549