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. anchors of components dynamically created
QtWS25 Last Chance

anchors of components dynamically created

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 3.1k 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.
  • N Offline
    N Offline
    Nitenq
    wrote on last edited by
    #1

    Hello !

    I have problems with positioning dynamically created objects. When I try to anchor those objects, I get the error "Cannot anchor to a null item". Here is an example where I try to place a button next to a static Rectangle. Any Idea ?

                Rectangle{
                    id:myRectangle
                    anchors.centerIn: parent
                    color:"yellow"
                    width: 50
                    height: 50
                }
    
                Component {
                        id: tagButtonComponent;
    
                       Button {
                            text: "new tag"
                        }
                }
    
                function addNewTag(tagName){
                    var newButton = tagButtonComponent.createObject(tagCloudPanel);
    
                    if (newButton === null) {
                        // Error Handling
                        console.log("Error creating object");
                    }
                    else{
                      // --> this line does not work
                        newButton.anchors.left = myRectangle.anchors.right
                        newButton.anchors.leftMargin = 8
    
                     // save the object in array for later use
                        tagButtons.push(newButton);
                    }
                }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      miguelorenes
      wrote on last edited by
      #2

      the problem is here:

      newButton.anchors.left = myRectangle.anchors.right;
      

      It should be

      newButton.anchors.left = myRectangle.right;
      

      BTW, don't forget newButton and myRectangle need to have a sibling relationship, in other case you will also get an error like:

      QML Button: Cannot anchor to an item that isn't a parent or sibling.
      

      Cheers!.

      1 Reply Last reply
      3
      • N Offline
        N Offline
        Nitenq
        wrote on last edited by
        #3

        Thanks for the answer, that was an silly mistake ;)

        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