The application uses events (inherited from QEvent ). Each event, as it should be, has a strictly defined type - an integer between QEvent::User and QEvent::MaxUser , obtained through the static function QEvent::registerEventType() .
Event types are easily identified in the receiver object method:
void MyClass::customEvent(QEvent *event) { if(event->type() == MyEventOne::eventType()) { MyEventOne *event1 = static_cast<MyEventOne*>(event); ... } else if(event->type() == MyEventTwo::eventType()) { MyEventTwo *event2 = static_cast<MyEventTwo*>(event); ... } else ... } However, problems begin as soon as an event comes from a dynamically loaded plug-in. The numeric values of the types of the same class are different.
I tried to register events in two ways.
Method one
File myeventone.h :
#include <QtCore/QEvent> class MyEventOne : public QEvent { public: static const QEvent::Type &_event_type; explicit MyEventOne(); }; File myeventone.cpp :
#include "myeventone.h" const QEvent::Type &MyEventOne::_event_type = static_cast<QEvent::Type>(QEvent::registerEventType()); MyEventOne::MyEventOne() : QEvent(_event_type) {} Second way
File myeventone.h :
#include <QtCore/QEvent> class MyEventOne : public QEvent { public: static QEvent::Type eventType(); explicit MyEventOne(); }; File myeventone.cpp :
#include <QtCore/QGlobalStatic> #include "myeventone.h" Q_GLOBAL_STATIC_WITH_ARGS(int, _g_event_type, (QEvent::registerEventType())) QEvent::Type MyEventOne::eventType() { return static_cast<QEvent::Type>(*_g_event_type); } MyEventOne::MyEventOne(): QEvent(MyEventOne::eventType()) {} In both cases, if I use the plugin and send an event object from it, I get a different numeric value of the event type. At the same time, the files of the MyEventOne class are the MyEventOne in the plugin and directly in the main part of the application.
I know that the QEvent::registerEventType() function has an hint argument that can act as a hint when generating a numeric value for an event type, but in this case depresses the need to define such hints (many event types) somehow globally and preferably a single list.
How can you solve this problem and if you cannot do without the above-mentioned hints, then how to use them correctly?
Addition
Made a helper static function that by name assigns a constant value to a specific type of event.
int EventHelper::registerEventType(const QString &class_name) { if(class_name == QLatin1String("MyEventOne")) return QEvent::registerEventType(QEvent::User+1000); else if(class_name == QLatin1String("MyEventTwo")) return QEvent::registerEventType(QEvent::User+1001); return QEvent::registerEventType(); } As a result, when an application is connected to a plugin, events from the plugin have a different type value. It becomes obvious that the event is being re-registered. How to get around this?