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. Evaluate JavaScript from Qt source
Forum Updated to NodeBB v4.3 + New Features

Evaluate JavaScript from Qt source

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.9k Views 3 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.
  • M Offline
    M Offline
    moravas
    wrote on last edited by
    #1

    Hi Folks,

    can anybody explain me, how I can execute javascript from a command line application? As I saw, some QWebWidget or something like that is needed on some UI interface.
    My problem is that I would like to put some algorithm into separated javascript file due to portability and evaluate it, but I don't like any UI.

    Regards,
    Norbert

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You can evaluate it using QtScript module.

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        moravas
        wrote on last edited by p3c0
        #3

        Hi,

        thank you fort the short reply. I tried out, without too much success. I have the following sample JS function in separated file:

        .pragma library
        function func() {
            return 2 * 8;
        }
        

        and the following C++ code:

            QFile scriptFile("://compiler.js");
            scriptFile.open(QIODevice::ReadOnly);
            QString script(scriptFile.readAll());
            scriptFile.close();
        
            qDebug() << script;
        
            QScriptEngine myEngine;
            QScriptValue retVal = myEngine.evaluate(script, "://compiler.js");
            qDebug() << retVal.toString();
        

        but I'm getting the error: "SyntaxError: Parse error". Could you point me, what did I wrong?

        Regards,
        Norbert

        p.s.: sorry for the wrong highlight, I'm new to this forum design.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          I have not used QtScript myself, sorry. One thing that might help is to open the file in this mode: QFile::ReadOnly | QFile::Text

          (Z(:^

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Hi @moravas,

            .pragma library

            is QML-only syntax. You can't use this with Qt Script.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              moravas
              wrote on last edited by
              #6

              Hi,

              thank you for the help, it solved the issue. The result is "undefined" now. I don't udnerstand, but I think, it's regarding to another topic.

              Regards.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by A Former User
                #7

                The result is undefined now, because your javascript contains only a function definition. Everything works fine and undefined is the correct result. If you want to receive something else, try for example:

                function func() {
                return 2 * 8;
                }
                func() // actually call func() 
                
                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