Qt Custom Event Handling: Creating and Dispatching Custom Events

Qt enables the creation of custom event classes that must inherit from QEvent. Each custom event class requires a globally unique Type value (retrievable via event->type()) and a corresponding event handling method in the application.

Creating a Custom Event Class

To create a custom event class:

  1. Inherit from QEvent.
  2. Define a globally unique Type value.

Qt Event Type Values

Each event class has a unique Type value (similar to an ID). Custom event classes must use values after QEvent::User (since earlier values are reserved for Qt's predefined events). Insure QEvent::User + VALUE is globally unique.

Handling Custom Events

Two approaches exist for handling custom events:

  1. Event Filter: Install a event filter on the target object and implement logic in eventFilter().
  2. Event Overriding: Override the event() function in the target object's class and implement logic there.

Significance of Custom Event Classes

Custom events are useful for:

  • Extending functionality of existing component classes.
  • Developing new component classes with unique features.
  • Sending messages to third-party component classes.

#include <QEvent> #include <QString>

class CustomStringEvent : public QEvent { QString m_payload; public: static const Type TYPE = static_cast<Type>(QEvent::User + 100);

explicit CustomStringEvent(QString payload = "");
QString payload() const;

};

#endif // CUSTOMSTRINGEVENT_H_


**CustomStringEvent Header File**

</div><div>```
#include "CustomStringEvent.h"

CustomStringEvent::CustomStringEvent(QString payload)
    : QEvent(TYPE)
{
    m_payload = payload;
}

QString CustomStringEvent::payload() const
{
    return m_payload;
}

CustomStringEvent Implementation File

#include <QWidget> #include <QPushButton> #include <QLineEdit> #include "CustomStringEvent.h"

class Widget : public QWidget { Q_OBJECT

QLineEdit lineEdit;
QPushButton button;

public: bool eventFilter(QObject *obj, QEvent *evt); bool event(QEvent *evt);

Widget(QWidget *parent = nullptr);
~Widget();

};

#endif // WIDGET_H


**Widget Header File**

</div><div>```
#include "Widget.h"
#include <QApplication>
#include <QMouseEvent>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent), lineEdit(this)
{
    lineEdit.installEventFilter(this);
}

bool Widget::event(QEvent *evt)
{
    if (evt->type() == QMouseEvent::MouseButtonDblClick)
    {
        qDebug() << "Event: Before sending custom event";

        CustomStringEvent cse("HelloQt");

        QApplication::sendEvent(&lineEdit, &cse);

        qDebug() << "Event: After sending custom event";
    }
    return QWidget::event(evt);
}

bool Widget::eventFilter(QObject *obj, QEvent *evt)
{
    if ((obj == &lineEdit) && (evt->type() == CustomStringEvent::TYPE))
    {
        CustomStringEvent *cse = dynamic_cast<CustomStringEvent*>(evt);
        if (cse)
        {
            lineEdit.insert(cse->payload());
            qDebug() << "Received:" << cse->payload();
            return true;
        }
    }
    return QWidget::eventFilter(obj, evt);
}

Widget::~Widget() {}

Widget Implemantation File

int main(int argc, char *argv[]) { QApplication app(argc, argv); Widget w; w.show(); return app.exec(); }


**Main Function**

</div>### Summary

- Custom event classes must inherit from `QEvent`.
- Use values after `QEvent::User` for the `Type` value.
- The `Type` value must be globally unique.
- Provide a handling method for custom events (event filter or overridden `event()`).

</div>

Tags: Qt QEvent custom events Event Handling eventFilter

Posted on Fri, 29 May 2026 22:04:26 +0000 by NoPHPPhD