Placing dynamic QML element in another dynamic QML element.



  • Hello

    As my title says, I would like to create a dynamic element inside another dynamic element in QML.
    For example I would like to dynamically create a big blue Rectangle (as background) and in it I would like to place another red and smaller rectangle (as an Item; also dynamically created).

    Hopefully this would be possible in QML since for dynamic elements the Id can't be used. Any hints, examples or information is very welcome.

    Thanks in advance !


  • Moderators

    What do you actually mean by "dynamic element"? One loaded with Loader, or with .createComponent(), or one instantiated in C++?

    The procedure is quite simple in any case: you create the second object in QML defining the first object - and in the same way. So, for example, using Loader element:
    @
    // Your QML file:
    ...
    Loader {
    source: "Dynamic1.qml"
    }

    // Dynamic1.qml
    Rectangle {
    //your first rectangle
    Loader {
    source: "Dynamic2.qml"
    }
    }

    // Dynamic2.qml
    Rectangle {
    // Your second rectangle
    }
    @



  • Well I need to do this using QML only. So I suppose Loader or .createComponent() are my options.

    What I mean by dynamic elements is for example:
    Creating a number of TextEdit Items according to the data in a List. So If in the list I find a...let's say number 5, then I would need to create 5 TextEdit items with the posibility to read and write data, and identify each one of the dynamically created TextEdit items.

    I hope I make myself clearer this time. I will investigate further if the Loader element is useful for my purpose.


  • Moderators

    Sound more like a job for ListView or - even better - Repeater elements, then. You can access Repeater's elements by inspecting item.children, and ListView (or GridView) by checking out the model. You need to identify the elements either by index or objectName property, though - standard ids will not work.


Log in to reply
 

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