Showing posts with label Enum. Show all posts
Showing posts with label Enum. Show all posts

Sunday, December 1, 2013

Exposing C++ ENUM to QML

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.
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.
#include 

int main(int argc, char **argv)
{
 qmlRegisterUncreatableType("GamePadObserver", 1, 0,"GamePadObserver", "");
        ...
}
Now we can use Enum from QML, following how we can do that.
...
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 ) {
                ...
            } 
        }        
    }

}

Tuesday, May 25, 2010

Enum value to String in Qt

Update:

it looks like that Qt framework made some changes that made staticQtMetaObject as protected.

Now to get staticQtMetaObject you need to use following code.
class StaticQtMetaObject : public QObject
{
public:
    static inline const QMetaObject& get() {return staticQtMetaObject;}
};
then use it like following
QString KeyboardOptionWidget::getKeyString( int aKeyCode )
{
    int index = StaticQtMetaObject::get().indexOfEnumerator("Key");
    QMetaEnum metaEnum = StaticQtMetaObject::get().enumerator( index);
    QString keyString = metaEnum.valueToKey( aKeyCode );
    return keyString.replace("Key_","");
    return QString();
}
--------------
Some time we might need to convert Enum value to string for various reason like logging or debugging. I also needed similar thing for one of my project.

I found one interesting code that uses Qt's meta-object system to convert Enum to String. I have created one similar function that convert key board code to string.

QString KeyboardOptionWidget::getKeyString( int aKeyCode )
{
    int index = QApplication::staticQtMetaObject.indexOfEnumerator("Key");
    QMetaEnum metaEnum = QApplication::staticQtMetaObject.enumerator( index);
 
    QString keyString = metaEnum.valueToKey( aKeyCode );
    return keyString.replace("Key_","");
} 

Hope this helps.