Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-60489

Nesting QtQuick.Layouts break the layout properties e.g. alignment

    XMLWordPrintable

Details

    Description

      In the example we have nested ColumnLayout - ColumnLayout - RowLayout and the content of the RowLayout is aligned incorrectly and Text is not wrapped. Changing ColumnLayouts to GridLayouts doesn't help. Hence, I assume this problem concerns all QtQuick.Layouts.

      The problem can be worked around by adding an Item with preferred width into the second ColumnLayout (the same where RowLayout resides). Also removing one layer of nesting seems to fix the problem.

      See the attached images for reference: blue items denote correct layout (i.e. the workaround is used) and red items are wrong. Also the attached example shows the problem.

      Tested on macOS and Windows 7.

      QML from the attached example:

      main.qml
      import QtQuick 2.6
      import QtQuick.Layouts 1.3
      import QtQuick.Window 2.0
      
      Window {
          id: root
          color: "white"
          visible: true
      
          width: 580
          height: 480
      
          ColumnLayout {
              anchors {
                  margins: 10
                  fill: parent
              }
      
              readonly property var errors: [
                  "This is a long message that should wrap when the screen is narrow, and center when the screen is wide.",
                  "This should be centered."
              ]
      
              // Note that blue messages behave properly, and red ones fail.
              // Also, IconMessage is a ColumnLayout, so it's automatically "Layout.fillWidth: true"
              IconMessage {
                  iconColor: 'blue'
                  messages: parent.errors[1]
              }
      
              IconMessage {
                  iconColor: 'blue'
                  messages: parent.errors[0]
              }
      
              IconMessage {
                  enableSpanner: false
                  iconColor: 'red'
                  messages: parent.errors[1]
              }
      
              IconMessage {
                  enableSpanner: false
                  iconColor: 'red'
                  messages: parent.errors[0]
              }
      
              Item {
                  id: spacer
                  Layout.fillHeight: true
                  width: 1
              }
          }
      }
      
      IconMessage.qml
      import QtQuick 2.6
      import QtQuick.Layouts 1.3
      
      // This Item should display a square icon with a wrapping message.
      // When the message is short, the row should center horizontally in the column.
      // When the message is wider than the column width, the message should wrap.
      
      ColumnLayout {
          id: root
          spacing: 0
          property alias messages: icon_message.text
          property alias iconColor: icon_rect.color
          property alias enableSpanner: spanner.visible
      
          // When this "spanner" is not visible (or omitted), the layout breaks and nothing wraps or aligns
          Item {
              id: spanner
              Layout.fillWidth: true
              height: 1
          }
      
          RowLayout {
              Layout.fillWidth: false
              Layout.maximumWidth: root.width
              Layout.alignment: Qt.AlignHCenter
      
              Rectangle {
                  id: icon_rect
                  width: 60
                  height: 60
                  color: 'black'
              }
      
              Text {
                  id: icon_message
                  Layout.fillWidth: true
                  Layout.fillHeight: true
                  Layout.maximumHeight: implicitHeight
                  verticalAlignment: Text.AlignVCenter
                  wrapMode: Text.Wrap
                  maximumLineCount: 2
                  elide: Text.ElideRight
                  font.pixelSize: 18
              }
          }
      }
      

      Attachments

        1. layout-bug-narrow.png
          layout-bug-narrow.png
          232 kB
        2. layout-bug-wide.png
          layout-bug-wide.png
          279 kB
        3. qtbug60489.zip
          5 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            smd Jan Arve
            qtcomsupport Qt Support
            Votes:
            5 Vote for this issue
            Watchers:
            11 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes