0

I am using Qt 5.2.1 and I have a custom scrollbar.

import QtQuick 1.1

Item {
    id: root
    property variant flickable: parent
    property int rightSpacing: 5
    property int scrollStep: 25
    anchors.top: parent.top
    //anchors.topMargin: -50
    anchors.bottom: parent.bottom
    anchors.right: parent.right
    width: 16
    z: 4

    Rectangle {
        id: up
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.rightMargin: rightSpacing
        visible: scrollbar.visible

        color: "dark grey"
        height: 16
        width: 16
        border.width: 1
        border.color: "black"

        MouseArea {
            anchors.fill: parent

            Image {
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.horizontalCenterOffset: 0.5

                source: theme.location + "triangle.png"
                height: 11
                width: 11
                fillMode: Image.PreserveAspectFit
            }

            onClicked: {

                if((flickable.contentY - scrollStep) <= 0) {
                    flickable.contentY = 0
                }
                else {
                    flickable.contentY = flickable.contentY - scrollStep
                }
            }

            onReleased: {
                up.color = "dark grey"
            }

            onPressed: {
                up.color = "light grey"
            }
        }
    }

    Rectangle {
        id: scrollbar


        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.right: parent.right

        anchors.topMargin: 16
        anchors.bottomMargin: 16
        anchors.rightMargin: rightSpacing

        width: 16//0.03 * parent.width
        radius: 0//0.5 * width
        color: "lightgrey"
        visible: flickable.visibleArea.heightRatio < 1.0
        clip: true

        border.width: 1
        border.color: "black"

        Rectangle {
            id: handle
            width: parent.width
            height: flickable.visibleArea.heightRatio * scrollbar.height
            color: "dark grey"
            opacity: clicker.drag.active ? 0.7 : 0.4
            radius: 0//0.5 * width
            border.width: 1
            border.color: "black"

        }

        Binding {
            target: handle
            property: "y"
            value: flickable.visibleArea.yPosition * scrollbar.height
            when: !clicker.pressed
        }

        MouseArea {
            id: clicker
            anchors.fill: parent

            drag {
                target: handle
                minimumY: 0
                maximumY: scrollbar.height - handle.height
                axis: Drag.YAxis
            }

            onMouseYChanged: {
                flickable.contentY = handle.y / drag.maximumY * (flickable.contentHeight - flickable.height)
            }

            onClicked: {
                flickable.contentY = mouse.y / scrollbar.height * (flickable.contentHeight - flickable.height)
            }
        }
    }

    Rectangle {
        id: down
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.rightMargin: rightSpacing
        visible: scrollbar.visible

        color: "dark grey"
        height: 16
        width: 16
        border.width: 1
        border.color: "black"

        MouseArea {
            anchors.fill: parent

            Image {
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.horizontalCenterOffset: 0.5

                source: theme.location + "triangle.png"
                height: 11
                width: 11
                fillMode: Image.PreserveAspectFit

                rotation: 180
            }

            onClicked: {
                if((flickable.contentY + scrollStep) > (flickable.contentHeight - flickable.height)) {
                    flickable.contentY = (flickable.contentHeight - flickable.height)
                }
                else {
                    flickable.contentY = flickable.contentY + scrollStep
                }
            }

            onReleased: {
                down.color = "dark grey"
            }

            onPressed: {
                down.color = "light grey"
            }
        }
    }
}

This the how the scroll bar is used with the ListView

        ListView
        {
            id: listView
            focus: elist.activeWindow
            anchors.top: listHeader.bottom
            anchors.topMargin: 2
            anchors.bottom: parent.bottom
            anchors.bottomMargin: details.visible ? 210 : 1
            width: parent.width
            model: eventProxyModel
            delegate: delListItem
            highlight: delListHighlight
            highlightMoveDuration: 0
            highlightFollowsCurrentItem: true
            property bool sortOrder: false
            interactive: false
            boundsBehavior: Flickable.StopAtBounds
            snapMode: ListView.SnapToItem
            clip: true

            VScrollBar {
                id: listScrollBar
                anchors.rightMargin: -5
                anchors.bottomMargin: 1
            }
        }

The problem is that when about 40 items are more are inserted into the model. I have a script that inserts a lot of elements and I can scroll to the top and bottom and everything behaves just fine. When I go to insert 1 more item and scroll up the top element is missing and can scroll up to view it. When I scroll to the bottom there is an empty space at the bottom of the ListView. I can also use the handle to scroll past the bottom and the ListView will continue to scroll past the boundary. If I keep adding elements, top elements are not accessible and the bottom becomes bigger with empty elements and the scrollable area becomes larger.

I've been going at for days and would like some guidance on what may be causing this behavior, thank you.

  • Are you unable to do what you want with QtQuick.Controls Scrollbar? (I don't remember if Controls even existed in 5.2.1) – JarMan Mar 25 '21 at 03:27
  • I'm not sure, but I will definitely check if it exists – Patricio Torres Mar 25 '21 at 05:47
  • If you could isolate this code into a project we can compile and replicate the issue, that would help. Also, even though it sounds like you have to use Qt 5.2 (?) you may want to download Qt 5.15.3 and see if the same thing happens there. – David K. Hess Mar 25 '21 at 14:02
  • I checked and found QtQuick.Controls 1 was added in 5.1. So you should be able to use a [ScrollView](https://doc.qt.io/qt-5/qml-qtquick-controls-scrollview.html), which can be customized with a [ScrollViewStyle](https://doc.qt.io/qt-5/qml-qtquick-controls-styles-scrollviewstyle.html). – JarMan Mar 25 '21 at 14:03
  • Does QtQuick.Controls conflict with Qt Declarative Module? I attempted to add QtQuick.Controls to my project and it says it's not installed. – Patricio Torres Mar 25 '21 at 20:41

0 Answers0