tags:

views:

543

answers:

1

I have created a custom event in my Qt application by subclassing QEvent.

class MyEvent : public QEvent
{
  public:
    MyEvent() : QEvent((QEvent::Type)2000)) {}
    ~MyEvent(){}
}

In order to check for this event, I use the following code in an event() method:

if (event->type() == (QEvent::Type)2000)
{
  ...
}

I would like to be able to define the custom event's Type somewhere in my application so that I don't need to cast the actual integer in my event methods. So in my event() methods I'd like to be able to do something like

if (event->type() == MyEventType)
{
  ...
}

Any thoughts how and where in the code I might do this?

+5  A: 

If the event-type identifies your specific class, i'd put it there:

class MyEvent : public QEvent {
public:
    static const QEvent::Type myType = static_cast<QEvent::Type>(2000);
    // ...
};

// usage:
if(evt->type() == MyEvent::myType) {
    // ...
}
Georg Fritzsche
That helped a lot, thanks! Though I don't entirely understand why you made you revision that you did.
Chris
If initialized in the class definition, it can be used in integral constant expressions like with `case` in `switch` statements or as array bounds.
Georg Fritzsche