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. calling javascript function from Qt Console application
Forum Updated to NodeBB v4.3 + New Features

calling javascript function from Qt Console application

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 2.1k 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.
  • S Offline
    S Offline
    SherifOmran
    wrote on 2 Mar 2019, 09:42 last edited by aha_1980 3 Mar 2019, 16:56
    #1

    I have a js function that i need to evaluate from qt console application, and i am trying to learnt the QScriptEngine

    so far i could read my function

    double a;
    a=engine.evaluate("(myfunc(2,3))").toNumber();
    

    in my simplejsfn.js i have

    function myfunc(p1, p2) { return p1*p2; }
    

    but i dont get the expected output

    J 1 Reply Last reply 2 Mar 2019, 09:51
    0
    • S SherifOmran
      2 Mar 2019, 09:42

      I have a js function that i need to evaluate from qt console application, and i am trying to learnt the QScriptEngine

      so far i could read my function

      double a;
      a=engine.evaluate("(myfunc(2,3))").toNumber();
      

      in my simplejsfn.js i have

      function myfunc(p1, p2) { return p1*p2; }
      

      but i dont get the expected output

      J Offline
      J Offline
      JonB
      wrote on 2 Mar 2019, 09:51 last edited by JonB 3 Feb 2019, 09:53
      #2

      @SherifOmran

      but i dont get the expected output

      It always helps then if you say what you do get as the output!! (E.g. for all I know, you haven't included your simplejsfn.js, so the function isn't defined.)

      Although I know nothing about this, why not start out from a simpler, non-function expression, e.g. as per the docs example myEngine.evaluate("1 + 2");. Does that work?

      Then the docs go on to give a simple function example:

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

      Does that work?

      That's how I'd approach it....

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SherifOmran
        wrote on 3 Mar 2019, 16:39 last edited by SherifOmran 3 Mar 2019, 16:43
        #3

        @JonB said in calling javascript function from Qt Consolt application:

        QScriptValue threeAgain = fun.call(QScriptValue(), args);

        I read the js function from a js file

        QScriptEngine engine;
              QString fileName(":/simplejsfn.js");
              QFile scriptFile(fileName);
              scriptFile.open(QIODevice::ReadOnly);
              QTextStream stream(&scriptFile);
              QString contents = stream.readAll();
              scriptFile.close();
        
              QScriptValue fun = engine.evaluate(contents);
              QScriptValueList args;
              args << 1 << 2;
              QScriptValue threeAgain = fun.call(QScriptValue(), args);
        

        The js file has the following

        function myfunc(p1, p2) { return p1*p2; }
        

        while debugging i see the qstring in content with the following

        "function myfunc(p1, p2) { return p1*p2; }\n"
        

        The value of threeAgain is invalid ..

        When i write the function as you do inside the evaluate it works, however when the function is inside a QString it does not work !

        J 1 Reply Last reply 4 Mar 2019, 05:45
        0
        • S SherifOmran
          3 Mar 2019, 16:39

          @JonB said in calling javascript function from Qt Consolt application:

          QScriptValue threeAgain = fun.call(QScriptValue(), args);

          I read the js function from a js file

          QScriptEngine engine;
                QString fileName(":/simplejsfn.js");
                QFile scriptFile(fileName);
                scriptFile.open(QIODevice::ReadOnly);
                QTextStream stream(&scriptFile);
                QString contents = stream.readAll();
                scriptFile.close();
          
                QScriptValue fun = engine.evaluate(contents);
                QScriptValueList args;
                args << 1 << 2;
                QScriptValue threeAgain = fun.call(QScriptValue(), args);
          

          The js file has the following

          function myfunc(p1, p2) { return p1*p2; }
          

          while debugging i see the qstring in content with the following

          "function myfunc(p1, p2) { return p1*p2; }\n"
          

          The value of threeAgain is invalid ..

          When i write the function as you do inside the evaluate it works, however when the function is inside a QString it does not work !

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 4 Mar 2019, 05:45 last edited by
          #4

          @SherifOmran What if you do

          QString contents = stream.readAll().trimmed();
          

          ?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply 4 Mar 2019, 07:30
          1
          • J jsulm
            4 Mar 2019, 05:45

            @SherifOmran What if you do

            QString contents = stream.readAll().trimmed();
            

            ?

            S Offline
            S Offline
            SherifOmran
            wrote on 4 Mar 2019, 07:30 last edited by
            #5

            @jsulm same result except /n is removed

            J 1 Reply Last reply 4 Mar 2019, 07:36
            0
            • S SherifOmran
              4 Mar 2019, 07:30

              @jsulm same result except /n is removed

              J Online
              J Online
              jsulm
              Lifetime Qt Champion
              wrote on 4 Mar 2019, 07:36 last edited by
              #6

              @SherifOmran Did you try to call https://doc.qt.io/qt-5/qscriptengine.html#uncaughtException to see whether there is an exception and if so what it is?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • S Offline
                S Offline
                SherifOmran
                wrote on 4 Mar 2019, 09:14 last edited by
                #7

                i did not try because this is a very simple example. I took another approach to create a browser and load the js file

                J J 2 Replies Last reply 4 Mar 2019, 09:20
                0
                • S SherifOmran
                  4 Mar 2019, 09:14

                  i did not try because this is a very simple example. I took another approach to create a browser and load the js file

                  J Offline
                  J Offline
                  JonB
                  wrote on 4 Mar 2019, 09:20 last edited by JonB 3 Apr 2019, 09:21
                  #8

                  @SherifOmran
                  Creating a browser, if you do not want it for other purposes, just to evaluate some JS is a very "heavyweight" solution. Just saying....

                  1 Reply Last reply
                  1
                  • S SherifOmran
                    4 Mar 2019, 09:14

                    i did not try because this is a very simple example. I took another approach to create a browser and load the js file

                    J Online
                    J Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on 4 Mar 2019, 12:55 last edited by
                    #9

                    @SherifOmran You're adding a browser because it is a "very simple example"?!
                    In my opinion it would be way faster and easier to check for exceptions instead (you should do this anyway)...

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    3
                    • S Offline
                      S Offline
                      SherifOmran
                      wrote on 4 Mar 2019, 16:52 last edited by
                      #10

                      yes that is true, and it will be heavy, but i intent to run very complicated js file and if i can not run a simple test function, how will it be with a complicated script !
                      If any body can provide me a working example, this would be great.

                      J 1 Reply Last reply 4 Mar 2019, 19:54
                      0
                      • S SherifOmran
                        4 Mar 2019, 16:52

                        yes that is true, and it will be heavy, but i intent to run very complicated js file and if i can not run a simple test function, how will it be with a complicated script !
                        If any body can provide me a working example, this would be great.

                        J Offline
                        J Offline
                        JonB
                        wrote on 4 Mar 2019, 19:54 last edited by JonB 3 Apr 2019, 19:58
                        #11

                        @SherifOmran

                        qDebug() << contents;
                              QScriptValue fun = engine.evaluate(contents);
                        qDebug() << engine.hasUncaughtException();
                              QScriptValueList args;
                              args << 1 << 2;
                              QScriptValue threeAgain = fun.call(QScriptValue(), args);
                        qDebug() << engine.hasUncaughtException();
                        qDebug() << threeAgain.isValid();
                        

                        would be an improvement which would take you 10 seconds.

                        S 1 Reply Last reply 5 Mar 2019, 14:22
                        1
                        • J JonB
                          4 Mar 2019, 19:54

                          @SherifOmran

                          qDebug() << contents;
                                QScriptValue fun = engine.evaluate(contents);
                          qDebug() << engine.hasUncaughtException();
                                QScriptValueList args;
                                args << 1 << 2;
                                QScriptValue threeAgain = fun.call(QScriptValue(), args);
                          qDebug() << engine.hasUncaughtException();
                          qDebug() << threeAgain.isValid();
                          

                          would be an improvement which would take you 10 seconds.

                          S Offline
                          S Offline
                          SherifOmran
                          wrote on 5 Mar 2019, 14:22 last edited by
                          #12

                          @JonB said in calling javascript function from Qt Console application:

                          qDebug() << engine.hasUncaughtException();

                          Same result ! I am compiling under Mac high sierra 10.13.6

                                QString contents = "function myfunc(p1, p2) { return p1*p2; }";
                                QScriptValue fun = engine.evaluate(contents);
                                qDebug() << engine.hasUncaughtException();
                                QScriptValueList args;
                                args << 1 << 2;
                                QScriptValue threeAgain = fun.call(QScriptValue(), args);
                                qWarning() << engine.hasUncaughtException();
                                bool A= threeAgain.isValid();
                          
                          S 1 Reply Last reply 5 Mar 2019, 14:24
                          0
                          • S SherifOmran
                            5 Mar 2019, 14:22

                            @JonB said in calling javascript function from Qt Console application:

                            qDebug() << engine.hasUncaughtException();

                            Same result ! I am compiling under Mac high sierra 10.13.6

                                  QString contents = "function myfunc(p1, p2) { return p1*p2; }";
                                  QScriptValue fun = engine.evaluate(contents);
                                  qDebug() << engine.hasUncaughtException();
                                  QScriptValueList args;
                                  args << 1 << 2;
                                  QScriptValue threeAgain = fun.call(QScriptValue(), args);
                                  qWarning() << engine.hasUncaughtException();
                                  bool A= threeAgain.isValid();
                            
                            S Offline
                            S Offline
                            SherifOmran
                            wrote on 5 Mar 2019, 14:24 last edited by
                            #13

                            Please test it on your side and let me know, if this simple code works. May be this is a bug ? I am using clang under mac high sierra.

                            1 Reply Last reply
                            0

                            1/13

                            2 Mar 2019, 09:42

                            • Login

                            • Login or register to search.
                            1 out of 13
                            • First post
                              1/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved