Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to customize ScrollBar of ListView decorated by a custom ScrollView
Forum Updated to NodeBB v4.3 + New Features

How to customize ScrollBar of ListView decorated by a custom ScrollView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 431 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    teddybeams
    wrote on 5 Mar 2024, 19:01 last edited by
    #1

    I'm working on porting a project from Qt Quick Controls 1 to Qt Quick Controls 2. In the existing codebase, there's a custom ScrollView which is used in many places to decorate a ListView. With Quick Controls 1, this offered the ability to have custom styling for scroll bars everywhere the custom ScrollView was used via the ScrollViewStyle.

    While porting this component, I've refactored the styling to work with Quick Controls 2. However, the scrollbars aren't appearing properly whenever I use this custom ScrollView. The scrollbars appear very small and aren't positioned correctly. It seems like I have to attach to the ScrollBar directly to the ListView to get them to work correctly.

    Here's a minimal, reproducible example of what I mean:

    A custom ScrollView

    // MyScrollView.qml
    import QtQuick
    import QtQuick.Controls
    
    ScrollView {
        ScrollBar.vertical: ScrollBar {
            background: Rectangle {
                color: "black"
                radius: width/2
            }
            contentItem: Rectangle {
                radius: width/2
                color: "#73D0F9"
                implicitHeight: 8
                implicitWidth: 6
            }
        }
    }
    

    main.qml

    import QtQuick
    import QtQuick.Controls
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        MyScrollView {
            anchors.fill: parent
    
            ListView {
                model: 20
                delegate: ItemDelegate {
                    text: "Item " + index
                    required property int index
                }
            }
        }
    }
    

    Produces this:
    1febada8-efb4-49fc-afe7-7f6f6508bbb0-image.png

    If I instead attach the ScrollBar to the Listview, everything seems to work, but the default ScrollBar for the ScrollView overlaps with my custom one on the ListView:

    //MyScrollView.qml
    import QtQuick
    import QtQuick.Controls
    
    ScrollView {
    }
    
    

    main.qml

    import QtQuick
    import QtQuick.Controls
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        MyScrollView {
            anchors.fill: parent
    
            ListView {
                ScrollBar.vertical: ScrollBar {
                    background: Rectangle {
                        color: "black"
                        radius: width/2
                    }
                    contentItem: Rectangle {
                        implicitHeight: 6
                        implicitWidth: 8
                        radius: width/2
                        color: "#73D0F9"
                    }
                }
    
                model: 20
                delegate: ItemDelegate {
                    text: "Item " + index
                    required property int index
                }
            }
        }
    }
    
    

    Produces this:

    b73bc63d-46bc-4dd0-8488-f310305b2d44-image.png

    Why does the ScrollBar not get positioned or scaled correctly in the first example? Is it because the ScrollBar is parented to the ScrollView instead of the ListView? I'm not sure what I'm doing wrong

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Danima
      wrote on 6 Mar 2024, 12:27 last edited by
      #2

      Try this code

      import QtQuick 2.12
      import QtQuick.Window 2.15
      import QtQuick.Controls 2.13
      
      Window
      {
          id:win1
          visible:true
          width:900
          height:300
      
          ListView {
              id:list
              width: 100
              height:parent.height
              model: 20
              delegate: ItemDelegate {
                      text: "Item " + index
                      required property int index
                  }
              ScrollBar.vertical: vsb
          }
      
          ScrollBar {
              id: vsb
              width: 16
              anchors.left: list.right
              anchors.top: parent.top
              anchors.bottom: parent.bottom
              orientation: Qt.Vertical
              policy: ScrollBar.AlwaysOn
          }
      
      }
      
      1 Reply Last reply
      0

      1/2

      5 Mar 2024, 19:01

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved