Reputation: 737
UPDATE
Fixed by calculating the needed position using the max content.x position and a 0.0-1.0 factor
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-Docs: ScrollBar QML Type
This property holds the position of the scroll bar, scaled to 0.0 - 1.0.
but the position never reaches 1.0 - because the position only holds 1 minus bar size
any idea how to calculate the correct mid/max values for the position? i want to build a timer that toggles between 0%, 50% and 100% position
Qt-Sample: Non-attached Scroll Bars
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
}
}
}
Upvotes: 2
Views: 899
Reputation: 8277
I don't think I can answer the "why" part of your question. But to get the bar size, I believe you can use the Scrollbar's contentItem
property. To get 0%, 50%, and 100%, you could probably do something like this (for a horizontal scrollbar):
// 0%
position: 0
// 50%
position: (width - contentItem.width) / width / 2
// 100%
position: (width - contentItem.width) / width
Upvotes: 2