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. How to make a Enum known to QML if it is inside a QObject derived class
Forum Updated to NodeBB v4.3 + New Features

How to make a Enum known to QML if it is inside a QObject derived class

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 1.2k 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I have a Q_OBJECT derived class, which has several Q_PROPERTY values. One of them is an enum, defined in that class. In order to make it known to QML, I would need to do Q_DECLARE_METATYPE(MarkGui), but this is not possible as Q_DECLARE_METATYPE requires a copy constructor, which is forbidden for Q_OBJECT derived classes. What is the way to do it?

    raven-worxR 1 Reply Last reply
    0
    • M maxwell31

      Hi,

      I have a Q_OBJECT derived class, which has several Q_PROPERTY values. One of them is an enum, defined in that class. In order to make it known to QML, I would need to do Q_DECLARE_METATYPE(MarkGui), but this is not possible as Q_DECLARE_METATYPE requires a copy constructor, which is forbidden for Q_OBJECT derived classes. What is the way to do it?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @maxwell31 said in How to make a Enum known to QML if it is inside a QObject derived class:

      I would need to do Q_DECLARE_METATYPE(MarkGui)

      No.
      Actually you need Q_ENUM

      enum MyEnum {
          ...
      };
      Q_ENUM(MyEnum)
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by maxwell31
        #3

        Hm, I was using Q_ENUMS. But When I try to use it, I get the following error:

          QMetaProperty::read: Unable to handle unregistered datatype 'myEnumType' for property 'QQmlDMObjectData::myEnumType'.
        

        I also registered the class which hosts the enum:

          qmlRegisterType<MyClass>("MyClass",1,0,"MyClass");
        

        So the problem seems to be, that I use the enum as a property

        raven-worxR 1 Reply Last reply
        0
        • M maxwell31

          Hm, I was using Q_ENUMS. But When I try to use it, I get the following error:

            QMetaProperty::read: Unable to handle unregistered datatype 'myEnumType' for property 'QQmlDMObjectData::myEnumType'.
          

          I also registered the class which hosts the enum:

            qmlRegisterType<MyClass>("MyClass",1,0,"MyClass");
          

          So the problem seems to be, that I use the enum as a property

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @maxwell31
          how does your Q_PROPERTY declaration look like?
          Did you add the class namespace to the enum type in the property declaration?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • M Offline
            M Offline
            maxwell31
            wrote on last edited by
            #5

            Here the class:

            class MarkGui: public QObject {
              Q_OBJECT
              Q_PROPERTY(MarkType markType READ markType WRITE       setMarkType NOTIFY markTypeChanged)
            
            public:
              MarkGui(QObject *parent = 0);
              explicit MarkGui(Mark * m, QObject *parent = 0);
              enum MarkType {
                MOMENT,
                TIMERANGE,
              };
               Q_ENUM(MarkType);
            

            I tried with adding the class namespace, but it leads to the same result

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxwell31
              wrote on last edited by
              #6

              Hm, it seems I need to register the enum seperatly
              qRegisterMetaTypeMarkGui::MarkType("MarkGui::MarkType");

              Gojir4G 1 Reply Last reply
              0
              • ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by ODБOï
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • M maxwell31

                  Hm, it seems I need to register the enum seperatly
                  qRegisterMetaTypeMarkGui::MarkType("MarkGui::MarkType");

                  Gojir4G Offline
                  Gojir4G Offline
                  Gojir4
                  wrote on last edited by Gojir4
                  #8

                  Hi @maxwell31,

                  Usually you have not to register the enum. This would be a pain when you have tens of enums declared.
                  But you need to register the class hosting the enum, because you need a keyword to access the enum from QML side.

                  So Q_ENUM(MarkType) more calling qmlRegisterType<MarkGui>("MarkGui",1,0,"MarkGui"); at runtime should be enough to retrieve the enum from QML.

                  This works perfectly on my side:

                  #ifndef MARKGUI_H
                  #define MARKGUI_H
                  
                  #include <QObject>
                  #include <QtQml>
                  
                  class MarkGui: public QObject {
                      Q_OBJECT
                      Q_PROPERTY(MarkType markType READ markType WRITE setMarkType NOTIFY markTypeChanged)
                  
                  public:
                      MarkGui(QObject *parent = 0) : QObject(parent){};
                  
                      enum MarkType {
                          MOMENT,
                          TIMERANGE,
                      };
                      Q_ENUM(MarkType);
                  
                      MarkType markType() const
                      {
                          return m_markType;
                      }
                      static void declareQml(){
                          qmlRegisterType<MarkGui>("MarkGui", 1, 0, "MarkGui");
                      }
                  public slots:
                      void setMarkType(MarkType markType)
                      {
                          if (m_markType == markType)
                              return;
                  
                          m_markType = markType;
                          emit markTypeChanged(m_markType);
                      }
                  signals:
                      void markTypeChanged(MarkType markType);
                  private:
                      MarkType m_markType;
                  };
                  #endif // MARKGUI_H
                  
                  //main()
                  //...
                      MarkGui markGui;
                      MarkGui::declareQml();
                  
                      QQmlApplicationEngine engine;
                      engine.rootContext()->setContextProperty("markGui", &markGui);
                  ...
                  
                  

                  Then from QML:

                       import MarkGui 1.0
                       ...
                       markGui.markType = MarkGui.MOMENT
                       console.log(markGui.markType)
                       markGui.markType = MarkGui.TIMERANGE
                       console.log(markGui.markType)
                  

                  Output:

                  qml: 0
                  qml: 1
                  
                  1 Reply Last reply
                  3
                  • M Offline
                    M Offline
                    maxwell31
                    wrote on last edited by
                    #9

                    Ok, good to know.Thank you for your help!

                    1 Reply Last reply
                    0

                    • Login

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