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. How to fit and align items of GridView?
Forum Updated to NodeBB v4.3 + New Features

How to fit and align items of GridView?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.6k Views 1 Watching
  • 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
    Ibrahim
    wrote on last edited by Ibrahim
    #1

    Hi. I wrote this code:

    Window {
      visible: true
      width: 640
      height: 480
      title: qsTr("Hello World")
    
      GridView {
        id: area
        width: parent.width
        height: parent.height
        anchors.centerIn: parent
        anchors.margins: 20
        cellWidth: width / 4
        cellHeight: height / 4
    
        model: 40
        clip: true
        interactive: false
    
        delegate: Rectangle {
          id: rect
          width: area.cellWidth - 5
          height: area.cellHeight - 5
          color: 'red'
          Text {
            text: index
            color: 'white'
            anchors.centerIn: parent
          }
          MouseArea {
            anchors.fill: parent
            onClicked: rect.visible = false
          }
        }
      }
    

    I set model as 40 and I set interactive as false. But I see just 15 items like this:
    alt text
    I want to fit all items to screen. How can I do it? I didn't use GridLayout because when I click the rectangle, I set visible of that rectangle as false and GridLayout aligns all rectangles again. But I don't want it.
    Also if I set model of GridView as 4, I get this result:
    alt text
    But I want like this (4 items aligned to center):
    alt text
    How can I do them (to fit and align)? Thanks.

    UPDATE! - SOLVED!

    import QtQuick 2.10
    import QtQuick.Window 2.10
    import QtQuick.Layouts 1.3
    
    Window {
      visible: true
      width: 420
      height: 720
      title: qsTr("Hello World")
    
    
      GridView {
        id: area
    
        property int verticalChoice: (parent.width < parent.height) ? 4 : model / 4
        property int horizontalChoice: (parent.width < parent.height) ? model / 4 : 4
    
        width: (model < 8) ? 2 * cellWidth : cellWidth * verticalChoice
        height: (model < 8) ? 2 * cellHeight : cellHeight * horizontalChoice
        anchors.centerIn: parent
    
        cellWidth: (parent.width / verticalChoice)
        cellHeight: (parent.height / horizontalChoice)
    
        model: 8
        clip: true
        interactive: false
    
        delegate: Item {
          width: area.cellWidth
          height: area.cellHeight
          Rectangle {
            id: rect
            anchors {
              fill: parent
              margins: 5
            }
            color: 'red'
            Text {
              text: index
              color: 'white'
              anchors.centerIn: parent
            }
          }
        }
      }
    }
    

    But anchors.centerIn: parent isn't working.

    raven-worxR 1 Reply Last reply
    0
    • I Ibrahim

      Hi. I wrote this code:

      Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
      
        GridView {
          id: area
          width: parent.width
          height: parent.height
          anchors.centerIn: parent
          anchors.margins: 20
          cellWidth: width / 4
          cellHeight: height / 4
      
          model: 40
          clip: true
          interactive: false
      
          delegate: Rectangle {
            id: rect
            width: area.cellWidth - 5
            height: area.cellHeight - 5
            color: 'red'
            Text {
              text: index
              color: 'white'
              anchors.centerIn: parent
            }
            MouseArea {
              anchors.fill: parent
              onClicked: rect.visible = false
            }
          }
        }
      

      I set model as 40 and I set interactive as false. But I see just 15 items like this:
      alt text
      I want to fit all items to screen. How can I do it? I didn't use GridLayout because when I click the rectangle, I set visible of that rectangle as false and GridLayout aligns all rectangles again. But I don't want it.
      Also if I set model of GridView as 4, I get this result:
      alt text
      But I want like this (4 items aligned to center):
      alt text
      How can I do them (to fit and align)? Thanks.

      UPDATE! - SOLVED!

      import QtQuick 2.10
      import QtQuick.Window 2.10
      import QtQuick.Layouts 1.3
      
      Window {
        visible: true
        width: 420
        height: 720
        title: qsTr("Hello World")
      
      
        GridView {
          id: area
      
          property int verticalChoice: (parent.width < parent.height) ? 4 : model / 4
          property int horizontalChoice: (parent.width < parent.height) ? model / 4 : 4
      
          width: (model < 8) ? 2 * cellWidth : cellWidth * verticalChoice
          height: (model < 8) ? 2 * cellHeight : cellHeight * horizontalChoice
          anchors.centerIn: parent
      
          cellWidth: (parent.width / verticalChoice)
          cellHeight: (parent.height / horizontalChoice)
      
          model: 8
          clip: true
          interactive: false
      
          delegate: Item {
            width: area.cellWidth
            height: area.cellHeight
            Rectangle {
              id: rect
              anchors {
                fill: parent
                margins: 5
              }
              color: 'red'
              Text {
                text: index
                color: 'white'
                anchors.centerIn: parent
              }
            }
          }
        }
      }
      

      But anchors.centerIn: parent isn't working.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Ibrahim
      you need to calculate the size of the GridView yourself based on the desired values/behavior.
      And then center it in the parent (anchors.centerIn: parent).

      To show all 40 items, you of course need to also adapt the cellSize dynamically.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      I 1 Reply Last reply
      3
      • raven-worxR raven-worx

        @Ibrahim
        you need to calculate the size of the GridView yourself based on the desired values/behavior.
        And then center it in the parent (anchors.centerIn: parent).

        To show all 40 items, you of course need to also adapt the cellSize dynamically.

        I Offline
        I Offline
        Ibrahim
        wrote on last edited by
        #3

        @raven-worx thanks, how can I set cellSize? Is this a property? Because I couldn't find it.

        raven-worxR 1 Reply Last reply
        0
        • I Ibrahim

          @raven-worx thanks, how can I set cellSize? Is this a property? Because I couldn't find it.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Ibrahim said in How to fit and align items of GridView?:

          @raven-worx thanks, how can I set cellSize? Is this a property? Because I couldn't find it.

          you are already using it in the code you've posted?! (cellWidth / cellHeight)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the 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