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. Qt and scripting

Qt and scripting

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.7k Views 1 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
    medvedm
    wrote on last edited by
    #1

    I have a bunch of classes which inherit from QObject that have a bunch of Q_PROPERTY members which represent commands my application can receive. I have a class which waits on a socket, gets some bytes, and then turns the bytes into one of my command classes. This controller then emits a signal so that other parts of the program can respond to the commands that come in (the command is passed in the signal).

    Is there a way using one of the various scripting engines (QScript, QJSEngine) that I can easily script inserting different sets of commands? I'd like to be able to say in the script:

    Make X Command
    X.param1 = 3
    X.param2 = 27

    Make Y Command
    Y.param1 = foo

    Send X
    Send Y

    Or something like that.

    Everything I've found to read about the script engines is related to QML and GUIs, which are not what I am doing here...

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      QML is simply a CSS- like language with full JavaScript support. It is mainly used to create GUIs, but it can certainly be used for other things too.

      QML makes an excellent bridge between QObjects and JavaScript -- it is tightly integrated with the Qt object model so it has full access to your Q_PROPERTY members. Through it, you can easily manipulate your QObjects using JavaScript.

      QJSEngine is a low-level component of the QML engine; writing QML code gives you more power than using QJSEngine directly. Qt Script is now deprecated so I don't recommend using that.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

        Can you please explain how, via QML, I can tie objects in my C++ code w/ Q_PROPERTY members to JS? I guess I don't get it.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          use QtScript module. Taken from the "docs":http://qt-project.org/doc/qt-5.0/qtscript/qtscript-index.html you could do this:
          @
          QScriptEngine engine;
          QObject *someObject = new MyObject;
          QScriptValue objectValue = engine.newQObject(someObject); //expose your object to the script so you can access it from your scripts
          engine.globalObject().setProperty("myObject", objectValue);

          engine.evaluate(....); //<-- pass your script
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • M Offline
            M Offline
            medvedm
            wrote on last edited by
            #5

            Yeah, except you'll notice in this case you have to instantiate the class outside of the script and then pass it in. I want the script to inst the class.

            Also, I agree w/ the prev poster - QtScript sounds like it has lost favor to QJSEngine.

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              [quote author="medvedm" date="1381411337"]Yeah, except you'll notice in this case you have to instantiate the class outside of the script and then pass it in. I want the script to inst the class.
              [/quote]

              ok, then you will need to expose the class structure (QMetaObject) to QtScript.

              @
              QScriptValue myScriptClass = engine.scriptValueFromQMetaObject<MyClass>();
              engine.globalObject().setProperty("MyClass", myScriptClass);
              @

              Then you can use in your script:
              @
              var x = new MyClass;
              @

              But read this for "details":http://qt-project.org/doc/qt-5.0/qtscript/qtscript-index.html#making-a-qobject-based-class-new-able-from-a-script.

              [quote author="medvedm" date="1381411337"]
              Also, I agree w/ the prev poster - QtScript sounds like it has lost favor to QJSEngine.
              [/quote]
              QtScript is part of Qt core. Also QML uses it...

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                [quote author="raven-worx" date="1381412701"]QtScript is part of Qt core. Also QML uses it...[/quote]No, Qt Script is NOT part of Qt Core.

                Qt Quick 1 (deprecated, also known as Qt Declarative) uses Qt Script. Qt Quick 2 does not.

                http://qt-project.org/doc/qt-5.1/qtdoc/addons.html

                "Qt Script: This module is primarily provided for compatibility with Qt 4, and to support applications that use the Qt Declarative Module. An alternative API is provided by the essential Qt Qml Module."

                [quote author="medvedm" date="1381408092"]Can you please explain how, via QML, I can tie objects in my C++ code w/ Q_PROPERTY members to JS? I guess I don't get it.[/quote]How to convert C++ objects into QML objects:

                • http://qt-project.org/doc/qt-5.1/qtqml/referenceexamples-adding.html
                • http://qt-project.org/doc/qt-5.1/qtqml/referenceexamples-adding-main-cpp.html

                How to run JavaScript code in QML:

                • http://qt-project.org/doc/qt-5.1/qtdoc/qtquick-usecase-integratingjs.html

                How to create QML objects in JavaScript

                • http://qt-project.org/doc/qt-5.1/qtqml/qtqml-javascript-dynamicobjectcreation.html

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  [quote author="JKSH" date="1381418341"]No, Qt Script is NOT part of Qt Core.[/quote]
                  oops...you are right. My fault sorry.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  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