Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Passing arguments to the QJSEngine

    General and Desktop
    4
    7
    1248
    Loading More Posts
    • 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
      Lolo67 last edited by

      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());
      
      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @Lolo67 last edited by JonB

        @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 Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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 Reply Quote 1
          • mrjj
            mrjj Lifetime Qt Champion last edited by mrjj

            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 Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 Reply Quote 1
              • mrjj
                mrjj Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • L
                  Lolo67 last edited by

                  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 Reply Quote 0
                  • First post
                    Last post