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. Grid layout doesn't work well
Forum Updated to NodeBB v4.3 + New Features

Grid layout doesn't work well

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 803 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.
  • Y Offline
    Y Offline
    ynakane
    wrote on last edited by
    #1

    Hi!
    I want to arrange the rectangles as shown in the figure.
    glid.png
    I implemented it using GridLayout, but it looks like the following, and I can't arrange it as I want.
    glid2.png
    Below is my code.
    Does anyone know the cause?
    Thank you.

    GridLayout{
        id: gird
        anchors.fill: parent
    
        Rectangle{
            color: "red"
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.column: 0
            Layout.row: 0
            Layout.columnSpan: 2
            Layout.rowSpan: 2
        }
        Rectangle{
            color: "green"
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.column: 2
            Layout.row: 0
            Layout.columnSpan: 1
            Layout.rowSpan: 1
        }
        Rectangle{
            color: "blue"
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.column: 3
            Layout.row: 0
            Layout.columnSpan: 1
            Layout.rowSpan: 1
        }
        Rectangle{
            color: "yellow"
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.column: 2
            Layout.row: 1
            Layout.columnSpan: 2
            Layout.rowSpan: 1
        }
        Rectangle{
            color: "pink"
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.column: 4
            Layout.row: 0
            Layout.columnSpan: 2
            Layout.rowSpan: 2
        }
        Rectangle{
            color: "gray"
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.column: 0
            Layout.row: 2
            Layout.columnSpan: 2
            Layout.rowSpan: 2
        }
        Rectangle{
            color: "brown"
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.column: 2
            Layout.row: 2
            Layout.columnSpan: 2
            Layout.rowSpan: 2
        }
        Rectangle{
            color: "navy"
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.column: 4
            Layout.row: 2
            Layout.columnSpan: 2
            Layout.rowSpan: 2
        }
    }
    
    1 Reply Last reply
    0
    • denishessbergerD Offline
      denishessbergerD Offline
      denishessberger
      wrote on last edited by
      #2

      Set a preferred height / width.

      GridLayout{
          id: gird
          anchors.fill: parent
      
          Rectangle{
              color: "red"
              Layout.fillWidth: true
              Layout.fillHeight: true
              Layout.preferredWidth: 50
              Layout.preferredHeight: 50
              Layout.column: 0
              Layout.row: 0
              Layout.columnSpan: 2
              Layout.rowSpan: 2
          }
          Rectangle{
              color: "green"
              Layout.fillHeight: true
              Layout.fillWidth: true
              Layout.column: 2
              Layout.row: 0
              Layout.columnSpan: 1
              Layout.rowSpan: 1
          }
          Rectangle{
              color: "blue"
              Layout.fillHeight: true
              Layout.fillWidth: true
              Layout.column: 3
              Layout.row: 0
              Layout.columnSpan: 1
              Layout.rowSpan: 1
          }
          Rectangle{
              color: "yellow"
              Layout.fillHeight: true
              Layout.fillWidth: true
              Layout.column: 2
              Layout.row: 1
              Layout.columnSpan: 2
              Layout.rowSpan: 1
          }
          Rectangle{
              color: "pink"
              Layout.fillHeight: true
              Layout.fillWidth: true
              Layout.column: 4
              Layout.row: 0
              Layout.columnSpan: 2
              Layout.rowSpan: 2
          }
          Rectangle{
              color: "gray"
              Layout.fillHeight: true
              Layout.fillWidth: true
              Layout.preferredWidth: 50
              Layout.preferredHeight: 50
              Layout.column: 0
              Layout.row: 2
              Layout.columnSpan: 2
              Layout.rowSpan: 2
          }
          Rectangle{
              color: "brown"
              Layout.fillHeight: true
              Layout.fillWidth: true
              Layout.preferredWidth: 50
              Layout.column: 2
              Layout.row: 2
              Layout.columnSpan: 2
              Layout.rowSpan: 2
          }
          Rectangle{
              color: "navy"
              Layout.fillHeight: true
              Layout.fillWidth: true
              Layout.preferredWidth: 50
              Layout.preferredHeight: 50
              Layout.column: 4
              Layout.row: 2
              Layout.columnSpan: 2
              Layout.rowSpan: 2
          }
      }
      
      Y 1 Reply Last reply
      1
      • TassosT Offline
        TassosT Offline
        Tassos
        wrote on last edited by
        #3

        Unfortunately I don't know the cause, it is probably GridLayout's bad implementation, but I can suggest a workaround:

        import QtQuick
        import QtQuick.Window
        import QtQuick.Layouts
        
        Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
        
          component StretchedRect: Rectangle {
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.preferredWidth: Layout.columnSpan
            Layout.preferredHeight: Layout.rowSpan
          }
        
          GridLayout{
            id: gird
            anchors.fill: parent
        
            rows: 4
            columns: 6
        
            StretchedRect {
              color: "red"
              Layout.columnSpan: 2
              Layout.rowSpan: 2
            }
        
            StretchedRect {
              color: "green"
              Layout.columnSpan: 1
              Layout.rowSpan: 1
            }
        
            StretchedRect {
              color: "blue"
              Layout.columnSpan: 1
              Layout.rowSpan: 1
            }
        
            StretchedRect {
              color: "pink"
              Layout.columnSpan: 2
              Layout.rowSpan: 2
            }
        
            StretchedRect {
              color: "yellow"
              Layout.columnSpan: 2
              Layout.rowSpan: 1
            }
        
            StretchedRect {
              color: "gray"
              Layout.columnSpan: 2
              Layout.rowSpan: 2
            }
        
            StretchedRect {
              color: "brown"
              Layout.columnSpan: 2
              Layout.rowSpan: 2
            }
        
            StretchedRect {
              color: "navy"
              Layout.columnSpan: 2
              Layout.rowSpan: 2
            }
          }
        }
        

        Let me explain:
        Since you want to arrange the rectangles in 6x4 grid.
        I put the

            rows: 4
            columns: 6
        

        properties in the GridLayout

        That allows to reduce clutter removing all the Layout.column and Layout.row from the Rectangles since their position is implied.
        Caution: the order of Rectangles is now significant. I had to place the yellow one bellow the pink one.
        And finally setting the Layout.prefferedWidth and Layout.prefferedHeight for each rectangle to its corresponding columnSpan and rowSpan, forced the GridLayout to arrange them as you wanted.

        HTH.

        Y 1 Reply Last reply
        0
        • denishessbergerD denishessberger

          Set a preferred height / width.

          GridLayout{
              id: gird
              anchors.fill: parent
          
              Rectangle{
                  color: "red"
                  Layout.fillWidth: true
                  Layout.fillHeight: true
                  Layout.preferredWidth: 50
                  Layout.preferredHeight: 50
                  Layout.column: 0
                  Layout.row: 0
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
              }
              Rectangle{
                  color: "green"
                  Layout.fillHeight: true
                  Layout.fillWidth: true
                  Layout.column: 2
                  Layout.row: 0
                  Layout.columnSpan: 1
                  Layout.rowSpan: 1
              }
              Rectangle{
                  color: "blue"
                  Layout.fillHeight: true
                  Layout.fillWidth: true
                  Layout.column: 3
                  Layout.row: 0
                  Layout.columnSpan: 1
                  Layout.rowSpan: 1
              }
              Rectangle{
                  color: "yellow"
                  Layout.fillHeight: true
                  Layout.fillWidth: true
                  Layout.column: 2
                  Layout.row: 1
                  Layout.columnSpan: 2
                  Layout.rowSpan: 1
              }
              Rectangle{
                  color: "pink"
                  Layout.fillHeight: true
                  Layout.fillWidth: true
                  Layout.column: 4
                  Layout.row: 0
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
              }
              Rectangle{
                  color: "gray"
                  Layout.fillHeight: true
                  Layout.fillWidth: true
                  Layout.preferredWidth: 50
                  Layout.preferredHeight: 50
                  Layout.column: 0
                  Layout.row: 2
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
              }
              Rectangle{
                  color: "brown"
                  Layout.fillHeight: true
                  Layout.fillWidth: true
                  Layout.preferredWidth: 50
                  Layout.column: 2
                  Layout.row: 2
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
              }
              Rectangle{
                  color: "navy"
                  Layout.fillHeight: true
                  Layout.fillWidth: true
                  Layout.preferredWidth: 50
                  Layout.preferredHeight: 50
                  Layout.column: 4
                  Layout.row: 2
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
              }
          }
          
          Y Offline
          Y Offline
          ynakane
          wrote on last edited by
          #4

          @denishessberger

          Thanks you for answering.
          It works very well.
          But I can't understand meaning of "50".

          1 Reply Last reply
          0
          • TassosT Tassos

            Unfortunately I don't know the cause, it is probably GridLayout's bad implementation, but I can suggest a workaround:

            import QtQuick
            import QtQuick.Window
            import QtQuick.Layouts
            
            Window {
              width: 640
              height: 480
              visible: true
              title: qsTr("Hello World")
            
              component StretchedRect: Rectangle {
                Layout.fillHeight: true
                Layout.fillWidth: true
                Layout.preferredWidth: Layout.columnSpan
                Layout.preferredHeight: Layout.rowSpan
              }
            
              GridLayout{
                id: gird
                anchors.fill: parent
            
                rows: 4
                columns: 6
            
                StretchedRect {
                  color: "red"
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
                }
            
                StretchedRect {
                  color: "green"
                  Layout.columnSpan: 1
                  Layout.rowSpan: 1
                }
            
                StretchedRect {
                  color: "blue"
                  Layout.columnSpan: 1
                  Layout.rowSpan: 1
                }
            
                StretchedRect {
                  color: "pink"
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
                }
            
                StretchedRect {
                  color: "yellow"
                  Layout.columnSpan: 2
                  Layout.rowSpan: 1
                }
            
                StretchedRect {
                  color: "gray"
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
                }
            
                StretchedRect {
                  color: "brown"
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
                }
            
                StretchedRect {
                  color: "navy"
                  Layout.columnSpan: 2
                  Layout.rowSpan: 2
                }
              }
            }
            

            Let me explain:
            Since you want to arrange the rectangles in 6x4 grid.
            I put the

                rows: 4
                columns: 6
            

            properties in the GridLayout

            That allows to reduce clutter removing all the Layout.column and Layout.row from the Rectangles since their position is implied.
            Caution: the order of Rectangles is now significant. I had to place the yellow one bellow the pink one.
            And finally setting the Layout.prefferedWidth and Layout.prefferedHeight for each rectangle to its corresponding columnSpan and rowSpan, forced the GridLayout to arrange them as you wanted.

            HTH.

            Y Offline
            Y Offline
            ynakane
            wrote on last edited by
            #5

            @Tassos
            Thanks you for answering.
            It is very cool implementation and very helpful.

            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