Important: Please read the Qt Code of Conduct -

Save dynamic QML scene to a QML file

  • Hello,

    I was trying to save a dynamically created scene such as the example below into a QML file and load the same configuration later.

    Does QML provide anything to save the scene back in a QML file? Thank you for any help.


  • I think you should not save a QML file, but a file that represents the information that is represented by the QML interface. That is, unless you are creating a QML editor, of course.

  • Thanks Andre.
    So, I am creating an editor which allows users to place custom QML elements onto a scene dynamically. Now I want to save the layout that the user created and also the user can later edit the QML manually to change values using text editor.
    What would be the best way to do this?

  • Look into Qt Creator source code, you find there what you needed.

  • Good question! Looking forward the tricks!

  • Okay, I tried to go through Qt Creator source but I don't see a straightforward way of using Qt classes or methods for storing a dynamic QML scene. as @Andre pointed out I realized I am trying to do the same thing as Qt-Creator, saving a dynamically created scene of QML elements.
    This means, I should write a module that parses my scene and writes a formatted QML file.

  • Yep, if we describe it in few words then you need to: go through all children recursively, get their properties, write data into file.

  • Actually for the QtQuick Designer we wrote a quite sophisticated DOM like model for QML that takes care of instantiating the QML objects and retrieving the data and (re)writing the data in the QML file (the text representation).
    Since Qt Creator 2.2 this even works cross process. The QML scene is created in a separate process to allow loading/unloading of custom C++ plugins.

  • Hi ThomasHartmann,

    Is this DOM like model available as some sort of api or library that we can use?

    I am also stuck in this problem, how to we save the current dynamic scene of qml to a qml file? Do we pass everything of the qml to a c++ function, and then output it to a text file?

  • Yes. We created some kind of DOM for qml language.
    It is the core of the QtQuick Designer: creator/src/plugins/qmldesigner.

    We called it Model and it is quite abstract and hides some parts of the QML syntax.

    Since we wrote it for the Designer it follows the model/view pattern and assumes that several views exists. Actually the model itself is independent from any text representation (*.qml) and a special view (called RewriterView) is responsible for the synchronization with the text file.

    Another view called InstanceView is responsible to create the actual QDeclarativeItems for visual representation. The instantiation of items happens in another process to allow arbitrary plugins.

    This approach might be a little to complex for simpler things. Since it is open source as part of Qt Creator everybody can have a look of course.

Log in to reply