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. Component createObject has no effect
Forum Updated to NodeBB v4.3 + New Features

Component createObject has no effect

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 214 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.
  • E Offline
    E Offline
    exru
    wrote on last edited by
    #1
       Rectangle{
            id: selectedArea
            color: "transparent"
    
            anchors.top: topLeft.top
            anchors.left: topLeft.left
            anchors.right: bottomRight.right
            anchors.bottom: bottomRight.bottom
    
            Component{
                id: verticalLine
                ShapePath {
                    strokeColor: "red"
                    strokeWidth: 1
                    startY: 0
                    PathLine {
                        x: 0
                        y: 100
                    }
                    Component.onCompleted: {
                        console.log(startX);
                    }
                }
            }
    
            Shape {
                id: net
                anchors.fill: parent
                z:1
                Component.onCompleted: {
                    for(var i = 0; i != 10; i ++){
                        verticalLine.createObject(net, {startX: i * 10}); // object created but nothing visible
                    }
                }
            }
    
    
    
        }
    
    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #3

      @exru
      You need to manage the list of items in the data property of Shape. This is a list. So I added your object to the list:

      Rectangle{
              id: selectedArea
              color: "transparent"
      
              anchors.fill: parent
      
              Component{
                  id: verticalLine
                  ShapePath {
                      strokeColor: "red"
                      strokeWidth: 1
                      startY: 0
      
                      PathLine { x: 0; y: 100 }
      
                      Component.onCompleted: {
                          console.log(startX);
                      }
                  }
              }
      
              Shape {
                  id: net
                  anchors.fill: parent
      
                  z:1
                  Component.onCompleted: {
                      var obj
      
                      for(var i = 0; i !== 10; i ++){
                          obj = verticalLine.createObject(net, {startX: i * 10})
                          data.push(obj)
                      }
                  }
              }
          }
      

      You can also create a new list, push items onto list, and substitute:

      var list=[]
      var obj
      ...
      obj = createObject...
      list.push(obj)
      ...
      data = list
      

      C++ is a perfectly valid school of magic.

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

        vertical line is just a component. You have to use it.

        https://doc.qt.io/qt-5/qml-qtqml-component.html#createObject-method
        https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html
        https://doc.qt.io/qt-5/qml-qtquick-repeater.html
        https://doc.qt.io/qt-5/qml-qtquick-loader.html

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #3

          @exru
          You need to manage the list of items in the data property of Shape. This is a list. So I added your object to the list:

          Rectangle{
                  id: selectedArea
                  color: "transparent"
          
                  anchors.fill: parent
          
                  Component{
                      id: verticalLine
                      ShapePath {
                          strokeColor: "red"
                          strokeWidth: 1
                          startY: 0
          
                          PathLine { x: 0; y: 100 }
          
                          Component.onCompleted: {
                              console.log(startX);
                          }
                      }
                  }
          
                  Shape {
                      id: net
                      anchors.fill: parent
          
                      z:1
                      Component.onCompleted: {
                          var obj
          
                          for(var i = 0; i !== 10; i ++){
                              obj = verticalLine.createObject(net, {startX: i * 10})
                              data.push(obj)
                          }
                      }
                  }
              }
          

          You can also create a new list, push items onto list, and substitute:

          var list=[]
          var obj
          ...
          obj = createObject...
          list.push(obj)
          ...
          data = list
          

          C++ is a perfectly valid school of magic.

          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