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. Correctness check: using anchors with layouts

Correctness check: using anchors with layouts

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

    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?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vladstelmahovsky
      wrote on last edited by
      #2

      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

      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