how to set the scrollbar position to max? its not 1.0 as stated in the docs

Viewed 366

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

Position Property

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

  • i don't understand whats the sense is of scaling the position between 0 and 1 but then make it directly related to bar size
  • are there any meaningful usages for position values above (1 minus bar-size)?
  • is there a way to get the bar size?

enter image description here

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
    }
}

}
1 Answers

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
Related