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. Export enum from C++ into QML
Forum Updated to NodeBB v4.3 + New Features

Export enum from C++ into QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 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.
  • A Offline
    A Offline
    Alien
    wrote on 13 Mar 2018, 08:28 last edited by
    #1

    Hey,

    I want to have enum in my c++ class and use it in qml but the value of enum elements are undefined.

    My header file:

    #ifndef CENUM_TEST_H
    #define CENUM_TEST_H
    
    #include <QObject>
    
    
    
    class C_TEST : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int MODE MEMBER m_mode NOTIFY modeChanged)
    private:
        int m_mode;
    public:
        explicit C_TEST(QObject *parent = nullptr);
    
    
        enum MY_ENUM
        {
          One=0,
          Two,
          Three
        };
        Q_ENUM(MY_ENUM)
    
        int mode() const;
        void setMode(int mode);
    
    signals:
        void modeChanged();
    
    public slots:
    };
    
    #endif // CENUM_TEST_H
    

    CPP

    #include "c_test.h"
    
    int C_TEST::mode() const
    {
        return m_mode;
    }
    
    void C_TEST::setMode(int mode)
    {
        if(m_mode != mode)
        {
        m_mode = mode;
    emit modeChanged();
        }
    
    }
    
    C_TEST::C_TEST(QObject *parent) : QObject(parent)
    {
        setMode(2);
    }
    

    My main.cpp:

    int main(int argc, char *argv[])
    {
    #if defined(Q_OS_WIN)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
        QGuiApplication app(argc, argv);
        C_TEST test;
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("COBJECT",&test);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
        return app.exec();
    }
    
    

    finally my qml document:

    import QtQuick 2.9
    
    Rectangle
    {
        id:rectID
        property alias textField: textID
        property int mode: COBJECT.MODE
        x:10*index
        y:index*height
        width:45; height:25
        color: "red"
        Text
        {
            anchors.fill: parent
            id: textID
            text:
            {
                switch(mode)
                {
                case COBJECT.One:
                    qsTr("1")
                    break;
                case COBJECT.Two:
                    qsTr("2")
                    break;
                case COBJECT.Three:
                    qsTr("3")
                    break;
                default:
                    qsTr("NONE");
                }
            }
        }
    
        Item
        {
          focus: true
          Keys.onPressed:
          {
            rectID[index].textField.text = "hey"
          }
        }
    }
    
    

    Extra Information:

    OS:Ubuntu 16.04LTS
    QT SDK: 5.10.0
    QT CREATOR:4.5.0

    I appreciate your help.
    Yours,

    J B 2 Replies Last reply 13 Mar 2018, 08:37
    0
    • A Alien
      13 Mar 2018, 08:28

      Hey,

      I want to have enum in my c++ class and use it in qml but the value of enum elements are undefined.

      My header file:

      #ifndef CENUM_TEST_H
      #define CENUM_TEST_H
      
      #include <QObject>
      
      
      
      class C_TEST : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(int MODE MEMBER m_mode NOTIFY modeChanged)
      private:
          int m_mode;
      public:
          explicit C_TEST(QObject *parent = nullptr);
      
      
          enum MY_ENUM
          {
            One=0,
            Two,
            Three
          };
          Q_ENUM(MY_ENUM)
      
          int mode() const;
          void setMode(int mode);
      
      signals:
          void modeChanged();
      
      public slots:
      };
      
      #endif // CENUM_TEST_H
      

      CPP

      #include "c_test.h"
      
      int C_TEST::mode() const
      {
          return m_mode;
      }
      
      void C_TEST::setMode(int mode)
      {
          if(m_mode != mode)
          {
          m_mode = mode;
      emit modeChanged();
          }
      
      }
      
      C_TEST::C_TEST(QObject *parent) : QObject(parent)
      {
          setMode(2);
      }
      

      My main.cpp:

      int main(int argc, char *argv[])
      {
      #if defined(Q_OS_WIN)
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      #endif
          QGuiApplication app(argc, argv);
          C_TEST test;
          QQmlApplicationEngine engine;
          engine.rootContext()->setContextProperty("COBJECT",&test);
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
          return app.exec();
      }
      
      

      finally my qml document:

      import QtQuick 2.9
      
      Rectangle
      {
          id:rectID
          property alias textField: textID
          property int mode: COBJECT.MODE
          x:10*index
          y:index*height
          width:45; height:25
          color: "red"
          Text
          {
              anchors.fill: parent
              id: textID
              text:
              {
                  switch(mode)
                  {
                  case COBJECT.One:
                      qsTr("1")
                      break;
                  case COBJECT.Two:
                      qsTr("2")
                      break;
                  case COBJECT.Three:
                      qsTr("3")
                      break;
                  default:
                      qsTr("NONE");
                  }
              }
          }
      
          Item
          {
            focus: true
            Keys.onPressed:
            {
              rectID[index].textField.text = "hey"
            }
          }
      }
      
      

      Extra Information:

      OS:Ubuntu 16.04LTS
      QT SDK: 5.10.0
      QT CREATOR:4.5.0

      I appreciate your help.
      Yours,

      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 13 Mar 2018, 08:37 last edited by J.Hilk
      #2

      @Alien hi,
      IIRC you'll have to register the enums seperately in your main.cpp using qmlRegisterUncreatableType

      something along this:

      qmlRegisterUncreatableType<C_TEST >("CppEnums",1,0,"Enums","Enum is not a type");
      
      //qml
      import CppEnums 1.0
      ...
      Text
          {
              anchors.fill: parent
              id: textID
              text:
              {
                  switch(mode)
                  {
                  case Enums.One:
                      qsTr("1")
                      break;
                  case Enums.Two:
                      qsTr("2")
                      break;
                  case Enums.Three:
                      qsTr("3")
                      break;
                  default:
                      qsTr("NONE");
                  }
              }
          }
      
      

      you can also make your Q_Property directly of your enum type without the cast into int

      Q_PROPERTY(MY_ENUM MODE MEMBER m_mode NOTIFY modeChanged)
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Alien
        wrote on 13 Mar 2018, 09:27 last edited by
        #3

        Thank you for your solution that works fine but I think that I've to use both registration please look at the below code

        engine.rootContext()->setContextProperty("COBJECT",&test);
        qmlRegisterUncreatableType<C_TEST >("CppEnums",1,0,"Enums","Enum is not a type");
        

        Since I need the mode variable (this variable will change time to time)
        does this usage have any problems?

        J 1 Reply Last reply 13 Mar 2018, 09:37
        0
        • A Alien
          13 Mar 2018, 09:27

          Thank you for your solution that works fine but I think that I've to use both registration please look at the below code

          engine.rootContext()->setContextProperty("COBJECT",&test);
          qmlRegisterUncreatableType<C_TEST >("CppEnums",1,0,"Enums","Enum is not a type");
          

          Since I need the mode variable (this variable will change time to time)
          does this usage have any problems?

          J Online
          J Online
          J.Hilk
          Moderators
          wrote on 13 Mar 2018, 09:37 last edited by
          #4

          @Alien said in Export enum from C++ into QML:

          Thank you for your solution that works fine but I think that I've to use both registration please look at the below code

          engine.rootContext()->setContextProperty("COBJECT",&test);
          qmlRegisterUncreatableType<C_TEST >("CppEnums",1,0,"Enums","Enum is not a type");
          

          Since I need the mode variable (this variable will change time to time)

          you need the contxtProperty too, the qmlRegisterUncreatableType just makes your Enum(-class) available to your qml files you still need a reference object from your cpp-backend.

          does this usage have any problems?

          I don't think so, I use it the same way.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • A Alien
            13 Mar 2018, 08:28

            Hey,

            I want to have enum in my c++ class and use it in qml but the value of enum elements are undefined.

            My header file:

            #ifndef CENUM_TEST_H
            #define CENUM_TEST_H
            
            #include <QObject>
            
            
            
            class C_TEST : public QObject
            {
                Q_OBJECT
                Q_PROPERTY(int MODE MEMBER m_mode NOTIFY modeChanged)
            private:
                int m_mode;
            public:
                explicit C_TEST(QObject *parent = nullptr);
            
            
                enum MY_ENUM
                {
                  One=0,
                  Two,
                  Three
                };
                Q_ENUM(MY_ENUM)
            
                int mode() const;
                void setMode(int mode);
            
            signals:
                void modeChanged();
            
            public slots:
            };
            
            #endif // CENUM_TEST_H
            

            CPP

            #include "c_test.h"
            
            int C_TEST::mode() const
            {
                return m_mode;
            }
            
            void C_TEST::setMode(int mode)
            {
                if(m_mode != mode)
                {
                m_mode = mode;
            emit modeChanged();
                }
            
            }
            
            C_TEST::C_TEST(QObject *parent) : QObject(parent)
            {
                setMode(2);
            }
            

            My main.cpp:

            int main(int argc, char *argv[])
            {
            #if defined(Q_OS_WIN)
                QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            #endif
                QGuiApplication app(argc, argv);
                C_TEST test;
                QQmlApplicationEngine engine;
                engine.rootContext()->setContextProperty("COBJECT",&test);
                engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                if (engine.rootObjects().isEmpty())
                    return -1;
                return app.exec();
            }
            
            

            finally my qml document:

            import QtQuick 2.9
            
            Rectangle
            {
                id:rectID
                property alias textField: textID
                property int mode: COBJECT.MODE
                x:10*index
                y:index*height
                width:45; height:25
                color: "red"
                Text
                {
                    anchors.fill: parent
                    id: textID
                    text:
                    {
                        switch(mode)
                        {
                        case COBJECT.One:
                            qsTr("1")
                            break;
                        case COBJECT.Two:
                            qsTr("2")
                            break;
                        case COBJECT.Three:
                            qsTr("3")
                            break;
                        default:
                            qsTr("NONE");
                        }
                    }
                }
            
                Item
                {
                  focus: true
                  Keys.onPressed:
                  {
                    rectID[index].textField.text = "hey"
                  }
                }
            }
            
            

            Extra Information:

            OS:Ubuntu 16.04LTS
            QT SDK: 5.10.0
            QT CREATOR:4.5.0

            I appreciate your help.
            Yours,

            B Offline
            B Offline
            beecksche
            wrote on 13 Mar 2018, 11:07 last edited by beecksche
            #5
            This post is deleted!
            1 Reply Last reply
            0

            1/5

            13 Mar 2018, 08:28

            • Login

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