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. Pass enum into QML and use it in JavaScript. How?
Forum Update on Monday, May 27th 2025

Pass enum into QML and use it in JavaScript. How?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 881 Views
  • 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by
    #1

    Hello!
    I need to get enum from C++ into QML:

    class SomeClass : public QObject {
    
    	Q_OBJECT
    
    	public:
    
    		enum Types {
    			Type0 = 0,
    			Type1 = 1,
    			Type2 = 2
    		}; Q_ENUM(Types)
    };
    

    How to get it into QML and use it JavaScript?

    ODБOïO 1 Reply Last reply
    0
    • B bogong

      Hello!
      I need to get enum from C++ into QML:

      class SomeClass : public QObject {
      
      	Q_OBJECT
      
      	public:
      
      		enum Types {
      			Type0 = 0,
      			Type1 = 1,
      			Type2 = 2
      		}; Q_ENUM(Types)
      };
      

      How to get it into QML and use it JavaScript?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      @bogong

      class SomeClass : public QObject {
      
      	Q_OBJECT
              Q_PROPERTY(Types  type READ type WRITE setType NOTIFY typeChanged) //<
      
      	public:
      
      		enum class Types  : int{  //<
      			Type0 = 0,
      			Type1 = 1,
      			Type2 = 2
      		}; Q_ENUM(Types)
      
         Types  type()const{
              return m_type;
          }
          void setType(const Types &t){
              if(m_type!=s){
                  m_type=t;
                  emit typeChanged(m_type);
              }
          }
      signal : 
      void typeChanged(Types t);
      
       private :
         Types m_type  = Type0; //<
      
      }; 
      Q_DECLARE_METATYPE(SomeClass::Types ) //<
      

      //main.cpp

         SomeClass obj; // create class obj
         QQmlApplicationEngine engine;
         engine.rootContext()->setContextProperty("myObj",&obj); // make it known in QML
      
      
         qmlRegisterType<SomeClass>("com.my.comp", 1, 0, "SomeClass"); // register the enum
         
      

      //qml js

      import com.my.comp 1.0
      
      ..
      Image{
       source : myObj.type === SomeClass.Type0 ? "/typeZeroImg/img.png" : "autherImg/img.png"
      }
      
      B 1 Reply Last reply
      1
      • ODБOïO ODБOï

        @bogong

        class SomeClass : public QObject {
        
        	Q_OBJECT
                Q_PROPERTY(Types  type READ type WRITE setType NOTIFY typeChanged) //<
        
        	public:
        
        		enum class Types  : int{  //<
        			Type0 = 0,
        			Type1 = 1,
        			Type2 = 2
        		}; Q_ENUM(Types)
        
           Types  type()const{
                return m_type;
            }
            void setType(const Types &t){
                if(m_type!=s){
                    m_type=t;
                    emit typeChanged(m_type);
                }
            }
        signal : 
        void typeChanged(Types t);
        
         private :
           Types m_type  = Type0; //<
        
        }; 
        Q_DECLARE_METATYPE(SomeClass::Types ) //<
        

        //main.cpp

           SomeClass obj; // create class obj
           QQmlApplicationEngine engine;
           engine.rootContext()->setContextProperty("myObj",&obj); // make it known in QML
        
        
           qmlRegisterType<SomeClass>("com.my.comp", 1, 0, "SomeClass"); // register the enum
           
        

        //qml js

        import com.my.comp 1.0
        
        ..
        Image{
         source : myObj.type === SomeClass.Type0 ? "/typeZeroImg/img.png" : "autherImg/img.png"
        }
        
        B Offline
        B Offline
        bogong
        wrote on last edited by bogong
        #3

        @LeLev said in Pass enum into QML and use it in JavaScript. How?:

        Q_DECLARE_METATYPE(SomeClass::Types )

        This forgotten for me. Thx. Issue closed.
        And it's not working if it inside of Singletone class because of disabled default constructor.

        J.HilkJ 1 Reply Last reply
        0
        • B bogong

          @LeLev said in Pass enum into QML and use it in JavaScript. How?:

          Q_DECLARE_METATYPE(SomeClass::Types )

          This forgotten for me. Thx. Issue closed.
          And it's not working if it inside of Singletone class because of disabled default constructor.

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @bogong don't forget to set the topic to solved, when it's solved 😉


          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

          • Login

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