QML ScrollBar.AsNeeded background track always visible on empty ListView when using Basic style
-
Hi everyone,
I am working with Qt 6.9.3 and customizing a ScrollBar using the QtQuick.Controls.Basic style. I have encountered an issue where the ScrollBar.AsNeeded policy does not work as expected when attached to a ListView.When the ListView is completely empty (or content fits within bounds), the scroll handle/thumb correctly hides, but the custom background track remains permanently visible on the screen. If I switch back to the default QtQuick.Controls style without customization, the AsNeeded policy works perfectly.
Here is the custom ScrollBar implementation I am using in a standalone file:
// CustomScrollBar.qml import QtQuick import QtQuick.Controls.Basic ScrollBar { id: control policy: ScrollBar.AsNeeded minimumSize: 0.1 hoverEnabled: false contentItem: Rectangle { implicitWidth: 10 implicitHeight: 10 color: control.pressed ? "#444" : "#888" // Handle correctly tracks active/size states visible: control.policy === ScrollBar.AlwaysOn || (control.active && control.size < 1.0) } background: Rectangle { implicitWidth: control.implicitWidth color: "#E0E0E0" // This background remains visible even when ListView is empty (control.size == 1.0) } }And how it is attached:
ListView { id: listView Layout.fillHeight: true Layout.fillWidth: true clip: true model: 0 // Empty for testing ScrollBar.vertical: CustomScrollBar { anchors.right: listView.right } }Is there a specific internal property or visibility binding for the background item in the Basic style template that I need to manually handle to make it respect the AsNeeded policy when the view has no overflow?
Thanks for your help!
-
Add a similar visible property to the background element inside your CustomScrollBar.qml.
-
Thanks for the answer, @Nils-Sjoberg , but unfortunately, that solution didn't work.
When I add the visibility logic directly to the background property in
CustomScrollBar.qml (visible: control.policy === ScrollBar.AlwaysOn || (control.active && control.size < 1.0)),
the scrollbar appearing only when scrolling overflowing items. But the scrollbar is not visible when items are overflowing the list without scrolling.
However, according to QML specifications, we shouldn't need to manually override the background's visible property to get standard policy behavior.
-
Thanks for the answer, @Nils-Sjoberg , but unfortunately, that solution didn't work.
When I add the visibility logic directly to the background property in
CustomScrollBar.qml (visible: control.policy === ScrollBar.AlwaysOn || (control.active && control.size < 1.0)),
the scrollbar appearing only when scrolling overflowing items. But the scrollbar is not visible when items are overflowing the list without scrolling.
However, according to QML specifications, we shouldn't need to manually override the background's visible property to get standard policy behavior.
@Gary90 said in QML ScrollBar.AsNeeded background track always visible on empty ListView when using Basic style:
Thanks for the answer, @Nils-Sjoberg , but unfortunately, that solution didn't work.
When I add the visibility logic directly to the background property in
CustomScrollBar.qml (visible: control.policy === ScrollBar.AlwaysOn || (control.active && control.size < 1.0)),
the scrollbar appearing only when scrolling overflowing items. But the scrollbar is not visible when items are overflowing the list without scrolling.
However, according to QML specifications, we shouldn't need to manually override the background's visible property to get standard policy behavior.
background: Rectangle { implicitWidth: control.implicitWidth color: "#E0E0E0" visible: control.policy === ScrollBar.AlwaysOn || control.size < 1.0 } -
Remove control.active && from the condition.