Nominate our 2022 Qt Champions!

Trying to access properties inside a dynamically created qml component [ Solved]

  • Hi,
    I have a piece of code that dynamically generates (from javascript) a form. In the example below the form only contains one textfield but the use-case i have is more complicated. There is also a button which should gather the information inside the form and do something with it. The button is outside the dynamic creation. The problem I face is that button doesnt know what's inside and the dynamic form doesnt know what's outside. Trying to access properties from either side results insome 'unknown' property ... error.

    @ApplicationWindow {
    visible: true
    width: 800
    height: 600
    property var currentAppForm : null
    property string formQML : "import QtQuick 2.2; import QtQuick.Controls 1.1;import QtQuick.Layouts 1.1;
    TextField {id : pin_0; text : "blup"}"

    function createform() {
        if (currentAppForm != null) currentAppForm.destroy(0)
        currentAppForm = Qt.createQmlObject(formQML, theparent, "autoform2");
        id : doit
        onTriggered: {
            if ( currentAppForm != null)
                console.debug("do something with the content of the dynamic created form") 
        id : doform
        onTriggered: {
    Column {
        width : 200
        height : 400
            text : "make form"
            action: doform
            text : "press me"
            action: doit
            id : theparent
            width : 500
            height : 300
            border.width: 1


  • Moderators


    If I understood you correctly,
    To access elements of dynamically created elements you can use the variable of it for eg in your case currentAppForm
    To test it you can try following

    should print "blup"

    And to access outer elements inside the dynamic form you can just access them using their id's

  • Thanks for the hint. I didnt realize that you could access the internal members (of the dynamic qml component) without referring to an id. Your currentAppForm.text doesnt bother about the actual id of the textfield. In my actual code the controls are wrapped in a column and I was trying (unsuccessfully) by using its id to access the stuff that was parented by the column. But all these ids are unknown to the outside.
    I have solved it by creating a string property that contains the values of the internal controls. This property can be accessed without using id's.

  • Two hints:
    1)For maintenance purpose, dynamic creation of QML object can be done using a QML file:(instead of a string )
    myObj = Qt.createComponent("Obj.qml");
    if (myObj.status === Component.Ready)

    function finishToCreateObj() {
    if (myObj.status == Component.Ready) {
    myObj.createObject(objParent, {id: anIdEasytoDefined } );
    } else {
    2) Using this method you can specified an id easily (in the previously code I set "anIdEasytoDefined"

Log in to reply