I need to create several custom event classes for a Qt application.
Right now, it looks like I will need to implement the following event type registration code for each event class:
class MyEvent : public QEvent
{
public:
MyEvent() : QEvent(registeredType())
{
}
static QEvent::Type eventType;
private:
static QEvent::Type registeredType();
}
QEvent::Type MyEvent::eventType = QEvent::None;
QEvent::Type MyEvent::registeredType()
{
if (eventType == QEvent::None)
{
int generatedType = QEvent::registerEventType();
eventType = static_cast<QEvent::Type>(generatedType);
}
return eventType;
}
Any suggestions on how I can simplify this, or at least hide it with a macro?
Event Filters The QObject::installEventFilter() function enables this by setting up an event filter, causing a nominated filter object to receive the events for a target object in its QObject::eventFilter() function.
The QEvent class is the base class of all event classes. Event objects contain event parameters.
Qt is an event-based system, and all GUI applications are event-driven. In an event-driven application, there is usually a main loop that listens for events and then triggers a callback function when one of those events is detected.
That's what templates are for. They can be used with constant integral parameters, which need to be known at compile time too:
enum EventNames { UpdateEvent,... }
template<EventNames E>
class MyEvent : public QEvent
{
public:
MyEvent() : QEvent(registeredType())
{
}
static QEvent::Type eventType;
private:
static QEvent::Type registeredType();
}
The common code lokes like this:
template<EventNames E>
QEvent::Type MyEvent<E>::registeredType()
{
if (eventType == QEvent::None)
{
int generatedType = QEvent::registerEventType();
eventType = static_cast<QEvent::Type>(generatedType);
}
return eventType;
}
Static initialization (beware!) looks like this:
QEvent::Type MyEvent<UpdateEvent>::eventType = QEvent::None;
The code specific for each event type can be implemented as template specialization then.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With