Qt: Defining a custom event type

后端 未结 3 1199
粉色の甜心
粉色の甜心 2020-12-29 07:53

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

class MyEvent : public QEvent
{
  public:
    MyEvent() : QEvent((QEvent::Type)20         


        
3条回答
  •  天涯浪人
    2020-12-29 08:20

    For convenience, you can use the QEvent::registerEventType() static function to register and reserve a custom event type for your application. Doing so will allow you to avoid accidentally re-using a custom event type already in use elsewhere in your application.

    Example:

    class QCustomEvent : public QEvent
    {
    public:
        QCustomEvent() : QEvent(QCustomEvent::type())
        {}
    
        virtual ~QCustomEvent()
        {}
    
        static QEvent::Type type()
        {
            if (customEventType == QEvent::None)
            {
                int generatedType = QEvent::registerEventType()
                customEventType = static_cast(generatedType);
            }
            return customEventType;
        }
    
    private:
        static QEvent::Type customEventType;
    };
    
    QEvent::Type QCustomEvent::customEventType = QEvent::None;
    

提交回复
热议问题