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. Column's height not calculated properly when its children use anchors?

Column's height not calculated properly when its children use anchors?

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 1.9k 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.
  • I Offline
    I Offline
    ivoryz
    wrote on last edited by
    #1

    @
    Column {
    Rectangle { width: 10; height: 10 }
    Rectangle {
    anchors { left: parent.left; right: parent.right }
    height: 10
    }
    Component.onCompleted: console.log("height:", height)
    }
    @

    Output:
    height: 10

    I expected 20 here...
    Why the horizontal anchoring affects the column's height?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

      Actually the anchor modify "visually" the aspect of your Component, but does not change the values of the with and height properties.
      This could be disturbing first : (Specially for Text element for myself), but after some time your do with it.
      In your case this : @width : parent.width
      height : parent.height@ seems all right!

      dmcr

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ivoryz
        wrote on last edited by
        #3

        Thanks for the hint. But I did try using parent.width and it did not help:
        @
        Column {
        Rectangle { id: rect1; width: 10; height: 10 }
        Rectangle {
        id: rect2
        width: parent.width
        height: 10
        }
        Component.onCompleted: console.log("height:", height)
        }
        @

        still outputs "height: 10"
        It gives 20 if I replace "width: parent.width" with "width: rect1.width", though. But, if I had many rectangles in the column then I would have to iterate through all of them in order to find their maximum width (hence the width of the Column, I assume). I want to avoid such a procedure and make things simple. The final goal is to have the width of the rect2 match the width of the column regardless of how many items are present in it, and be able to get the correct height of this Column.
        Here is the full story: I am using the VisualItemModel like so:
        @
        VisualItemModel {
        id: itemModel
        Column {
        /* Some UI here stacked vertically*/
        }
        Column {
        /* Some other UI stacked vertically
        }
        ...
        }
        @

        which is then used in
        @
        ListView {
        model: itemModel
        orientation: ListView.Horizontal
        snapMode: ListView.SnapOneItem // Only one Column from itemModel is visible in the ListView at a time
        ...
        Component.onCompleted: {
        /* Iterate through ListView's contentItem.children (Columns), find the maximum Column height/width and set these max values as width/height for the ListView*/
        }
        }
        @

        So, in order to set the height/width of the ListView I need to know the maximum height/width of the Columns in the VisualItemModel (NOTE: I specifically need the width/height of the ListView be based on the maximum values of height/width obtained from the Columns). But, having rect2 defined like above I won't get correct values for its Column's height. Maybe what I want to achieve (ListView that accomodates its children based on their max sizes) can be done easier (any suggestions?), but still the question posed in the title of this post holds for me...

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

          Actually i read quite fast your post.
          In fact, the width of your column is undefined as it stands.
          you had to set : idColumn.with = ....

          and what your intend to do is all right... if you really needs it.
          However in many cases you don't need it hopefully.

          dmcr

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ivoryz
            wrote on last edited by
            #5

            I think I found a workaround for this issue:
            instead of "width: parent.width" in rect2 I used "width: parent.childrenRect.width", and in this case the Column's height was correct: 20.
            Btw, similar behaviour can be also seen when Rectangle is used instead of the Column item:
            @
            Rectangle {
            width: childrenRect.width
            height: childrenRect.height
            Rectangle { width: 10; height: 10 }
            Rectangle {
            anchors { left: parent.left; right: parent.right }
            height: 10
            }
            Component.onCompleted: console.log("height:", height)
            }
            @

            Like with the Column, it outputs "height: 10". So, it seems like this is a common behaviour when the parent's width is not explicitelyt defined...
            So, instead of anchoring, one can use "width: parent.childrenRect.width" to fix it, while leaving the parent's width undefined.

            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