Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Reload .qml file runtime
Forum Updated to NodeBB v4.3 + New Features

Reload .qml file runtime

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 2.3k 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.
  • O Offline
    O Offline
    ODБOï
    wrote on 25 Jan 2018, 16:10 last edited by ODБOï
    #1

    Hi, I'm trying to create an application where .qml files are accessible for the user (he can edit / change visual aspect ).

    When the application starts : if users .qml file is present at given path, application must load that file, else it must load the 'same' (default version) file from qrc .

    if (while app is running) user edits the file, application must reload file with changes.

    I have tryed lot of things with Loader/ QQuickPaintedItem etc..

    Finnaly i dit this reusable thing :

    RunTimeLoader.qml

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    
    Item {
        id:customLoader
        Component.onCompleted: {
            initUserFile()
        }
        property url userFileUrl: "file:///C:/Users/user/Documents/QML_CUSTOM_ITEM/Customizable.qml"  // user accessible path/YourComponent.qml
        property url defaultFile: "Customizable.qml"  // default
        property string userFileStr
        property string defaultFileStr
        signal setDefault()
        signal reload()
        onSetDefault:{
            var request = new XMLHttpRequest()
            request.open('GET',defaultFile)
            request.onreadystatechange = function(event) {
                if (request.readyState === XMLHttpRequest.DONE&&request.responseText.length>0) {
                    defaultFileStr = request.responseText;
                    Qt.createQmlObject(defaultFileStr,loaderItem,null)
                }
            }
            request.send()
            delete(request)
        }
        onUserFileStrChanged: {
            if(userFileStr.length>0){
                Qt.createQmlObject(userFileStr,loaderItem,null)
            }
        }
        function initUserFile(){
            var request = new XMLHttpRequest()
            request.open('GET',userFileUrl)
            request.onreadystatechange = function(event) {
                if (request.readyState === XMLHttpRequest.DONE&&request.responseText.length>0) {
                    userFileStr = request.responseText;
                }
            }
            request.send()
            delete(request)
        }
        onReload: {
            for(var i = loaderItem.children.length; i > 0 ; i--) {
                console.log("destroying: " + i)
                loaderItem.children[i-1].destroy()
            }
            initUserFile()
            initTimer.start()
        }
        Timer{
            id:initTimer
            running: true
            interval: 1200
            repeat: false
            onTriggered: {
                if(userFileStr.length>0&&loaderItem.children.length<1){
                    Qt.createQmlObject(userFileStr,loaderItem,null)
                }
                else if (loaderItem.children.length<1){
                    setDefault()
                }
            }
        }
        Button{
            text: "Reload"
            onClicked: {
                reload()
            }
        }
        Item{
            id:loaderItem
            anchors.fill:  parent
        }
    }
    

    To use this : just change 'userFileUrl' and 'defaultFile'

    then in your main.qml

    RunTimeLoader{
       height:parent.height
       width: parent.width
    }
    

    Please tell me if you know better way to implement this dynamic reloading behavior.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 25 Jan 2018, 17:33 last edited by
      #2

      It is not a good idea to present the qml file itself for the user to edit. You can provide some property file where user can edit the file and reload your app. You can do this easily with QML and C++ integration.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      O 1 Reply Last reply 25 Jan 2018, 17:49
      1
      • D dheerendra
        25 Jan 2018, 17:33

        It is not a good idea to present the qml file itself for the user to edit. You can provide some property file where user can edit the file and reload your app. You can do this easily with QML and C++ integration.

        O Offline
        O Offline
        ODБOï
        wrote on 25 Jan 2018, 17:49 last edited by
        #3

        @dheerendra I thought it was a good idea :p
        But seriously, when i'm sayin 'user' this is just for the exemple... i'm trying to load a *.qml file from 'standartPath, like c:\Documents\etc....' runtime.

        1 Reply Last reply
        0

        2/3

        25 Jan 2018, 17:33

        • Login

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