QML ListView定制滚动条

ListView可以设置默认的滚动条,但默认的滚动条往往不符合我们的要求,所以可以自已定制效果不同的滚动条,下面是代码示例:

//MyListView.qml

import QtQuick 2.12
import QtQuick.Controls 2.12

Item {
    id: rootItem
    width: 350
    height: 400

    ListModel {
        id: listRouteModel
        ListElement {titleText: qsTr("标题0"); titleColor: "white"}
        ListElement {titleText: qsTr("标题1"); titleColor: "white"}
        ListElement {titleText: qsTr("标题2"); titleColor: "white"}
        ListElement {titleText: qsTr("标题3"); titleColor: "white"}
        ListElement {titleText: qsTr("标题4"); titleColor: "white"}
        ListElement {titleText: qsTr("标题5"); titleColor: "white"}
        ListElement {titleText: qsTr("标题6"); titleColor: "white"}
        ListElement {titleText: qsTr("标题7"); titleColor: "white"}
        ListElement {titleText: qsTr("标题8"); titleColor: "white"}
        ListElement {titleText: qsTr("标题9"); titleColor: "white"}
        ListElement {titleText: qsTr("标题10"); titleColor: "white"}
        ListElement {titleText: qsTr("标题11"); titleColor: "white"}
        ListElement {titleText: qsTr("标题12"); titleColor: "white"}
        ListElement {titleText: qsTr("标题13"); titleColor: "white"}
        ListElement {titleText: qsTr("标题14"); titleColor: "white"}
        ListElement {titleText: qsTr("标题15"); titleColor: "white"}
        ListElement {titleText: qsTr("标题16"); titleColor: "white"}
        ListElement {titleText: qsTr("标题17"); titleColor: "white"}
        ListElement {titleText: qsTr("标题18"); titleColor: "white"}
        ListElement {titleText: qsTr("标题19"); titleColor: "white"}
        ListElement {titleText: qsTr("标题20"); titleColor: "white"}
        ListElement {titleText: qsTr("标题21"); titleColor: "white"}
        ListElement {titleText: qsTr("标题22"); titleColor: "white"}
        ListElement {titleText: qsTr("标题23"); titleColor: "white"}
        ListElement {titleText: qsTr("标题24"); titleColor: "white"}
    }


    ListView{
        id:listView
        clip: true
        width: 410;
        height: 380
        anchors.bottomMargin: 2
        model: listRouteModel;
        focus: true
        delegate: tabDelegate
    }

    //滚动条
    Rectangle {
        id: scrollbar
        x:400; y:0
        width: 10; height: 380
        radius: 10
        //按钮
        Rectangle {
            id: button
            x:0
            y: listView.visibleArea.yPosition * scrollbar.height
            width: 10
            height: listView.visibleArea.heightRatio * scrollbar.height
            color: "#818b81"
            radius: 10

            //鼠标区域
            MouseArea {
                id: mouseArea
                anchors.fill:button
                drag.target: button
                drag.axis: Drag.YAxis
                drag.minimumY: 0
                drag.maximumY: scrollbar.height
                //拖动
                onMouseYChanged: {
                    listView.contentY = button.y / scrollbar.height * listView.contentHeight
                }
            }
        }
    }
    //Component
    Component {
        id: tabDelegate
        Rectangle {
            width: 100; height: 25;
            color: (listView.currentIndex === index) ? "blue": "green"
            //标题
            Text {
                width: parent.width - 3; height: 25;
                anchors.left: parent.left;
                anchors.leftMargin: 0;
                anchors.top: parent.top
                anchors.topMargin: 0
                font.pixelSize: 16;
                color: (listView.currentIndex === index) ? "red" : titleColor
                text: titleText
                horizontalAlignment: Text.AlignHCenter; //文字水平居中对齐
                verticalAlignment: Text.AlignVCenter;//文字垂直居中对齐
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    listView.currentIndex = index

                    console.log("clicked currentIndex================" + listView.currentIndex)
                }
            }
        }
    }//end Component

}
//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12


Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    MyListView {
        id:myListView
        width: 350
        height: 400
        anchors.top: parent.top
        anchors.topMargin: 20
        anchors.left: parent.left
        anchors.leftMargin: 20
    }
}


运行效果:

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值