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. Qml Repeater Elements dynamic size
QtWS25 Last Chance

Qml Repeater Elements dynamic size

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 3.4k 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.
  • kingwill101K Offline
    kingwill101K Offline
    kingwill101
    wrote on last edited by
    #1

    ive been trying to dynamically generate/resizing a keypad using the Repeater
    but for some reason the layout often only shows one element instead of all.

    If i use a fixed height or width instead of changingWidth/changingHeight it is rendered properly but if i try to set it after the window has been resized thats where the problem comes in.

    Any help would be appreciated

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
    
        property var keys: ["1", "2", "3", "QTY", "4", "6", "5", "Disc", "7", "8", "9", "Price", "+/-", "0", ".", "<="]
    
        property var changingWidth: 0
        property var changingHeight: 0
    
        GridLayout {
            id: grid
            anchors.fill: parent
    
            columns: 4
    
            onHeightChanged: function () {
                changingHeight = height / 4
            }
    
            onWidthChanged: function () {
                changingWidth = width / 4
            }
    
            Repeater {
                model: keys.length
                delegate: Rectangle {
                    width: changingWidth
                    height: changingHeight
                    Text {
                        text: keys[index]
                        anchors.centerIn: parent
                    }
                }
            }
        }
    }
    
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      BrunoGeorgevich
      wrote on last edited by
      #2

      Have you tried to use GridView instead of GridLayout? With GridView you dont need to use Repeater, because it have a property called "model" where you passes your model or array.

      http://doc.qt.io/qt-5/qml-qtquick-gridview.html
      http://doc.qt.io/qt-5/qml-qtquick-gridview.html#model-prop

      import QtQuick 2.5
      import QtQuick.Controls 1.4
      import QtQuick.Layouts 1.0
      
      ApplicationWindow {
          visible: true
          width: 640
          height: 480
      
          property var keys: ["1", "2", "3", "QTY", "4", "6", "5", "Disc", "7", "8", "9", "Price", "+/-", "0", ".", "<="]
      
          GridView {
              id: grid
              property int columns : 4
              property int rows : 4
              anchors.fill: parent
              cellWidth: width/columns
              cellHeight: height/rows
              model:keys
              delegate: Rectangle {
                      width: grid.cellWidth
                      height: grid.cellHeight
                      Text {
                          text: keys[index]
                          anchors.centerIn: parent
                      }
                  }
              }
          }
      

      try this code above, i dont have total sure it will works, but it something like i've done above.

      kingwill101K 1 Reply Last reply
      1
      • B BrunoGeorgevich

        Have you tried to use GridView instead of GridLayout? With GridView you dont need to use Repeater, because it have a property called "model" where you passes your model or array.

        http://doc.qt.io/qt-5/qml-qtquick-gridview.html
        http://doc.qt.io/qt-5/qml-qtquick-gridview.html#model-prop

        import QtQuick 2.5
        import QtQuick.Controls 1.4
        import QtQuick.Layouts 1.0
        
        ApplicationWindow {
            visible: true
            width: 640
            height: 480
        
            property var keys: ["1", "2", "3", "QTY", "4", "6", "5", "Disc", "7", "8", "9", "Price", "+/-", "0", ".", "<="]
        
            GridView {
                id: grid
                property int columns : 4
                property int rows : 4
                anchors.fill: parent
                cellWidth: width/columns
                cellHeight: height/rows
                model:keys
                delegate: Rectangle {
                        width: grid.cellWidth
                        height: grid.cellHeight
                        Text {
                            text: keys[index]
                            anchors.centerIn: parent
                        }
                    }
                }
            }
        

        try this code above, i dont have total sure it will works, but it something like i've done above.

        kingwill101K Offline
        kingwill101K Offline
        kingwill101
        wrote on last edited by
        #3

        @BrunoGeorgevich thanks man you are a lifesaver!!!

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved