Correctness check: using anchors with layouts



  • I was able to successfully create a colored background for a delegate definition consisting of a column layout of text objects by doing the following (simplified as an example):

        ColumnLayout {
          Rectangle {
            anchors.fill: parent
            color: "green"
          }
    
          Text {
            Layout.fillWidth: true
            text: "Line1"
          }
    
          Text {
            Layout.fillWidth: true
            text: "Line2"
          }
        }
    

    It worked in the code as I expected. The green area was a background to the two text items and wrapped them precisely. At the code review for this however, I was branded a Qt heretic for using anchor placement for the Rectangle within a ColumnLayout (and sentenced to be burned in a sauna :-) ). I argued that it was the most elegant way of putting a colored background behind the text with the right implicit z-order and taking advantage of the ColumnLayout's wrap-to-contents behavior. It also had the added advantage of not increasing the depth of the overall component layout. The reviewer insisted that anchor placement is undefined within a layout, but the evidence is that is simply supercedes any layout behavior (which is what I wanted and expected). The re-write looked like the following:

        Rectangle {
          color: "green"
          height: wrapper.impliciteWidth
    
          ColumnLayout {
            id: wrapper
            Text {
              Layout.fillWidth: true
              text: "Line1"
            }
    
           Text {
             Layout.fillWidth: true
             text: "Line2"
           }
        }
      }
    

    While the re-write was equivalent, I thought it was clunkier because of the "height: wrapper.impliciteHeight" line to get the vertical wrapping behavior and because it was unnecessarily deeper than it had to be.

    So, question: is it undefined to use anchor placement directly within a layout, or is it guaranteed by design to work as I assumed - that is, to override any layout placement and take the child object out of consideration for allotment of cells?



  • AFAIK, Layout not supposed to work with anchors. So if its works right now, you are just lucky and noone guarantee it will work in future


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.