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. how to edit the property of dynamically created object?

how to edit the property of dynamically created object?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 358 Views 1 Watching
  • 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.
  • N Offline
    N Offline
    NixUhs
    wrote on last edited by
    #1

    according to the tutorial,Qml can use createComponent() + createObject() to dynamically create objects
    but how to edit the property of these objects
    For example:
    //dyRec.qml
    Rectangle {
    id: rect
    TextEdit {
    id: rectText
    text: qsTr("Default")
    }
    }

    //main.qml
    Window {
    id: root
    var dyRect = Qt.createComponent("dyRec.qml")
    var dyRectObj = dyRect.createObject(root)
    //Here I want to edit the text of TextEdit in Rectangle, how to do it
    dyRectObj.rectText.text = qsTr("1111") //<--Error
    }

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You need to expose the text as a property. The id you set in your QML file is local - visible only inside of that same file. When you try to access it from another file (main.qml), it will never work. Does not matter if it is a dynamic component or not.

      So, for example:

      //dyRec.qml
      Rectangle {
        property alias text: rectText.text
        id: rect
        TextEdit {
          id: rectText
          text: qsTr("Default")
        }
      }
      
      // main.qml
      var dyRect = Qt.createComponent("dyRec.qml")
      var dyRectObj = dyRect.createObject(root)
      dyRectObj.text = qsTr("1111")
      

      (Z(:^

      1 Reply Last reply
      2

      • Login

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