Qt自定义一个事件类型

继承QEvent类,在type()函数中调用QEvent::registerEventType()得到一个合法的eventType:

 1 class QCustomEvent : public QEvent
 2 {
 3 public:
 4     QCustomEvent() : QEvent(QCustomEvent::type())
 5     {}
 6 
 7     virtual ~QCustomEvent()
 8     {}
 9 
10     static QEvent::Type type()
11     {
12         if (customEventType == QEvent::None)
13         {
14             int generatedType = QEvent::registerEventType()
15             customEventType = static_cast<QEvent::Type>(generatedType);
16         }
17         return customEventType;
18     }
19 
20 private:
21     static QEvent::Type customEventType;
22 };
23 
24 QEvent::Type QCustomEvent::customEventType = QEvent::None;

猜你喜欢

转载自www.cnblogs.com/wisdomroc/p/13397450.html