Managing events with an event filter
In this section, you will learn how to manage events and how to filter a specific event and perform a task. You can achieve event filtering by reimplementing event handlers and installing event filters. You can redefine what an event handler should do by subclassing the widget of interest and reimplementing that event handler.
Qt provides five different approaches for event processing, as follows:
- Reimplementing a specific event handler, such as
paintEvent()
- Reimplementing the
QObject::event()
function - Installing an event filter on the
QObject
instance - Installing an event filter on the
QApplication
instance - Subclassing
QApplication
and reimplementingnotify()
The following code handles the left mouse button click on a custom widget while passing all other button clicks to the base QWidget
class:
void MyClass::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton...