Qt and scripting



  • 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...


  • Moderators

    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.



  • 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.


  • Moderators

    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
    @



  • 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.


  • Moderators

    [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...


  • Moderators

    [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:

    How to run JavaScript code in QML:

    How to create QML objects in JavaScript


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.