QML. Как да разтегнете Grid за цялото родителско пространство

Имам персонализиран бутон:

import QtQuick 2.0

Rectangle {
    id: xobutton
    width: 100
    height: 100
    property string label: "?"
    border.width: 2

    Text {
        id: xobuttonLabel
        text: label
        font.pointSize: 75
        anchors.centerIn: parent
    } 
}

Има родител Rectangle:

Rectangle {
    width: 500
    height: 500

    Grid {
        anchors.centerIn: parent
        rows: 3; columns: 3; spacing: 0

        Repeater {
            model: 9
            Xobtn { }
        }
    } 
}

Трябва да разтегна всички бутони в Grid за цялото свободно пространство в родител Rectangle. Как мога да направя това? Благодаря.


person xskit    schedule 23.05.2015    source източник


Отговори (2)


Grid просто позиционира елементите в него, като спазва размера на елементите. За да преоразмерите елементите активно, трябва да използвате GridLayout и му кажете как елементите трябва да бъдат преоразмерени с Layout прикачени свойства . Така вашият код става нещо подобно, коментари, показващи промени в кода ви:

import QtQuick.Layouts 1.1 // needed for GridLayout

Rectangle {
    width: 500
    height: 500

    GridLayout {
        anchors.fill: parent // GridLayout must have the right size now
        rows: 3; columns: 3
        columnSpacing: 0; rowSpacing: 0 // changed from spacing: 0

        Repeater {
            model: 9
            Xobtn {
                // attached properties guiding resizing
                Layout.fillHeight: true
                Layout.fillWidth: true 
            }
        }
    }
}

Ако по някаква причина не искате да разчитате на QtQuick.Layouts, тогава трябва сами да изчислите width и height, но това е доста тромаво, ако имате нужда от нещо друго освен еднаква мрежа, нещо като:

        Xobtn {
            height: grid.parent.height / grid.columns - grid.spacing
            width: grid.parent.width / grid.rows - grid.spacing
        }

където grid ще бъде идентификаторът на решетката.

person hyde    schedule 23.05.2015

Без да използвате QtQuick.Layouts, които са малко по-скъпи, можете просто да направите нещо подобно (но трябва да знаете размера на елементите, които поставяте в мрежата:

import QtQuick 2.0

Rectangle {
    id: root
    width: 500
    height: 500

    readonly property int rectSize: 100

    Grid {
        anchors.centerIn: parent
        rows: 3; columns: 3;

        columnSpacing: (parent.width - (root.rectSize * columns)) / (columns - 1)
        rowSpacing: (parent.height - (root.rectSize * rows)) / (rows - 1)

        Repeater {
            model: parent.rows * parent.columns

            Rectangle {
                id: xobutton
                width: root.rectSize
                height: root.rectSize
                property string label: "?"
                border.width: 2

                Text {
                    id: xobuttonLabel
                    text: label
                    font.pointSize: 75
                    anchors.centerIn: parent
                }
            }
        }
    }
}
person Treviño    schedule 07.10.2016