Qt Passing Custom QEvent Data

Class

1
2
3
4
5
6
7
8
9
10
class MyCustomEvent : QEvent
{
public:
QString my_data;
MyCustomEvent(QEvent::Type type) : QEvent(type)
{
}
};

Event handling

1
2
3
4
5
6
7
8
9
10
11
12
13
bool mainwindow::event(QEvent* event)
{
switch(static_cast<int>(event->type()))
{
case 1000:
{
MyCustomEvent* custom_event = reinterpret_cast<MyCustomEvent*>(event);
// do stuff with custom_event->my_data
} break;
};
return QMainWindow::event(event);
}

Posting the event

1
2
3
MyCustomEvent* event = new MyCustomEvent(QEvent::Type(1000));
event->my_data = "test";
QApplication::postEvent(this, reinterpret_cast<QEvent*>(event));