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. GridView and distance between delegates
QtWS25 Last Chance

GridView and distance between delegates

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 566 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    Hi!
    My delegates vary in width. How do I set the distance between delegates?
    cellWidth doesn't suit me.

    1 Reply Last reply
    0
    • MarkkyboyM Offline
      MarkkyboyM Offline
      Markkyboy
      wrote on last edited by
      #2

      Post some working code :)

      Don't just sit there standing around, pick up a shovel and sweep up!

      I live by the sea, not in it.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        @Markkyboy said in GridView and distance between delegates:

        Post some working code :)

                        GridView {
                            id: gridViewSecondCategories
                            anchors.left: parent.left
                            anchors.leftMargin: labelNameRightMainInfoMain.anchors.leftMargin
                            anchors.right: parent.right
                            anchors.rightMargin: labelNameRightMainInfoMain.anchors.leftMargin
                            anchors.top: comboBoxSecondCategirtMainRightMainInfoMain.bottom
                            anchors.topMargin: textFieldNameRightMainInfoMain.anchors.topMargin
                            //width: 140
                            height: 90
                            //cellWidth: 50
                            cellHeight: 45
                            delegate: Item {
                                //x: 5
                                height: 30
                                width: labelDelegateGridViewSecondCategories.width + 40
                                Rectangle{
                                    id: rectangleDelegateGridViewSecondCategories
                                    height: parent.height
                                    width: parent.width
                                    color: "#c3bcbc"
                                    radius: height / 2
                                    Label{
                                        id: labelDelegateGridViewSecondCategories
                                        text: name
                                        font.pixelSize: 12
                                        anchors.verticalCenter: parent.verticalCenter
                                        anchors.left: parent.left
                                        anchors.leftMargin: 10
                                    }
                                    Button{
                                        id: buttonDelegateGridViewSecondCategories
                                        height: parent.height * 0.6
                                        width: height
                                        anchors.verticalCenter: parent.verticalCenter
                                        anchors.right: parent.right
                                        anchors.rightMargin: 10
                                        background: Rectangle{
                                            anchors.fill: parent
                                            color: "#00000000"
                                        }
        
                                        Image {
                                            source: "Images/cancel.png"
                                            anchors.fill: parent
                                        }
                                    }
                                }
        
                            }
                            model: ListModel {
                                id: listModelGreedWiewSecondCategories
                                ListElement {
                                    name: "Grey"
                                    colorCode: "grey"
                                }
        
                                ListElement {
                                    name: "Red"
                                    colorCode: "red"
                                }
        
                                ListElement {
                                    name: "Blue"
                                    colorCode: "blue"
                                }
        
                                ListElement {
                                    name: "Green"
                                    colorCode: "green"
                                }
                            }
                        }
        
        1 Reply Last reply
        0
        • MarkkyboyM Offline
          MarkkyboyM Offline
          Markkyboy
          wrote on last edited by Markkyboy
          #4

          Really, 'spacing' id required here, but GridView doesn't support that. There is another question here on the forum; https://forum.qt.io/topic/19775/solved-qml-gridview-and-spacing-between-children/3 which suggests cellWidth, which you say doesn't suit you. In which case, can you not use Row instead of GridView?, all I see when your code is compiled, is 4 grey radiused buttons in a row.

          If you use Row, you can individually control the width between each button by nesting each button and then you can use 'spacing: ?' to whatever you like. This is demonstrated by me on this link; https://forum.qt.io/topic/112024/vary-spacing-in-qml-row

          Should I be seeing more than just four labelled buttons from your code?

          EDIT/
          or, as a cheat, pad out the names of the buttons. 'green' is the longest name (5 characters), so add a space into the appropriate words; e.g. add 2 spaces to the end of "Red " - your buttons are now of equal width that I can see and the spacing is even.

          Don't just sit there standing around, pick up a shovel and sweep up!

          I live by the sea, not in it.

          M 1 Reply Last reply
          0
          • MarkkyboyM Markkyboy

            Really, 'spacing' id required here, but GridView doesn't support that. There is another question here on the forum; https://forum.qt.io/topic/19775/solved-qml-gridview-and-spacing-between-children/3 which suggests cellWidth, which you say doesn't suit you. In which case, can you not use Row instead of GridView?, all I see when your code is compiled, is 4 grey radiused buttons in a row.

            If you use Row, you can individually control the width between each button by nesting each button and then you can use 'spacing: ?' to whatever you like. This is demonstrated by me on this link; https://forum.qt.io/topic/112024/vary-spacing-in-qml-row

            Should I be seeing more than just four labelled buttons from your code?

            EDIT/
            or, as a cheat, pad out the names of the buttons. 'green' is the longest name (5 characters), so add a space into the appropriate words; e.g. add 2 spaces to the end of "Red " - your buttons are now of equal width that I can see and the spacing is even.

            M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #5

            @Markkyboy This is incomplete code. Each delegate will have several words and they will be of different lengths. There may be many delegates, or there may be more than one row, so row will not work.

            1 Reply Last reply
            1
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by
              #6

              it is work

              import QtQuick 2.9
              import QtQuick.Window 2.2
               
              Window {
                  visible: true
                  width: 640
                  height: 480
                  title: qsTr("Hello World")
               
                  ListModel {
                      id: textModel
                      ListElement { text: "Hello"; }
                      ListElement { text: "Hello, World!"; }
                      ListElement { text: "Flow"; }
                  }
               
                  Flow {
                      anchors.fill: parent
                      anchors.margins: 4
                      spacing: 10
               
                      Repeater {
                          model: textModel
                          Rectangle {
                              width: t_metrics.tightBoundingRect.width + 20
                              height: t_metrics.tightBoundingRect.height + 10
                              color: "gray";
                              radius: 5;
                              Text {
                                  id: currentText
                                  anchors.centerIn: parent
                                  text: model.text;
                              }
                              TextMetrics {
                                  id:     t_metrics
                                  font:   currentText.font
                                  text:   currentText.text
                              }
                          }
                      }
                  }
              }
              
              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