Q_ENUM 和 Q_ENUMS 有什么区别
What is the difference between Q_ENUM and Q_ENUMS
我刚刚找到了多个显示 Q_ENUM
和 Q_ENUMS
用法的示例,并且查看 Q_ENUM
的定义表明它包括 Q_ENUMS
和其他定义。
在Q_PROPERTY
、Qml/QtQuick、signals/slots、QVariants和qDebug()
输出中使用枚举时,我不确定要写哪一个。
似乎 Q_ENUM
更好,因为它是使用 Q_ENUMS
定义的,但我只是猜测。
到底有什么区别,为什么会有两个,应该优先选择哪个?
由于 Qt 5.5 Q_ENUMS
已被弃用,取而代之的是更好的 Q_ENUM
。
中有一个示例展示了它的用法
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;
};
有关从 Q_ENUMS
移动到 Q_ENUM
背后原因的更多详细信息,请阅读 this blog entry
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().
我刚刚找到了多个显示 Q_ENUM
和 Q_ENUMS
用法的示例,并且查看 Q_ENUM
的定义表明它包括 Q_ENUMS
和其他定义。
在Q_PROPERTY
、Qml/QtQuick、signals/slots、QVariants和qDebug()
输出中使用枚举时,我不确定要写哪一个。
似乎 Q_ENUM
更好,因为它是使用 Q_ENUMS
定义的,但我只是猜测。
到底有什么区别,为什么会有两个,应该优先选择哪个?
由于 Qt 5.5 Q_ENUMS
已被弃用,取而代之的是更好的 Q_ENUM
。
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;
};
有关从 Q_ENUMS
移动到 Q_ENUM
背后原因的更多详细信息,请阅读 this blog entry
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().