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. Qml a really ScrollBar?

Qml a really ScrollBar?

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 5 Posters 9.6k 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.
  • Q Offline
    Q Offline
    QtDancer
    wrote on last edited by
    #1

    I want to implement a ScrollBar in qml. you know, there is a listView and a scrollBar in my application. now, i just hope that the listView will move when I drag the scrollBar. just like a normal ScrollBar will do. i have tried lots of ways, but it doesn't work!
    can you help me ? thanks!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      msx_br
      wrote on last edited by
      #2

      From documentation (Home > Examples > QML Examples & Demos > UI Components: Scroll Bar Example) :

      @
      import Qt 4.7

      Item {
      id: scrollBar

       // The properties that define the scrollbar's state.
       // position and pageSize are in the range 0.0 - 1.0.  They are relative to the
       // height of the page, i.e. a pageSize of 0.5 means that you can see 50%
       // of the height of the view.
       // orientation can be either Qt.Vertical or Qt.Horizontal
       property real position
       property real pageSize
       property variant orientation : Qt.Vertical
      
       // A light, semi-transparent background
       Rectangle {
           id: background
           anchors.fill: parent
           radius: orientation == Qt.Vertical ? (width/2 - 1) : (height/2 - 1)
           color: "white"
           opacity: 0.3
       }
      
       // Size the bar to the required size, depending upon the orientation.
       Rectangle {
           x: orientation == Qt.Vertical ? 1 : (scrollBar.position * (scrollBar.width-2) + 1)
           y: orientation == Qt.Vertical ? (scrollBar.position * (scrollBar.height-2) + 1) : 1
           width: orientation == Qt.Vertical ? (parent.width-2) : (scrollBar.pageSize * (scrollBar.width-2))
           height: orientation == Qt.Vertical ? (scrollBar.pageSize * (scrollBar.height-2)) : (parent.height-2)
           radius: orientation == Qt.Vertical ? (width/2 - 1) : (height/2 - 1)
           color: "black"
           opacity: 0.7
       }
      

      }
      @

      msx_br - Brazil (Netherlands)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rocken
        wrote on last edited by
        #3

        My slightly modified version of Grégorys "Scrollbar":https://bitbucket.org/gregschlom/qmlscrollbar/src/tip/ScrollBar.qml:

        @
        import Qt 4.7

        Rectangle {
        id: scrollBar
        property variant target

        clip: true
        color: "#b3b3b3"
        width: 17
        radius: 10
        smooth: true
        
        anchors.top: target.top
        anchors.bottom: target.bottom
        anchors.right: target.right
        anchors.topMargin: 5
        anchors.bottomMargin: 5
        anchors.rightMargin: 5
        
        //visible: (track.height == slider.height) ? false : true //TODO: !visible -> width: 0 (but creates a binding loop)
        
        
        states: [
            State { name: "nothing"; },
            State { name: "disabled"; when: track.height == slider.height }
        ]
        transitions: [
            Transition { to: "disabled"; //reversible: true;
                SequentialAnimation {
                    NumberAnimation { target: scrollBar; property: "opacity"; to: 0; duration: 500; }
                    PropertyAction { target: scrollBar; property: "visible"; value: false; }
                }
            },
            Transition { to: "*";
                PropertyAction { target: scrollBar; property: "visible"; value: true; }
                NumberAnimation { target: scrollBar; property: "opacity"; to: 1; duration: 500; }
            }
        ]
        
        Timer {
            property int scrollAmount
        
            id: timer
            repeat: true
            interval: 20
            onTriggered: {
                target.contentY = Math.max(0, Math.min(target.contentY + scrollAmount,
                                              target.contentHeight - target.height));
            }
        }
        
        Item {
            id: track
            anchors.top: parent.top
            anchors.bottom: parent.bottom;
            width: parent.width
        
            MouseArea {
                anchors.fill: parent
                onPressed: {
                    timer.scrollAmount = target.height * (mouseY < slider.y ? -1 : 1) // scroll by a page
                    timer.running = true;
                }
                onReleased: {
                    timer.running = false;
                }
            }
        
            Rectangle {
                id:slider
                color: "#343434"
                smooth: true
                width: parent.width
                radius: 10
        
                anchors.bottom: (target.visibleArea.yPosition > 1)? parent.bottom: undefined
                height: {
                    if (target.visibleArea.yPosition<0)         // Oberer Rand
                        Math.max(30, Math.min(target.height / target.contentHeight * track.height, track.height-y) +target.height * target.visibleArea.yPosition)
                    else                                        // Mittelbereich
                        Math.min(target.height / target.contentHeight * track.height, track.height-y)
                    }
                y: Math.max(0,Math.min(track.height-30, target.visibleArea.yPosition * track.height));
        
                MouseArea {
                    anchors.fill: parent
                    drag.target: parent
                    drag.axis: Drag.YAxis
                    drag.minimumY: 0
                    drag.maximumY: track.height - height
        
                    onPositionChanged: {
                        if (pressedButtons == Qt.LeftButton) {
                            target.contentY = slider.y * target.contentHeight / track.height
                        }
                    }
                }
            }
        }
        

        }
        @
        in your QML simply add:
        @ScrollBar {
        target: lstParameter
        }@

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tiredtyrant
          wrote on last edited by
          #4

          Very useful, Rocken. Thank you!

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pmoelgaard
            wrote on last edited by
            #5

            Still useful (for legacy systems), thanks for sharing !

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pmoelgaard
              wrote on last edited by
              #6

              Still useful (for legacy systems), thanks for sharing !

              1 Reply Last reply
              0

              • Login

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