Event Handling & Signal/Slot Mechanism in Qt

Reference

Event Handling (OReilly): https://www.oreilly.com/library/view/programming-with-qt/0596000642/ch16.html

Not very recommended reference/link: https://en.ittrip.xyz/python/pyqt-event-signal-guide
– provide general review and is in good structure, but
– has Google Vignette ad
– titled A Comprehensive Guide but the article is very short
– use PyQt, and the example use event.key() that somehow cannot be used in PySide

Event Handling & Signal/Slot Mechanism: Central to Qt Power

– Central to Qt power: event handling & the signal/slot mechanism
> a paradigm that allows for clean & intuitive communication between objects

– Event handling:
the process by which the application responds to different user/system-generated events
– Event:
can be anything
e.g., a mouse click, a key press, a system-generated notification
– Event-driven:
> Qt widgets are event-driven
> meaning: reacts to events that are specifically relevant to their functionality

Basic event handling in Qt
Example 1:
– def mousePressEvent(self, event): …
> inside the class MyWidget(QWIdget):
> overriding the mousePressEvent method to react to mouse press events

Event Handling: Basic & Advanced

Event handling
a. basic:
– how to respond to events
> by connecting to signals
> by reimplementing event handlers
b. advanced
> by overriding the standard event-handling mechanism
— by using event filters & synthetic events

Event Filters
– Event Filter:
> an object that gets all events for another object
> has the chance to react to an event before the original addressee of the event can do so
> useful when you want to overried the behavior of an object, but do not want to or cannot cahnge the class of the object itself

Signal & Slots in Qt

Reference:
recommended https://doc.qt.io/qtforpython-6/tutorials/basictutorial/signals_and_slots.html
not recommended https://www.tutorialspoint.com/pyqt/pyqt_slot_connection.htm

Example:
– Action: Clicking a button
– Signal: the ‘click’
– Slot: what happens when that button is clicked, e.g., closing a window, saving a document, etc

1. Signals are emitted by objects
– All classes that inherited from QObject or one of its subclasses (e.g., QWidget), can contain signals and slots
– This is how the object communicates
– The object does not know/care whether anything is receiving the signals it emits

2. Slots can be used for receiving signals
– Slot does not know if it has any signals connected to it

Qt’s Widgets predefined signals & slots
a. QAbstractButton: clicked() signal
b. QLineEdit: clear() slot
Example:
– a text input field with a button to clear the text
– QLineEdit & QToolButton on its right side
– connect QToolButton’s clicked() signal to the slot clear() by using the connect() method of the signal

button = QToolButton()
line_edit = QLineEdit()
button.clicked.connect(line_edit.clear)

Working with QtEvents (C++)

Reference: https://www.learnqt.guide/working-with-events

1. What are Events
– Events:
> objects in your Qt application, and are represented by the QEvent class
> low level mechanism allowing you to control the look & the behaviour of your classes & objects, especially widgets
> very useful for building event related features inside your objects
> can be generated form within the application
> can be as a result of some external activity

– Example 1:
> QMouseEvent: an event for when someone clicks on a button
> QResizeEvent: an event for when one of your widgets is resized
> QCloseEvent: an event for when your application is closed

– Example 2:
> Want: Button to turn green when the mouse is hovering on top of it

2. Events and Signals/Slots
– The purpose of evnets is somewhat the same as for signals and slots
– Signals/slots allow you to respond when something happens
> something happens
> a signal is emitted
> if interested in that signal, you connect it to your slot and respond however you want in the implementation of your slot

Events
– allow you to do much more low level things that deeply affect the behavior of your objects
– Look deep:
> the signal and slot mechanism in Qt is itself powered by events

Using Events:
a. start with widget header class
b. capture the close event for the widget:
– add an override of the closeEvent() method to your widget