From e80f981cca98f4fbf16dfed6e2f93748de6415d0 Mon Sep 17 00:00:00 2001
From: Fatih Uzunoglu <fuzun54@outlook.com>
Date: Fri, 28 Mar 2025 21:56:52 +0200
Subject: [PATCH] qml: create the scroll bar depending on the orientation in
 `ListViewExt`

By default we expect the viewport to cover the content width or height
depending on the orientation, so in normal cases we only need one scroll
bar. This behavior can obviously be overridden where the list view is
reused.

This fixes multiple things:
- Not being able to interact with the bottom part of the list view even
if the orientation is vertical and the viewport covers the whole content
width, because the scroll bar may still be technically visible and consume
events even though it does not display anything visually.
- Flashing scroll bar upon creation of the view until the content and
viewport sizes are determined (regarding the orientation where scroll bar
is not wanted).
---
 modules/gui/qt/widgets/qml/ListViewExt.qml | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/modules/gui/qt/widgets/qml/ListViewExt.qml b/modules/gui/qt/widgets/qml/ListViewExt.qml
index ab4bbce99d61..26760c1c3735 100644
--- a/modules/gui/qt/widgets/qml/ListViewExt.qml
+++ b/modules/gui/qt/widgets/qml/ListViewExt.qml
@@ -44,6 +44,10 @@ ListView {
     property var isDropAcceptableFunc
     property var acceptDropFunc
 
+    property Component defaultScrollBar: Component {
+        ScrollBarExt { }
+    }
+
     // Private
 
     property bool _keyPressed: false
@@ -81,8 +85,18 @@ ListView {
     keyNavigationEnabled: false
     keyNavigationWraps: false
 
-    ScrollBar.vertical: ScrollBarExt { }
-    ScrollBar.horizontal: ScrollBarExt { }
+    ScrollBar.vertical: {
+        // By default vertical scroll bar is only used when the orientation is vertical.
+        if (root.defaultScrollBar && (root.orientation === ListView.Vertical))
+            return root.defaultScrollBar.createObject() // rely on JS/QML engine's garbage collection
+        return null
+    }
+    ScrollBar.horizontal: {
+        // By default horizontal scroll bar is only used when the orientation is horizontal.
+        if (root.defaultScrollBar && (root.orientation === ListView.Horizontal))
+            return root.defaultScrollBar.createObject() // rely on JS/QML engine's garbage collection
+        return null
+    }
 
     flickableDirection: Flickable.AutoFlickIfNeeded
 
-- 
GitLab