Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJSEngine, how to expose C++ enum to a JavaScript?
Forum Updated to NodeBB v4.3 + New Features

QJSEngine, how to expose C++ enum to a JavaScript?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.4k 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.
  • A Offline
    A Offline
    Alhasni
    wrote on last edited by
    #1

    I managed to expose an (uninstantiable) class to JavaScript using qmlRegisterType as follows:

    class MyClass : public QObject {
    	Q_OBJECT
    public:
    	MyClass ()  : _name("MyClass") { }
    
    	Q_INVOKABLE void setName(QString name) { _name = name; }
    
    	Q_INVOKABLE QString getName() { return _name; }
    
    	enum MyEnum { Entry1, Entry2 };
    
    private:
    	QString _name;
    };
    
    // registered using qmlRegisterType<MyClass>()
    

    But I cannot access MyEnum. Looking online there are only examples on how to do it with QML using import.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Did you try using Q_ENUM ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      A 1 Reply Last reply
      3
      • dheerendraD dheerendra

        Did you try using Q_ENUM ?

        A Offline
        A Offline
        Alhasni
        wrote on last edited by
        #3

        @dheerendra I added Q_ENUM(MyEnum) after the enum declaration but that didn't work.

        When I try to access it using m.Entry1 where m is an instance of MyClass I instantiate from a global object, I just get an "undefined" value.

        Note that I also cannot access it using MyClass.Entry1, I get the error:
        Uncaught exception at line 5: ReferenceError: MyClass is not defined

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Can you register your class like this ?

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

          Now accès it as MyEnums.Entry1

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          A 1 Reply Last reply
          1
          • dheerendraD dheerendra

            Can you register your class like this ?

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

            Now accès it as MyEnums.Entry1

            A Offline
            A Offline
            Alhasni
            wrote on last edited by
            #5

            @dheerendra

            Still doesn't work unfortunately.

            class MyClass : public QObject {
            	Q_OBJECT
            public:
            	MyClass() : _name("MyClass") { }
            
            	Q_INVOKABLE void setName(QString name) { _name = name; }
            
            	Q_INVOKABLE QString getName() { return _name; }
            
            	enum MyEnum { Entry1, Entry2 };
            	Q_ENUM(MyEnum)
            
            private:
            	QString _name;
            };
            
            // registered using qmlRegisterType<MyClass>("MyApp", 1, 0, "MyEnums");
            

            Evaluate:

            let c = controller.createClass();
            c.getName();
            MyEnums.Entry1
            

            Error: Uncaught exception at line 4: ReferenceError: MyEnums is not defined

            With QML, the examples would also import the registered type when running the script, but that doesnt work with QJSEngine...

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              Where did you include the qmlRegisterType ? Did you make the import statements in qml ?

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Alhasni
                wrote on last edited by Alhasni
                #7

                I don't understand the question. I am using QJSEngine, not QQMLEngine.

                I use qmlRegisterType like this:

                QJSEngine _jsEngine;
                
                QJSValue objectValue = _jsEngine.newQObject(_controller);
                _jsEngine.globalObject().setProperty("controller", objectValue);
                
                _jsEngine.installExtensions(QJSEngine::Extension::ConsoleExtension);
                
                qmlRegisterType<MyClass>("MyApp", 1, 0, "MyEnums");
                
                QJSValue result = _jsEngine.evaluate(script->toPlainText());
                
                Gojir4G 1 Reply Last reply
                0
                • A Alhasni

                  I don't understand the question. I am using QJSEngine, not QQMLEngine.

                  I use qmlRegisterType like this:

                  QJSEngine _jsEngine;
                  
                  QJSValue objectValue = _jsEngine.newQObject(_controller);
                  _jsEngine.globalObject().setProperty("controller", objectValue);
                  
                  _jsEngine.installExtensions(QJSEngine::Extension::ConsoleExtension);
                  
                  qmlRegisterType<MyClass>("MyApp", 1, 0, "MyEnums");
                  
                  QJSValue result = _jsEngine.evaluate(script->toPlainText());
                  
                  Gojir4G Offline
                  Gojir4G Offline
                  Gojir4
                  wrote on last edited by
                  #8

                  Hi @Alhasni,

                  According to https://doc.qt.io/qt-5/qjsengine.html#qobject-integration I guess you need to use QJSEngine::newQMetaObject():

                  QJSValue enumObjectValue = _jsEngine.newQMetaObject(_controller->metaObject());
                  _jsEngine.globalObject().setProperty("controller", enumObjectValue);
                  
                  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