How to pass an enum from a c++ pure virtual class to qml?
-
I am desperately trying to pass an enum from C++ to qml.
My class, with deviceType() as patient, looks as follows:
enum InputDeviceType { Undefined, Keyboard, Joystick, Network, Mouse, Mouse3d }; class InputDeviceConfiguratorGate : public QObject { Q_OBJECT Q_DISABLE_COPY(InputDeviceConfiguratorGate) Q_PROPERTY( InputDeviceType deviceType READ deviceType ) public: explicit InputDeviceConfiguratorGate(QObject *parent = 0); virtual ~InputDeviceConfiguratorGate() = 0; virtual InputDeviceType deviceType(); Q_INVOKABLE virtual QString deviceId() = 0; ... };
The class is registered to qml via:
qmlRegisterUncreatableType<InputDeviceConfiguratorGate>( "InputDeviceConfiguratorGate", 1, 0, "InputDeviceConfiguratorGate", "Not creatable in Qml." );
I tried a lot of different combinations: putting the enum in a separate class, using Q_ENUM, Q_ENUMS (i know, deprecated), introduce Q_PROPERTY and remove Q_INVOKABLE and make the function not pure virtual.
From the qml side, when trying to reference, I get the error:
ReferenceError: InputDeviceType is not defined
Which, so I believe, comes from the fact that InputDeviceConfiguratorGate is registered via qmlRegister Uncreatable Type.
I know about the following bug: https://bugreports.qt.io/browse/QTBUG-58454, which made me remove Q_INVOKABLE.
Are enums even possible in interfaces?
-
@c64zottel said in How to pass an enum from a c++ pure virtual class to qml?:
I tried a lot of different combinations: putting the enum in a separate class, using Q_ENUMS (i know, deprecated), introduce Q_PROPERTY and remove Q_INVOKABLE and make the function not pure virtual.
Have you tried Q_ENUM? The one which is meant to replace Q_ENUMS?
But I get among all the other errors:
ReferenceError: InputDeviceType is not defined
Is this a compiler error or what, and in what situation it comes? Please don't detach part of error messages from their contexts without even explaining where they come from, it doesn't help us at all.