Qt, sending and receiving events, and custom user events

Qt, sending and receiving events, and custom user events



1. Qt's event mechanism

The Qt program is event-driven, and each action of the program is triggered by an event behind the scenes; the occurrence and processing of events become the main line of program operation and exist in the entire life cycle of the program. There are many types of Qt events, common qt events are as follows:

  • Keyboard events: key presses and releases.
  • Mouse events: mouse movement, mouse button press and release.
  • Drag and drop event: Drag and drop with the mouse.
  • Wheel Events: Mouse wheel scrolling.
  • Paint event: Repaints certain parts of the screen.
  • Timed event: Timer expires.
  • Focus event: keyboard focus moves.
  • Enter and leave events: the mouse moves into or out of the widget.
  • Move event: The position of the widget changes.
  • Size change event: The size of the widget changes.
  • Show and hide events: widget shows and hides.
  • Window event: Whether the window is the current window.

In addition to the system's predefined events, sometimes we also need custom events to complete certain actions, or send certain actions to Qt's event loop for the display of the main interface.

For example, when using third-party components such as OpenCV and libhv, libhv needs to register a receiving callback function when receiving data through the network. In Qt, the callback function is a static function, and the received data cannot be displayed on the UI interface.

The solution is to implement a custom event with parameters and send the event to the main event loop of Qt, so that the received data can be displayed on the interface control through the ui pointer in the customEvent.


2. Custom events

All parent classes of Qt events are: QEvent

Qt event type: QEvent::Type

Among them, the event IDs that can be customized by users are 1000~65535.

The custom event method is also relatively simple, just inherit QEvent and pass in the custom type.

An example is shown below:

Custom type header file:

#ifndef CUSTOMEVENT_H
#define CUSTOMEVENT_H

#include <QEvent>
#include <QString>

// 注册自定义事件类型
#define CSTM_EVENT_ID1         static_cast<QEvent::Type>((QEvent::User + 1))
#define CSTM_EVENT_ID2         static_cast<QEvent::Type>((QEvent::User + 2))

class CustomEvent : public QEvent
{
    
    
public:
    CustomEvent();
};

class CustomEvent2 : public QEvent
{
    
    
public:
    CustomEvent2();
    QString msg;     /* 用于携带数据 */
};


#endif // CUSTOMEVENT_H

Source File:

#include "customevent.h"

CustomEvent::CustomEvent() : QEvent(CSTM_EVENT_ID1)
{
    
    

}

CustomEvent2::CustomEvent2() : QEvent(CSTM_EVENT_ID2)
{
    
    

}

3. Event sending and processing

There are two sending event functions: sendEvent and postEvent.

the difference:

The sendEvent function is blocking, so it supports the sending of stack space and heap space event objects (local objects and objects allocated by new).

The postEvent static function implements non-blocking sending, returns immediately after the event is sent, and the event will be sent to the event queue for processing.

Note: After the event object allocated by new is processed, it will be automatically destroyed by Qt internally.

To send events, you can use QApplication::sendEvent, or you can use the global variable qApp->sendEvent.

The sample code is as follows, and the virtual function customEvent is reimplemented in the main form:

protected:
    void customEvent(QEvent *event);
void Widget::customEvent(QEvent *event)
{
    
    
    switch ((int)event->type())
    {
    
    
    case CSTM_EVENT_ID1:
    {
    
    
        CustomEvent* e = static_cast<CustomEvent*>(event);
        qDebug() << "custom event 1";
        break;
    }
    case CSTM_EVENT_ID2:
    {
    
    
        CustomEvent2* e = static_cast<CustomEvent2*>(event);
        qDebug() << "custom event 2: " << e->msg;
        break;
    }
    default:break;
    }
}


void Widget::on_btnSendEvent1_clicked()
{
    
    
    CustomEvent e;
    qApp->sendEvent(this, &e);
}

void Widget::on_btnSendEvent2_clicked()
{
    
    
    CustomEvent2 e;
    e.msg = "send event 2 msg !";
    qApp->sendEvent(this, &e);
}

void Widget::on_btnPostEvent1_clicked()
{
    
    
    qApp->postEvent(this, new CustomEvent());
}

void Widget::on_btnPostEvent2_clicked()
{
    
    
    CustomEvent2* e = new CustomEvent2();
    e->msg = "post event 2 msg !";
    qApp->postEvent(this, e);
}


ends…

Guess you like

Origin blog.csdn.net/qq153471503/article/details/129495250
Recommended