Qt Passing Custom QEvent Data

Class

class MyCustomEvent : QEvent
{
public:

	QString my_data;

	MyCustomEvent(QEvent::Type type) : QEvent(type)
	{
	}
};

Event handling

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

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