What is the difference between Q_ENUM and Q_ENUMS

后端 未结 2 1703
Happy的楠姐
Happy的楠姐 2021-01-08 00:31

I just found multiple examples showing the usage of Q_ENUM and Q_ENUMS and looking into the definition of Q_ENUM showed me that it inc

相关标签:
2条回答
  • 2021-01-08 00:41

    The What's New in Qt 5.5 says:

    Added Q_ENUM to replace Q_ENUMS, which allows to get a QMetaEnum at compile time using QMetaEnum::fromType. Such enums are now automatically registered as metatypes, and can be converted to strings within QVariant, or printed as string by qDebug().

    0 讨论(0)
  • 2021-01-08 00:47

    Since Qt 5.5 Q_ENUMS is deprecated, replaced with the better Q_ENUM.

    There is an example showing its use in the Qt documentation:

    class MyClass : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
    
    public:
        MyClass(QObject *parent = 0);
        ~MyClass();
    
        enum Priority { High, Low, VeryHigh, VeryLow };
        Q_ENUM(Priority)
    
        void setPriority(Priority priority)
        {
            m_priority = priority;
            emit priorityChanged(priority);
        }
    
        Priority priority() const
        { 
            return m_priority; 
        }
    
    signals:
        void priorityChanged(Priority);
    
    private:
        Priority m_priority;
    };
    

    For further details on the reasons behind the move from Q_ENUMS to Q_ENUM, read this blog entry

    0 讨论(0)
提交回复
热议问题