Results 1 to 20 of 37

Thread: Qt 5.4 - Scriptable Qml Application

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt 5.4 - Scriptable Qml Application

    Hi all

    I want to learn how to make a qml application scriptable. I would like to have user defined scripts that can be changed without having to recompile the app. Also, I would like to create an object and call a function from anywhere in javascript. During researching the topic I found some webpages mentioning that QtScript is no longer maintained. Is that true? What is the recommanded way to make a qml-application fully scriptable in qt5.4 (for qml applications)?


    Some background:
    At first I thought, that there is already a javascript functionality in qml. So, I started to write some javascripts and loaded them from a file. I can make my c++ objects available with:
    Qt Code:
    1. // in main.cpp
    2. qmlRegisterType<CustonObject>("MyCustonObject", 1, 0, "QmlCustonObject");
    3.  
    4. // In my qml I can use a script with
    5. import "someFile.js" as File
    To copy to clipboard, switch view to plain text mode 
    This works great as long as the file is in the resources (qrc). However, I want to be able to change the scripts without having to recompile the application. So, I looked at
    Qt Code:
    1. Qt.import("otherFile.js")
    To copy to clipboard, switch view to plain text mode 
    And wanted to import that file from the "someFile.js" which is in the resources. I found that I need an absolute path for this to work. The problem is then that the path will change across different platforms. So I needed a way to have the path dynamic. The only way here was to wrap the import in a function
    Qt Code:
    1. // function in "someFile.js"
    2. function loadScript(path)
    3. {
    4. Qt.import(path);
    5. }
    To copy to clipboard, switch view to plain text mode 
    While that works, the script is then only available inside the function scope. If I warp everything in such a function call, the script gets laoded every time the function is called. So, I packed it into an object instead to avoid loading the script multiple times.
    Qt Code:
    1. // defined in "someFile.js"
    2. function Loader() {
    3. Qt.import(getPathFromQmlFunction());
    4. }
    5. Loader.prototype.Execute() {
    6. // do stuff here...
    7. }
    8.  
    9. var MyLoader = new Loader();
    To copy to clipboard, switch view to plain text mode 
    In a qml file I can then add a helper function. The only part to be aware of is that this function must be declared before the "someFile.js" gets included.
    Qt Code:
    1. // defined in qml
    2. function getPathFromQmlFunction()
    3. {
    4. // figure out path for plattform
    5. return "some/path/goes/here/otherFile.js";
    6. }
    To copy to clipboard, switch view to plain text mode 
    while this works, it requires me to fiddle with the Loader object and extend it depending on what I want to make available to the scripts. It seams a cumbersome way to implement scripting and I am unsure about it's limitations and restrictions across platforms. Hence, my initial question. What is the proper/recommanded way to make a qml application fully scriptable? Should I continue with this approach or should I use Qt Script instead and load my scripts with a QScriptEngine?

    Edit: fixed some typo
    Last edited by motaito; 14th April 2015 at 14:41.

Similar Threads

  1. Replies: 4
    Last Post: 19th November 2012, 14:35
  2. Replies: 3
    Last Post: 28th October 2011, 23:24
  3. Replies: 2
    Last Post: 7th September 2011, 13:12
  4. Replies: 1
    Last Post: 30th May 2011, 13:46
  5. Replies: 3
    Last Post: 6th January 2010, 16:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.