Qt-UI

Event Loop

QPainter | | Events

Qt applications are event driven.

This means that we do not act imperatively, but we react on events being delivered.

For this purpose the first action of each Qt application is to return program control to the so called event loop. The event loop gathers events of various kinds and dispatches them to the event handlers of those objects that they belong to.

Each Qt object that is a subclass of QObject has an event handler named QObject::event() that receives events of QEvent type.

The event() method does not handle the events itself. Based on the type of event delivered, it calls a specific event handler for that specific type of event, and notifies the caller whether the event was accepted or ignored.

The normal way for an event to be handled is by calling a virtual function. For example, QPaintEvent is handled 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.

For example, the following code handles left mouse button clicks on a custom subclassed QCheckBox widget while passing all other button clicks to the base class:

void MyCheckBox::mousePressEvent(QMouseEvent *event)
{
   if (event->button() == Qt::LeftButton)
      // handle left mouse button here
   else
      // pass on other buttons to base class
      QCheckBox::mousePressEvent(event);
}


QPainter | | Events

Options: