Event handling in Qt--Qt

Keywords: C++ Qt

1. Qt event handling

A. Message processing model for GUI applications

B.Qt platform converts system generated messages into Qt events
1.Qt event is an object of QEvent
2.Qt events are used to describe actions that occur inside or outside a program
3. Any QObject object has the ability to handle events

Event handling for C.GUI applications
1.Qt events are assigned to QWidget objects immediately after they occur
2. event(QEvent) in QWidget for event handling
3.event() calls different event handlers based on event type
4. Send Qt predefined signals in event handlers
5. Call the signal-associated slot function
Scene Analysis

QPushButton Event Processing Analysis
1. Receive mouse events
2. Call event(QEvent) member function
3. Call mouseReleaseEvent(QMouseEvent*) member function
4. Call click() member function
5. Trigger SIGNAL(clicked())
Code Sample--Custom Events

QMyPushButton.cpp
#include "QMyPushButton.h"
#include <QMouseEvent>

QMyPushButton::QMyPushButton(QWidget* parent, QButtonListener* listener) : QPushButton(parent)
{
    m_listener = listener;
}

void QMyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if( m_listener != NULL )
    {
        m_listener(this, e);

        e->accept();

        setDown(false);
    }
    else
    {
        QPushButton::mouseReleaseEvent(e);
    }
}

QMyPushButton.h
#ifndef QMYPUSHBUTTON_H
#define QMYPUSHBUTTON_H

#include <QPushButton>

typedef void (QButtonListener)(QObject*, QMouseEvent*);

class QMyPushButton : public QPushButton
{
    Q_OBJECT
protected:
    QButtonListener* m_listener;

    void mouseReleaseEvent(QMouseEvent *e);
public:
    explicit QMyPushButton(QWidget* parent = 0, QButtonListener* listener = 0);

signals:

public slots:

};
#endif // QMYPUSHBUTTON_H

Widget.cpp
#ifndef QMYPUSHBUTTON_H
#define QMYPUSHBUTTON_H

#include <QPushButton>

typedef void (QButtonListener)(QObject*, QMouseEvent*);

class QMyPushButton : public QPushButton
{
    Q_OBJECT
protected:
    QButtonListener* m_listener;

    void mouseReleaseEvent(QMouseEvent *e);
public:
    explicit QMyPushButton(QWidget* parent = 0, QButtonListener* listener = 0);

signals:

public slots:

};
#endif // QMYPUSHBUTTON_H

Widget.h
#ifndef QMYPUSHBUTTON_H
#define QMYPUSHBUTTON_H

#include <QPushButton>

typedef void (QButtonListener)(QObject*, QMouseEvent*);

class QMyPushButton : public QPushButton
{
    Q_OBJECT
protected:
    QButtonListener* m_listener;

    void mouseReleaseEvent(QMouseEvent *e);
public:
    explicit QMyPushButton(QWidget* parent = 0, QButtonListener* listener = 0);

signals:

public slots:

};
#endif // QMYPUSHBUTTON_H

The results of running and printing when the button is released are shown in the diagram

D. Events and signals are different
1. Events are handled by specific objects
2. Signals are generated actively by specific objects
3. Overwriting event handlers may cause changes in program behavior
4. The existence of a slot function does not change program behavior.
5. Generally speaking, signals are generated in specific event handling functions
E. Delivery of events

Key member functions in QEvent
1.void ignore() --Receiver ignores current event, which may be passed to parent component
2.void accept() --Receiver expects to handle current event
3.bool isAccepted() -- Determine if the current event has been handled
The result of code generation runs as shown in the diagram

Event filtering in F.Qt
1. Event filters can monitor events received by other components
2. Any QObject object can be used as an event filter
3. Event filter object needs to override the eventFilter() function
Components install event filters through the installEventFilter() function
1. Event filters receive events before components
2. Event filters can decide whether to forward events to component objects

Basic code implementation

Summary
1.Qt events and signals are different
2. Events are handled by objects in QObject
3. Signal triggered by QObject object
4. Override event handler may cause this mutant behavior
5. Signal triggering will not affect program behavior
6. Event handling is very common in practical engineering development

Posted by bossman on Wed, 14 Aug 2019 19:23:04 -0700