Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt, C++, QJSEngine, how to call class object function



  • I'm working on a C++ application using Qt 5.11.2. In my code I am using the QJSEngine.

    My application is configured using XML, in the XML I define global variables which are registered by the application using 'globalObject' and the 'setProperty' function.

    I want to be able to raise signals from the script, I've searched online and cannot find any example of how to achieve this, so I have written a helper class which is very simple:

    class clsSignalHelper : public QObject {
        Q_OBJECT
    public:
        explicit Q_INVOKABLE clsSignalHelper(QObject *parent = nullptr);
        void raise(int intCode, char* pszJSON);
    
    signals:
    
    public slots:
    };
    

    I've registered the above using:

            QObject* pobjSignalHelper = new clsSignalHelper();
            QJSValue objSigValue = pobjScriptEng->newQObject(pobjSignalHelper);
            pobjScriptEng->globalObject().setProperty("signal", objSigValue);
    

    'pobjScriptEng' is a pointer to an instance of QJSEngine. In my demonstration script I have:

    function connect() {
    console.info("-----------");
    console.info("a: " + a + ", typeof: " + typeof a);
    a++;
    console.info("b: " + b + ", typeof: " + typeof b);
    console.info("c: " + c + ", typeof: " + typeof c);
    console.info("c: " + c + ", typeof: " + typeof c);
    console.info("c[a]: " + c['a'] + ", typeof: " + typeof c['a']);
    console.info("c[b]: " + c['b'] + ", typeof: " + typeof c['b']);
    console.info("c[c][aa]: " + c['c']['aa'] + ", typeof: " + typeof c['c']['aa']);
    console.info("-----------");	
    
    console.info("signal:");
    console.info(signal);
    signal.raise(99, "Hello World");	
    }
    

    The above displays:

    2018-12-29 09:50:57.520934+0000 XMLMPAM[1971:241155] [js] -----------
    2018-12-29 09:50:57.521004+0000 XMLMPAM[1971:241155] [js] a: 123, typeof: number
    2018-12-29 09:50:57.521040+0000 XMLMPAM[1971:241155] [js] b: hello, typeof: string
    2018-12-29 09:50:57.521115+0000 XMLMPAM[1971:241155] [js] c: [object Object], typeof: object
    2018-12-29 09:50:57.521171+0000 XMLMPAM[1971:241155] [js] c: [object Object], typeof: object
    2018-12-29 09:50:57.521197+0000 XMLMPAM[1971:241155] [js] c[a]: 1, typeof: number
    2018-12-29 09:50:57.521250+0000 XMLMPAM[1971:241155] [js] c[b]: A, typeof: string
    2018-12-29 09:50:57.521301+0000 XMLMPAM[1971:241155] [js] c[c][aa]: 1, typeof: number
    2018-12-29 09:50:57.521346+0000 XMLMPAM[1971:241155] [js] -----------
    2018-12-29 09:50:57.521371+0000 XMLMPAM[1971:241155] [js] signal:
    2018-12-29 09:50:57.521450+0000 XMLMPAM[1971:241155] [js] clsSignalHelper(0x10a40c3c0)
    

    Most of the above is just verifying that my globals are visible, the last two lines show the helper object has been registered and is visible, but there is nothing happens 'signal.raise' is called, I've put a breakpoint in the raise function and it never gets hit. What haven't I done?



  • Fixed, in my Qt application:

            QJSValue objSigValue = pobjScriptEng->evaluate("function(a, b) {"
                                                           "return a + \" - \" + b;}");
            pobjScriptEng->globalObject().setProperty("signal", objSigValue);
    

    Then in my script:

    console.info("signal:");
    var r = signal("AAAA", "NNNNN");
    console.info(r);
    

    Ignore the name signal, that was just something I called the function before I changed its functionality.


Log in to reply