我有一个表格视图,我想用滑块滚动信息
如何获取模型的长度并绑定到滑块的值 我尝试过
onValueChanged: { console.log("Slider value:", control.value); console.log("Rounded value:", Math.round(control.value)); Model.currentRow = Math.round(control.value); }
但不工作
如果您的数据可以用
ListModel
表示,您可以考虑将它们呈现在 ListView
中,其中每个委托填充整个 ListView
。这样您一次只能看到一名代表。然后,ListView
自己的ScrollBar
可用于实现滑块以在这些委托之间导航。
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
ListModel {
id: pets
ListElement { name: "cat"; color: "black" }
ListElement { name: "dog"; color: "brown" }
ListElement { name: "bird"; color: "white" }
}
ListView {
anchors.fill: parent
model: pets
orientation: ListView.Horizontal
ScrollBar.horizontal: ScrollBar {
height: 40
policy: ScrollBar.AlwaysOn
stepSize: pets.count > 1 ? 1.0 / (pets.count-1) : 0
snapMode: ScrollBar.SnapOnRelease
}
delegate: Frame {
width: ListView.view.width
height: ListView.view.height - 40
background: Rectangle { color: index & 1 ? "#aaa" : "#eee" }
ColumnLayout {
anchors.centerIn: parent
height: parent.height
Text { text: "index: %1".arg(index) }
Text { text: model.name }
Text { text: model.color }
}
}
}
}
您可以在线尝试!