我有一个包含这样代码的 .h 文件:
const QEvent::Type MyOnEventType =
QEvent::Type(QEvent::registerEventType( QEvent::User + 500 ) );
此标头在应用程序中使用了两次。我发现一个问题,在不同的地方 MyOnEventType 有不同的整数值。如果在上面的代码上设置断点,调试器会停止 9 次。
请帮助如何声明自定义 QEvent 类型 ONCE
我不是 100% 确定我理解你的问题,但听起来你需要将声明和实现分开。就像是:
my_event.h
class MyEvent : public QEvent {
public:
static const QEvent::Type MyEventType;
// etc.
};
我的事件.cpp
#include "my_event.h"
const QEvent::Type MyEvent::MyEventType =
static_cast<QEvent::Type>(QEvent::registerEventType());
// etc.