将 Qt::[枚举/标志] 注册为 QtMetaType
Registering Qt::[Enum/Flag] as QtMetaType
我想创建一个对象来访问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:无法处理属性
'MyQMLMouseEvent::button'
的未注册数据类型'Qt::MouseButton'
在这里,他们说我可以通过调用qRegisterMetaType
来注册这些类型来解决此问题。
我不确定在这种情况下这是否正确,甚至适用于
Qt
已经在 QML 中可用 - 所以再次注册其中的一部分对我来说似乎是错误的?- 我可以从我几乎无法控制的某个类中
qRegisterMetaType
枚举和标志吗?
令人不安的是:失败的是Qt::MouseButton
和Qt::MouseEventFlags
,但不是那么Qt:MouseButtons
(这只是Qt::MouseButton
的别名)。
这意味着我可以通过将类型更改为复数来规避Qt::MouseButton
的问题,并且Qt::MouseEventFlags
可以int
传递,因为它确实在 QML 中不可用(应该Qt.MouseEventCreatedDoubleClick
)
不过,只是出于好奇,如果将来有必要:如果尚未注册枚举和标志,从Qt
命名空间注册枚举和标志的正确方法是什么? 每当我使用一些完全不同的类时,我真的不想每次在我的main.cpp
中注册它们。
将Q_PROPERTY
类型设置为int
无需仅仅为了使用枚举而声明或注册任何额外的元类型。
C++
class MouseButtonObject : public QObject
{
Q_OBJECT
Q_PROPERTY(int button READ button)
public:
explicit MouseButtonObject(QObject *parent = nullptr);
Qt::MouseButton button() { return Qt::LeftButton; }
};
QML
if (mouseButtonObject.button === Qt.LeftButton) {
console.log("button is Left Button")
}
- 无法将结构注册为增强几何体3D点
- 如何使用AngelScript注册SFML Vector2运算符
- 在遍历处理程序的向量时注册和注销处理程序
- 有没有任务栏API可以立即应用注册表更改
- 使用QJsEngine在Qt中注册自定义类型
- 即使使用调试编译标志,表达式也是"optimized out"
- 检查注册表项是否链接到(或副本)另一个注册表项
- 在 CMake 中为每个目标设置编译器/链接器标志
- 如何使用 TStyleManager::UnRegisterStyle() 取消注册样式
- WINAPI 注册应用程序重新启动时不清除打开的套接字
- SFML 碰撞永远不会在我的系统中注册
- 尝试从C++访问 UWP 的电子邮件邮件类会导致"REGDB_E_CLASSNOTREG类未注册"错误
- 用于创建/注册虚拟存储设备的 IOKit 驱动程序
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- 在轮班操作后使用携带标志
- 如何找出引入AVX标志的内容
- 如何添加预防措施以绕过未注册Microsoft.ACE.OLEDB.12.0?
- I2C 文件描述符上的 I2C 总线可写/可读标志
- C ++是否有C ++ 17 OSX 10.13.6的标志
- 将 Qt::[枚举/标志] 注册为 QtMetaType