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. Interact with Dynamic Object Created
Qt 6.11 is out! See what's new in the release blog

Interact with Dynamic Object Created

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 889 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.
  • A Offline
    A Offline
    AboutClod
    wrote on last edited by
    #1

    Hi everyone, this is my 2nd post on qt-forum and i'm stuck in a stupid issue.
    I've created a custom object called "TabTPDef.qml", and I use this object min. 4 times.
    I've created its with the following code, when the user click on the button.

     var component;
                           var sprite;
                           component = Qt.createComponent("TabTPDef.qml");
                           sprite = component.createObject(dhsbLink, {"x": 0, "y": 0});
    

    Now, in TabTPDef there is a button which call a C++ method to populate the 'TabTPDEF'. The populating works well, but it works for the all the TabTPDef instantiated. The onClicked for call the c++ method is descripted in 'TabTPDef' and i suppose the problem is here, because there isn't reference to the new component instantiated.

    Here how I create the new component:

    buttTp2.mouseButton.onClicked: {
        var component;
                           var sprite;
                           component = Qt.createComponent("TabTPDef.qml");
                           sprite = component.createObject(dhsbLink, {"x": 0, "y": 0,});
     
        sprite.anchors.top = parent.top
        sprite.anchors.topMargin = 100
        sprite.anchors.left = parent.left
        sprite.anchors.leftMargin = 450
        sprite.anchors.bottom = dhsbLink.bottom
        sprite.frateto.txtButt.text = "TP2";
        sprite.frateto.buttonMain.state = "off"
    
    }
    

    This is the TabTPDef, which contains other two custom object

    Item {
        id: tabTPDEF
        height: 700
        width: 325
        property alias soreta : soreta
        property alias frateto : frateto
    
        SwipeView {
            id: swipe
           currentIndex: 0
           clip: true
            anchors.fill: parent
    
            TabTPMonitor{
            id: frateto
            }
            TabTP{
             id: soreta
            }
        }
    ....
    }
    

    Sorry for the mistakes, i'm still learning qt, and my english is not so well.
    Thank you!

    1 Reply Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by
      #2

      @AboutClod

      http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html
      As a guess you're object isn't there when you tried using it? It may also have failed (maybe your component source is invalid / producing errors).

      You've missed all of this part of creating objects dynamically:

      if (component.status == Component.Ready)
              finishCreation();
          else
              component.statusChanged.connect(finishCreation);
      }
      
      function finishCreation() {
          if (component.status == Component.Ready) {
              sprite = component.createObject(appWindow, {"x": 100, "y": 100});
              if (sprite == null) {
                  // Error Handling
                  console.log("Error creating object");
              }
          } else if (component.status == Component.Error) {
              // Error Handling
              console.log("Error loading component:", component.errorString());
          }
      
      1 Reply Last reply
      1

      • Login

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