Qml 如何将滚动条位置设置为最大?它不是文件中规定的1.0
更新 通过使用max content.x位置和0.0-1.0系数计算所需位置来修复Qml 如何将滚动条位置设置为最大?它不是文件中规定的1.0,qml,scrollbar,Qml,Scrollbar,更新 通过使用max content.x位置和0.0-1.0系数计算所需位置来修复 Component.onCompleted: { var maxContentX = content.width - frame.width var factor = 1.0 // 0, 0.5, 1.0 var newPosition = (maxContentX*factor)/content.width hbar.position = newPosition } Qt文档:
Component.onCompleted:
{
var maxContentX = content.width - frame.width
var factor = 1.0 // 0, 0.5, 1.0
var newPosition = (maxContentX*factor)/content.width
hbar.position = newPosition
}
Qt文档:
此属性保留滚动条的位置,缩放为0.0-
1.0
但是这个位置永远不会达到1.0,因为这个位置只能容纳1个减去的条大小
- 我不明白在0和1之间缩放位置有什么意义,但要使它与条的大小直接相关
- 以上位置值(1减去条形尺寸)是否有任何有意义的用法
- 有没有办法确定酒吧的大小
我想我无法回答你问题的“为什么”部分。但是要获得滚动条的大小,我相信您可以使用滚动条的
contentItem
属性。要获得0%、50%和100%,您可能需要执行以下操作(对于水平滚动条):
感谢您的提示,仍然不是100%正确-缺少一些像素,但我已通过使用“var neededPosition=(content.width-frame.width*factor)/content.width”计算特定content.x的位置,将计算更改为与contentItem.width无关
import QtQuick 2.15
import QtQuick.Controls 2.15
Item
{
height: 300
width: 300
Column
{
Text { text: "vbar.position: "+vbar.position }
Text { text: "hbar.position: "+hbar.position }
}
Rectangle {
id: frame
clip: true
width: 160
height: 160
border.color: "black"
anchors.centerIn: parent
Text {
id: content
text: "ABC"
font.pixelSize: 160
x: -hbar.position * width
y: -vbar.position * height
}
ScrollBar {
id: vbar
hoverEnabled: true
active: hovered || pressed
orientation: Qt.Vertical
size: frame.height / content.height
anchors.top: parent.top
anchors.right: parent.right
anchors.bottom: parent.bottom
}
ScrollBar {
id: hbar
hoverEnabled: true
active: hovered || pressed
orientation: Qt.Horizontal
size: frame.width / content.width
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
}
}
}
// 0%
position: 0
// 50%
position: (width - contentItem.width) / width / 2
// 100%
position: (width - contentItem.width) / width