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

What can and can't you do in a QJSEngine function?



  • I am writing a C++ Qt application, the main configuration for the application is in XML, the application itself will be an application server that can run applications written in JavaScript. I've been playing around with QJSEngine and have some very simple routines which I've implemented using globalObject and setProperty.

    Now I'm trying to progress further and write more useful functions, is there a definitive list of what 'can' and 'cannot' be done in functions ? For example one of my examples:

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

    This works fine, I then tried:

            objHelper = pobjScriptEng->evaluate("function(strFileName) {"
                                                    "QFile file(strFileName);"
                                                    "if ( !file.open(QIODevice::ReadOnly) ) {"
                                                        "while(!file.atEnd()) {"
                                                        "}"
                                                    "}"
                                                    "return 123;"
                                                "}");
            pobjScriptEng->globalObject().setProperty("readBinaryFile", objHelper);
    

    This doesn't work and I'm not sure if its even possible...the return 123 is just temporary and I wanted to see if the function could be called and return, but I never see 123 in my script that calls it.



  • I modified my script to add a try/catch clause around the call to raise.

    This resulted in an error being displayed, char* was an issue, no idea why:

    Error: Unknown method parameter type: char*
    

    I then edited the code and changed the char* parameter for QString. It now works, I've no idea why it didn't like char*.


  • Lifetime Qt Champion

    Hi
    Most javascript can work. ( non visual )
    http://doc.qt.io/qt-5/qtqml-javascript-functionlist.html

    In your case you try to use QFile which is not a javascript class.

    In Qt5.12 its ECMAScript 6-7 level.



  • @mrjj , thank you, is there a way I can provide this functionality to extend the capabilities of the script?


  • Lifetime Qt Champion

    @SPlatten
    hi
    Its possible to use QObject based classes in the script so you can wrap QFile into such
    class and use that way
    http://doc.qt.io/qt-5/qjsengine.html#script-object-creation
    The QObject Integration section.



  • Thanks again, I’ll certainly look into it.



  • @mrjj, I've created a helper class, it's not finished, but enough to test:

    Prototype:

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

    Implementation:

    clsSignalHelper::clsSignalHelper(QObject *parent) : QObject(parent) {
    }
    
    char* clsSignalHelper::raise(int intCode, char* pszJSON) {
        qDebug("clsSignalHelper::raise");
        qDebug() << intCode << pszJSON;
        return "raise method test";
    }
    

    I add this to the script object with:

     clsSignalHelper* pSignalHlpr = new clsSignalHelper();
     QJSValue signalHelper = pobjScriptEng->newQObject(pSignalHlpr);
     pobjScriptEng->globalObject().setProperty("signalHlpr", signalHelper);
    

    In my test script I have:

    console.info("signalHlpr:");
    console.info(signalHlpr);
    console.info(typeof signalHlpr);
    console.info(signalHlpr.raise);
    console.info("signalHlpr.raise:");
    console.info(signalHlpr.raise(123, "HELLO"));
    

    In the application debug I see:

    2018-12-30 09:02:24.494886+0000 XMLMPAM[1768:118682] [js] signalHlpr:
    2018-12-30 09:02:24.494902+0000 XMLMPAM[1768:118682] [js] clsSignalHelper(0x10250adf0)
    2018-12-30 09:02:24.494919+0000 XMLMPAM[1768:118682] [js] object
    2018-12-30 09:02:24.494932+0000 XMLMPAM[1768:118682] [js] function() { [code] }
    2018-12-30 09:02:24.494945+0000 XMLMPAM[1768:118682] [js] signalHlpr.raise:
    

    It doesn't call the raise function and I don't see any of the qDebug statements. What have I missed?



  • I modified my script to add a try/catch clause around the call to raise.

    This resulted in an error being displayed, char* was an issue, no idea why:

    Error: Unknown method parameter type: char*
    

    I then edited the code and changed the char* parameter for QString. It now works, I've no idea why it didn't like char*.


Log in to reply