0

How do I get the scroll position of a QtQuick2 ScrollView? I tried scrollView.contentItem.y and scrollView.viewport.y but both return 0 always.

I think one solution might be to put a Flickable as the ScrollView's child. Is that gonna solve the problem? Also to me that's a hack, any way that's not a hack?

1
  • 1
    You really only need the flickable.
    – dtech
    Apr 19, 2017 at 18:32

1 Answer 1

0

You are right, the contentItem.x/y are always 0. However you can retrive the values you are looking for from either the parent of the contentItem (which is not the ScrollView but the contentItem of a Flickable, the flickableItem) with contentItem.parent.x/yor from the flickableItem with flickableItem.contentX/Y. The relation between both is: flickableItem.contentX === -flickableItem.contentItem.x

import QtQuick 2.0
import QtQuick.Controls 1.4

ApplicationWindow {
    id: root
    visible: true
    width: 400; height: 450

    ScrollView {
        id: sv
        anchors.fill: parent
        contentItem: Rectangle {
            id: rect
            width: 800
            height: 800
            gradient: Gradient {
                GradientStop { position: 0; color: 'grey' }
                GradientStop { position: 1; color: 'red' }
            }
        }
    }

    Text {
        anchors.centerIn: parent
        text: sv.contentItem.parent.x + ' / ' + sv.contentItem.parent.y + '\n'
              + sv.flickableItem.contentX  + ' / ' + sv.flickableItem.contentY + '\n'
              + sv.flickableItem.contentItem.x  + ' / ' + sv.flickableItem.contentItem.y + '\n'
              + sv.flickableItem.contentItem + '\t' + sv.contentItem.parent // Just to suport my claim, both are the same
        color: 'white'
    }
}

As dtech already pointed out: It might be better, to use the Flickable directly. If needed, you can attatch ScrollBars from the new QtQuick.Controls 2.x to it.

I have no measurements, but the QTees were boasting about, that the new QtQuick.Controls 2.x are supperior performancewise. By the way, they are easier to use and to style, but don't support the native look and feel. Here you would have the properties contentX/Y

1
  • 1
    You should refer to flickableItem, see e.g. my answer here.
    – BaCaRoZzo
    Apr 20, 2017 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.