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. What can and can't you do in a QJSEngine function?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.4k Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    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.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #7

      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*.

      Kind Regards,
      Sy

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #2

        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.

        SPlattenS 1 Reply Last reply
        1
        • mrjjM mrjj

          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.

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

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

          Kind Regards,
          Sy

          mrjjM 1 Reply Last reply
          0
          • SPlattenS SPlatten

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @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.

            1 Reply Last reply
            1
            • SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              Thanks again, I’ll certainly look into it.

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by SPlatten
                #6

                @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?

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #7

                  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*.

                  Kind Regards,
                  Sy

                  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