Qt. Declare custom QEvent type ONCE

拜拜、爱过 提交于 2019-12-10 22:37:18

问题


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


回答1:


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.


来源:https://stackoverflow.com/questions/8231907/qt-declare-custom-qevent-type-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!