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.2k 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 SherifOmran

    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

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #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 last edited by SherifOmran
      #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 !

      jsulmJ 1 Reply Last reply
      0
      • S SherifOmran

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

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on 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
        1
        • jsulmJ jsulm

          @SherifOmran What if you do

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

          ?

          S Offline
          S Offline
          SherifOmran
          wrote on last edited by
          #5

          @jsulm same result except /n is removed

          jsulmJ 1 Reply Last reply
          0
          • S SherifOmran

            @jsulm same result except /n is removed

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on 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 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

              JonBJ jsulmJ 2 Replies Last reply
              0
              • S SherifOmran

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #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

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

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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 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.

                    JonBJ 1 Reply Last reply
                    0
                    • S SherifOmran

                      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.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #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
                      1
                      • JonBJ JonB

                        @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 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
                        0
                        • S SherifOmran

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

                          • Login

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