Qt:定义自定义事件类型 [英] Qt: Defining a custom event type

查看:162
本文介绍了Qt:定义自定义事件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过子类化QEvent在我的Qt应用程序中创建了一个自定义事件。

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

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

为了检查这个事件,我在event()方法中使用下面的代码:

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

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

我想能够在应用程序中定义自定义事件的类型,以便我不需要在我的事件方法中转换实际的整数。所以在我的event()方法我想能够做一些像

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)
{
  ...
}

推荐答案

如果事件类型标识你的特定类,

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) {
    // ...
}

这篇关于Qt:定义自定义事件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆