我有一个自定义按钮:
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
}
}
Run Code Online (Sandbox Code Playgroud)
它有一个父级Rectangle:
Rectangle {
width: 500
height: 500
Grid {
anchors.centerIn: parent
rows: 3; columns: 3; spacing: 0
Repeater {
model: 9
Xobtn { }
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要为Gridparent中的所有可用空间拉伸所有按钮Rectangle。我怎样才能做到这一点?谢谢。
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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您出于某种原因不想依赖QtQuick.Layouts,那么您必须自己计算width和height,但是如果您只需要统一网格之外的任何东西,这将相当笨重,例如:
Xobtn {
height: grid.parent.height / grid.columns - grid.spacing
width: grid.parent.width / grid.rows - grid.spacing
}
Run Code Online (Sandbox Code Playgroud)
Gridgrid的 id在哪里。