[Resolved]About the Q_ENUMS in Qt Quick



  • If I define a enum type out of the class, Can I then use Q_ENUMS to regist the enum type in the class that inherit the QObject?

    If can, that is to say , once I use Q_ENUMS, I can use the enum type everywhere including the QML file?



  • http://doc.qt.nokia.com/latest/qobject.html#Q_ENUMS

    bq. If you want to register an enum that is declared in another class, the enum must be fully qualified with the name of the class defining it. In addition, the class defining the enum has to inherit QObject as well as declare the enum using Q_ENUMS().

    What this means that class defining enum has to derive from QObject, so global scope enums i guess might not work even just registering with Q_ENUMS, found one useful way to do it @ http://taschenorakel.de/michael/2011/07/14/using-c-enums-qml/



  • [quote author="manishsharma" date="1313394709"]http://doc.qt.nokia.com/latest/qobject.html#Q_ENUMS

    bq. If you want to register an enum that is declared in another class, the enum must be fully qualified with the name of the class defining it. In addition, the class defining the enum has to inherit QObject as well as declare the enum using Q_ENUMS().

    What this means that class defining enum has to derive from QObject, so global scope enums i guess might not work even just registering with Q_ENUMS, found one useful way to do it at http://taschenorakel.de/michael/2011/07/14/using-c-enums-qml/
    [/quote]

    Yes, I have do in this.
    But, how can i define a property of the enum type in my QML file?

    @
    class Mynamespace :public QObject
    {
    Q_OBJECT
    Q_ENUMS(DataType)

     public:
    
    enum DataType
    {
        type1,
        type2,
        type3
     };
    

    }

    //in my qml file
    //import

    Item {
    property DataType//? I am not sure, it seems do not work.

    }

    @



  • instead of creating enum property can't you just create an int property and assign value to it from exposed enum class from c++? Other thing which you have to do is call qmlRegisterType<Mynamespace>

    Below is how i got it working,

    my enum class

    @
    class EnumClass : public QObject
    {
    Q_OBJECT
    Q_ENUMS(ClassType)
    public:
    enum ClassType { TypeOne, TypeTwo, TypeThree };
    };@

    main.cpp

    @int main(int argc, char **argv)
    {
    QApplication app(argc, argv);

    qmlRegisterType<EnumClass>("myEnum",1,0,"EnumClass");
    
    QDeclarativeView *view = new QDeclarativeView;
    view->setSource(QUrl::fromLocalFile&#40;"./main.qml"&#41;&#41;;
    view->show(&#41;;
    
    app.exec&#40;&#41;;
    

    }@

    my main.qml file

    @import Qt 4.7
    import myEnum 1.0

    Rectangle {
    id: page
    width: 500; height: 200
    color: "lightgray"
    property int en : EnumClass.TypeThree

     Text {
         id: helloText
         text: "Hello world!"
         y: 30
         anchors.horizontalCenter: page.horizontalCenter
         font.pointSize: 24; font.bold: true
    
         MouseArea {
             anchors.fill: parent
             onClicked: {
                 console.log(en&#41;;
             }
         }
     }
    

    }

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.