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 pass enum class to QML?
Forum Updated to NodeBB v4.3 + New Features

How to pass enum class to QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlenum classenums
4 Posts 2 Posters 4.1k 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.
  • S Offline
    S Offline
    someoneinthebox
    wrote on 26 May 2016, 07:56 last edited by
    #1

    Hello everyone!

    I am on learning to QML with Qt and get some trouble with passing enum class to qml.

    When I use signal with int parameters - it's all right and code work perfectly.

    But, and here the trouble, if I use signal with some enum class parameters I have undefined values in qml signal handler. I tried to register enum class through qmlRegisterType but it's not working. I will be grateful for the help.

    Here the code:

    Some Helper class

    class Helper : public QObject
    {
        Q_OBJECT
        Q_ENUMS(Requester)
        Q_ENUMS(JANSWER)
    
    public:
    
    enum class Requester {
        ReqLogin,
        ReqNull,
        ReqSave,
        ReqError,
        ReqUnknown
    };
    
    enum class JANSWER {
        OK,
        Complete,
        Error,
        Unknown
    };
    
    };
    

    main.cpp

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        qmlRegisterType<JsonPackWorker>("com.jsonpackworker", 1, 0, "JsonPackWorker");
        qmlRegisterType<Helper>("com.Helper", 1, 0, "Helper");
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    }
    

    JsonPackWorker.h

    class JsonPackWorker : public QObject
    {
        Q_OBJECT
    public:
        <...>
    
    signals:    
        <...>
        void sendAnswer(Helper::Requester req, Helper::JANSWER answer);
    
    public slots:
        <...>
    };
    

    Somewehere in JsonPackWorker.cpp

    emit sendAnswer(Helper::Requester::ReqNull, Helper::JANSWER::OK);
    

    main.qml

    <...>
    import com.jsonpackworker 1.0
    import com.Helper 1.0
    
    ApplicationWindow {
        id: mainWindow
    
        // Requests in answers
        property int reqLogin: Helper.ReqLogin
    
        <...>
    
        JsonPackWorker {
            id: packWorker
            <...>
            onSendAnswer: {
                // Here I've got undefined undefined if there enum class parameters
                console.log(req + " " + answer)
                switch(req) {
                case reqLogin: loginDial.checkLogin(answer)
                    break;
                default:
                }
            }
        }
    
    }
    

    Mom said I am special.

    P 1 Reply Last reply 26 May 2016, 09:10
    0
    • S someoneinthebox
      26 May 2016, 07:56

      Hello everyone!

      I am on learning to QML with Qt and get some trouble with passing enum class to qml.

      When I use signal with int parameters - it's all right and code work perfectly.

      But, and here the trouble, if I use signal with some enum class parameters I have undefined values in qml signal handler. I tried to register enum class through qmlRegisterType but it's not working. I will be grateful for the help.

      Here the code:

      Some Helper class

      class Helper : public QObject
      {
          Q_OBJECT
          Q_ENUMS(Requester)
          Q_ENUMS(JANSWER)
      
      public:
      
      enum class Requester {
          ReqLogin,
          ReqNull,
          ReqSave,
          ReqError,
          ReqUnknown
      };
      
      enum class JANSWER {
          OK,
          Complete,
          Error,
          Unknown
      };
      
      };
      

      main.cpp

      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
      
          qmlRegisterType<JsonPackWorker>("com.jsonpackworker", 1, 0, "JsonPackWorker");
          qmlRegisterType<Helper>("com.Helper", 1, 0, "Helper");
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
          return app.exec();
      }
      

      JsonPackWorker.h

      class JsonPackWorker : public QObject
      {
          Q_OBJECT
      public:
          <...>
      
      signals:    
          <...>
          void sendAnswer(Helper::Requester req, Helper::JANSWER answer);
      
      public slots:
          <...>
      };
      

      Somewehere in JsonPackWorker.cpp

      emit sendAnswer(Helper::Requester::ReqNull, Helper::JANSWER::OK);
      

      main.qml

      <...>
      import com.jsonpackworker 1.0
      import com.Helper 1.0
      
      ApplicationWindow {
          id: mainWindow
      
          // Requests in answers
          property int reqLogin: Helper.ReqLogin
      
          <...>
      
          JsonPackWorker {
              id: packWorker
              <...>
              onSendAnswer: {
                  // Here I've got undefined undefined if there enum class parameters
                  console.log(req + " " + answer)
                  switch(req) {
                  case reqLogin: loginDial.checkLogin(answer)
                      break;
                  default:
                  }
              }
          }
      
      }
      
      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 26 May 2016, 09:10 last edited by
      #2

      Hi @someoneinthebox and Welcome,

      Apart from the rest you also need to declare both the enums as Q_DECLARE_METATYPE.
      So inside Helper:

      class Helper {
         ...
      };
      Q_DECLARE_METATYPE(Helper::Requester)
      Q_DECLARE_METATYPE(Helper::JANSWER)
      

      157

      S 1 Reply Last reply 26 May 2016, 10:50
      1
      • P p3c0
        26 May 2016, 09:10

        Hi @someoneinthebox and Welcome,

        Apart from the rest you also need to declare both the enums as Q_DECLARE_METATYPE.
        So inside Helper:

        class Helper {
           ...
        };
        Q_DECLARE_METATYPE(Helper::Requester)
        Q_DECLARE_METATYPE(Helper::JANSWER)
        
        S Offline
        S Offline
        someoneinthebox
        wrote on 26 May 2016, 10:50 last edited by
        #3

        @p3c0, thank you so much! It works now!

        Mom said I am special.

        P 1 Reply Last reply 26 May 2016, 11:07
        0
        • S someoneinthebox
          26 May 2016, 10:50

          @p3c0, thank you so much! It works now!

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 26 May 2016, 11:07 last edited by
          #4

          @someoneinthebox You're Welcome :)
          Btw. if you are using Qt 5.5 or greater then use Q_ENUM instead as Q_ENUMS are deprecated.

          157

          1 Reply Last reply
          1

          3/4

          26 May 2016, 10:50

          • Login

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