Qt. Declare custom QEvent type ONCE

2019-07-24 10:14发布

I have a .h file with such code:

const QEvent::Type MyOnEventType =
           QEvent::Type(QEvent::registerEventType( QEvent::User + 500 ) );

This header uses twice in application. I found a problem that in different places MyOnEventType have different integer value. If make a break point on that code above, debugger stops 9 times.

Please help how to declare custom QEvent type ONCE

标签: qt qevent
1条回答
手持菜刀,她持情操
2楼-- · 2019-07-24 10:53

I'm not 100% sure I'm understanding your question, but it sounds like you need to separate the declaration and the implementation. Something like:

my_event.h

class MyEvent : public QEvent {
 public:
  static const QEvent::Type MyEventType;
  // etc. 
};

my_event.cpp

#include "my_event.h"

const QEvent::Type MyEvent::MyEventType = 
        static_cast<QEvent::Type>(QEvent::registerEventType());

// etc.
查看更多
登录 后发表回答