- Game Programming using Qt 5 Beginner's Guide
- Pavel Strakhov Witold Wysota Lorenz Haas
- 483字
- 2021-08-27 18:31:07
Events
Any GUI application needs to react to the input events. We are already familiar with the signals and slots mechanism in QObject-based classes. However, QObject is not exactly a lightweight class. Signals and slots are powerful and convenient for connecting parts of the application, but invoking a signal for processing each keyboard press or mouse move will be too inefficient. To process such events, Qt has a special system that uses the QEvent class.
The dispatcher of the events is the event loop. Almost any Qt application uses the main event loop that is started by calling QCoreApplication::exec at the end of the main() function. While the application is running, the control flow is either in your code (that is, in the implementation of any function in the project) or in the event loop. When the operating system or a component of the application asks the event loop to process an event, it determines the receiver and calls a virtual function that corresponds to the event type. A QEvent object containing information about the event is passed to that function. The virtual function has a choice to accept or ignore the event. If the event was not accepted, the event is propagated to the parent object in the hierarchy (for example, from a widget to its parent widget, and from a graphics item to the parent item). You can subclass a Qt class and reimplement a virtual function to add custom events processing.
The following table shows the most useful events:
Event types | Description |
QEvent::KeyPress, QEvent::KeyRelease | A keyboard button was pressed or released. |
QEvent::MouseButtonPress, QEvent::MouseButtonRelease, QEvent::MouseButtonDblClick | The mouse buttons were pressed or released. |
QEvent::Wheel | The mouse wheel was rolled. |
QEvent::Enter | The mouse cursor entered the object's boundaries. |
QEvent::MouseMove | The mouse cursor was moved. |
QEvent::Leave | The mouse cursor left the object's boundaries. |
QEvent::Resize | The widget was resized (for example, because the user resized the window or the layout changed). |
QEvent::Close | The user attempted to close the widget's window. |
QEvent::ContextMenu | The user requested a context menu (the exact action depends on the operating system's way to open the context menu). |
QEvent::Paint | The widget needs to be repainted. |
QEvent::DragEnter, QEvent::DragLeave, QEvent::DragMove, QEvent::Drop | The user performs a drag and drop action. |
QEvent::TouchBegin, QEvent::TouchUpdate, QEvent::TouchEnd, QEvent::TouchCancel | A touchscreen or a trackpad reported an event. |
Each event type has a corresponding class that inherits QEvent (for example, QMouseEvent). Many event types have the dedicated virtual function, for example, QWidget::mousePressEvent and QGraphicsItem::mousePressEvent. More exotic events must be processed by re-implementing the QWidget::event (or QGraphicsItem::sceneEvent) function that receives all events, and using event->type() to check the event type.
Events dispatched in the graphics scene have special types (for example, QEvent::GraphicsSceneMousePress) and special classes (for example, QGraphicsSceneMouseEvent) because they have an extended set of information about the event. In particular, mouse events contain information about the coordinates in the item's and the scene's coordinate systems.