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. Trying to access properties inside a dynamically created qml component [ Solved]
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.2k 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.
  • M Offline
    M Offline
    mschouwen
    wrote on last edited by
    #1

    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");
        console.debug("done2")
    }
    
    Action{
        id : doit
        onTriggered: {
            if ( currentAppForm != null)
                console.debug("do something with the content of the dynamic created form") 
        }
    }
    Action{
        id : doform
        onTriggered: {
            createform()
        }
    }
    Column {
        width : 200
        height : 400
    
        Button{
            text : "make form"
            action: doform
    
        }
        Button{
    
            text : "press me"
            action: doit
        }
        Rectangle{
            id : theparent
            width : 500
            height : 300
            border.width: 1
        }
    }
    

    }
    @

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      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
      @
      currentAppForm.text
      @

      should print "blup"

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

      157

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mschouwen
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DeeeZ
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          0

          • Login

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