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. Passing arguments to the QJSEngine
QtWS25 Last Chance

Passing arguments to the QJSEngine

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.6k 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.
  • L Offline
    L Offline
    Lolo67
    wrote on last edited by
    #1

    I am trying to pass a list of arguments to a javascript function via the QJSEngine.
    The following code will display "The passed amount of arguments is 15" i.e. the length of the first argument passed - looking further I can see that args[0] contains "M", args[1] contains "Y", args[2] contains "F",....,args[15] contains "T" while I was awaiting something like "The passed amount of arguments is 3" where args[0] contains "MYFIRSTARGUMENT", args[1] contains "A", args[2] contains "B".

    QJSValueList args;
    args << "MYFIRSTARGUMENT";
    args << "A";
    args << "B";
    
    QJSValue fun = m_jsEngine->evaluate("(function(args) { return args.length; })");
    QJSValue result = fun.call(args);
    qDebug() << QString("The passed amount of arguments is %1").arg(result.toInt());
    
    JonBJ 1 Reply Last reply
    0
    • L Lolo67

      I am trying to pass a list of arguments to a javascript function via the QJSEngine.
      The following code will display "The passed amount of arguments is 15" i.e. the length of the first argument passed - looking further I can see that args[0] contains "M", args[1] contains "Y", args[2] contains "F",....,args[15] contains "T" while I was awaiting something like "The passed amount of arguments is 3" where args[0] contains "MYFIRSTARGUMENT", args[1] contains "A", args[2] contains "B".

      QJSValueList args;
      args << "MYFIRSTARGUMENT";
      args << "A";
      args << "B";
      
      QJSValue fun = m_jsEngine->evaluate("(function(args) { return args.length; })");
      QJSValue result = fun.call(args);
      qDebug() << QString("The passed amount of arguments is %1").arg(result.toInt());
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Lolo67
      Where does the documentation actually define QJSValueList? I see it used at http://doc.qt.io/qt-5/qjsvalue.html#call

      QJSValue QJSValue::call(const QJSValueList &args = QJSValueList())

      but it's not a hyperlink.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        It's a typedef of QList<QJSValue>

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

          Hi
          Im have not used this class so this is only a note.
          The doc sample seems to have extra parameter to the call function which seems to
          relate to your issue that only the first value of the list is used for arg.

          Doc sample

          QJSValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
          QJSValueList args;
          args << 1 << 2;
          QJSValue threeAgain = fun.call(QJSValue(), args);
          

          However the call is defined as
          QJSValue QJSValue::call(const QJSValueList & args = QJSValueList())

          So maybe sample is just wrong.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            A documentation fix is on its way.

            If you remove the QJSValue() from the parameters, the example works as expected.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

              Hi
              Ok, rare case of wrong docs :)

              So the reason that the OPs code is not working is that he need to specify the parameters separately and
              the arg is not/ cannot be used as a list ?
              like
              m_jsEngine->evaluate("(function(a1,a2, a3) { })");

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Lolo67
                wrote on last edited by
                #7

                By the way the same effect can be observed when using QScriptEngine

                QScriptEngine *m_ScriptEngine = new QScriptEngine(this);
                QScriptValueList scriptArgs;
                scriptArgs << "MYFIRSTARGUMENT";
                scriptArgs << "A";
                scriptArgs << "B";
                
                QScriptValue  fun = m_ScriptEngine->evaluate("(function(args) { return args.length; })");
                QScriptValue  result = fun.call(QScriptValue(), scriptArgs);
                qDebug() << QString("The passed amount of arguments is %1").arg(result.toInt32());
                delete m_ScriptEngine;
                
                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