I was working on update of one of my game and I while re-factoring I was required to expose C++ Enum to QML code.
Following is how header looks like with definition of Enum GamePadButton. I am registering Enum with Qt's Metaobject system using Q_ENUMS macro.
In my case I dont want to create instace of GamePadObserver in QML, I just want to expose it enum to QML and for that purpose we can use qmlRegisterUncreatableType macro. Its useful for exposing enum and attached property. Following how we can use this macro.
Following is how header looks like with definition of Enum GamePadButton. I am registering Enum with Qt's Metaobject system using Q_ENUMS macro.
class GamePadObserver: public QObject { Q_OBJECT Q_ENUMS(GamePadButton) public: enum GamePadButton{ A_BUTTON=0, B_BUTTON, C_BUTTON, X_BUTTON, Y_BUTTON, Z_BUTTON, ... NO_BUTTON }; public: GamePadObserver(QObject* parent = 0); virtual ~GamePadObserver(); ... };Now to be able to see this Enum to QML code we need to register GamePadObserver class to Qt Metaobject system.If you want to be able to create instance of class then you can use qmlRegisterType() macro.
In my case I dont want to create instace of GamePadObserver in QML, I just want to expose it enum to QML and for that purpose we can use qmlRegisterUncreatableType macro. Its useful for exposing enum and attached property. Following how we can use this macro.
#includeNow we can use Enum from QML, following how we can do that.int main(int argc, char **argv) { qmlRegisterUncreatableType ("GamePadObserver", 1, 0,"GamePadObserver", ""); ... }
... import GamePadObserver 1.0 ... Rectangle { id:main ... Connections{ target: GamePad onButtonPressed: { if( button == GamePadObserver.X_BUTTON || button == GamePadObserver.Y_BUTTON || button == GamePadObserver.A_BUTTON || button == GamePadObserver.B_BUTTON ) { ... } } } }
No comments:
Post a Comment