Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Resolved]About the Q_ENUMS in Qt Quick
Forum Updated to NodeBB v4.3 + New Features

[Resolved]About the Q_ENUMS in Qt Quick

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 4.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    harlentan
    wrote on 15 Aug 2011, 07:23 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      manishsharma
      wrote on 15 Aug 2011, 07:51 last edited by
      #2

      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/

      1 Reply Last reply
      0
      • H Offline
        H Offline
        harlentan
        wrote on 15 Aug 2011, 09:21 last edited by
        #3

        [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.

        }

        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          manishsharma
          wrote on 15 Aug 2011, 10:00 last edited by
          #4

          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;;
                   }
               }
           }
          

          }

          @

          1 Reply Last reply
          0

          1/4

          15 Aug 2011, 07:23

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved