我想创建一个对象来访问QMouseEvent
的值。
此事件有例如
Qt::MouseButton QMouseEvent::button() const
所以我添加到我的班级MyQMLMouseEvent
Q_PROPERTY(Qt::MouseButton button READ button)
和必要的吸气剂。
我预计,在QML方面,我可以这样使用它:
myQmlMouseEvent.button === Qt.ButtonLeft
Qt.ButtonLeft
可用,例如使用使用MouseArea
' clicked
信号。
现在进行测试,我处理的信号是我在QML中传递这个MyQMLMouseEvent
- 实例,如下所示:
onMyQMLMousEvent: console.log(JSON.stringify(myMouseEvent))
打印大多数属性,但在Qt::MouseButton
类型上失败。为此,它给了我错误:
QMetaProperty :: read:无法处理属性
'Qt::MouseButton'
的未注册数据类型'MyQMLMouseEvent::button'
Here他们说我可以通过致电qRegisterMetaType
来注册这些类型来解决这个问题
我不确定在这种情况下这是正确的还是可以用于
Qt
已经在QML中提供了 - 所以再次注册部分内容似乎对我不对了?qRegisterMetaType
枚举一些我无法控制的课程中的标志吗?有趣的是:失败的是Qt::MouseButton
和Qt::MouseEventFlags
,但不是Qt:MouseButtons
(这只是Qt::MouseButton
的别名)。
这意味着我可以通过将类型更改为复数来绕过Qt::MouseButton
的问题,Qt::MouseEventFlags
可以作为int
传递,因为它确实在QML中不可用(应该是{ {1}})
仍然只是出于好奇,以及将来是否有必要: 从Qt.MouseEventCreatedDoubleClick
命名空间注册枚举和标志的正确方法是什么还没注册?
每当我使用一些完全不同的课时,我都不想每次都在Qt
注册它们。
答案 0 :(得分:3)
将Q_PROPERTY
类型设置为int
没有必要声明或注册任何额外的元类型只是为了使用枚举。
class MouseButtonObject : public QObject
{
Q_OBJECT
Q_PROPERTY(int button READ button)
public:
explicit MouseButtonObject(QObject *parent = nullptr);
Qt::MouseButton button() { return Qt::LeftButton; }
};
if (mouseButtonObject.button === Qt.LeftButton) {
console.log("button is Left Button")
}