【发布时间】:2015-11-07 00:15:18
【问题描述】:
我想在我的 QML 应用程序中添加一个网格,它的行和列可以在运行时调整大小。
想想excel表格。我可以通过上下拖动顶部/底部边框来调整整行的大小。我可以通过左右拖动右/左边框来调整整个列的大小。这类似于具有水平和垂直方向的 SplitView。
我一直在谷歌上寻找答案,但不断得到不是我想要的结果。
有什么想法吗?
【问题讨论】:
我想在我的 QML 应用程序中添加一个网格,它的行和列可以在运行时调整大小。
想想excel表格。我可以通过上下拖动顶部/底部边框来调整整行的大小。我可以通过左右拖动右/左边框来调整整个列的大小。这类似于具有水平和垂直方向的 SplitView。
我一直在谷歌上寻找答案,但不断得到不是我想要的结果。
有什么想法吗?
【问题讨论】:
GridView 始终是固定单元格大小。您应该尝试使用 TableView
【讨论】:
这没什么可做的。只需使用 QML 绑定和锚定来实现此目的。
import QtQuick 2.0
Item {
width: 500; height: 500
GridView {
id: gridView
width: 300; height: 200
cellWidth: 80; cellHeight: 80
Component {
id: contactsDelegate
Text {
id: contactInfo
text: modelData
}
}
model: 5
delegate: contactsDelegate
}
Rectangle {
id: add
width: 100
height: 20
border.color: "red"
anchors {
top: parent.top
topMargin: 10
right: parent.right
rightMargin: 5
}
Text {
anchors.fill: parent
text: "Add Item"
}
MouseArea {
anchors.fill: parent
onClicked: gridView.model++
}
}
Rectangle {
id: newWidth
width: 100
height: 20
border.color: "red"
anchors {
top: add.bottom
topMargin: 10
right: parent.right
rightMargin: 5
}
Text {
anchors.fill: parent
text: "New Width"
}
MouseArea {
anchors.fill: parent
onClicked: gridView.width += 100
}
}
Rectangle {
width: 100
height: 20
border.color: "red"
anchors {
top: newWidth.bottom
topMargin: 10
right: parent.right
rightMargin: 5
}
Text {
anchors.fill: parent
text: "New Height"
}
MouseArea {
anchors.fill: parent
onClicked: gridView.height += 100
}
}
}
或者如果您想通过调整窗口大小来更改GridView 的width 和height,请执行以下操作:
import QtQuick 2.0
Item {
width: 500; height: 500
GridView {
id: gridView
anchors {
top: parent.top
left: parent.left
right: parent.right
bottom: parent.bottom
bottomMargin: 35
}
clip: true
cellWidth: 80; cellHeight: 80
Component {
id: contactsDelegate
Text {
id: contactInfo
text: modelData
}
}
model: 5
delegate: contactsDelegate
}
Rectangle {
id: add
width: 100
height: 20
border.color: "red"
anchors {
bottom: parent.bottom
bottomMargin: 10
horizontalCenter: parent.horizontalCenter
}
Text {
anchors.fill: parent
text: "Add Item"
}
MouseArea {
anchors.fill: parent
onClicked: gridView.model++
}
}
}
【讨论】: