qml下拉条实现

qml下拉条实现

代码结构

Rectangle里面嵌套一个Flickable,然后下面是一个Rectangle,作为滑动的区域,给最外层的Rectangle的y加一个属性动画。滑动区域写好onPressed和Onrelease即可。

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

Window {
    visible: true
    width: 1024
    height: 480
    title: qsTr("Drag Icon")

    Rectangle{
        id:root
        x:0
        y:0
        height:480
        width : 310
        border.color: "black"
        Flickable {
            id: flickable
            width: 300
            height: 420
            clip: true
            contentWidth: contentItem.childrenRect.width
            contentHeight: contentItem.childrenRect.height

            property bool show: true

            Column {
                id: contentColumn
                width: flickable.width
                spacing: 10

                Repeater {
                    model: 20
                    delegate: Rectangle {
                        width: flickable.width - 20
                        height: 50
                        color: index % 2 === 0 ? "lightblue" : "lightgreen"
                        Text {
                            anchors.centerIn: parent
                            text: "Item " + (index + 1)
                        }
                    }
                }
            }

            ScrollBar.vertical: ScrollBar {
                policy: ScrollBar.AlwaysOn
            }
        }

        Rectangle{
            id:rec
            anchors.top: flickable.bottom
            anchors.topMargin: 20
            height : 20
            width : 300
            color:"red"
            property real startY
            property real threshold : 5
            property bool isopen : true

            Component.onCompleted: {
                rec.isopen = true
            }

            MouseArea{
                anchors.fill: parent
                onClicked: {
                    // rec.startY = mouse.y;
                    // var position = rec.mapToItem(null,mouse.x,mouse.y)
                    // console.log("rec.startY = " + rec.startY)
                    // console.log("position.y = " +position.y)
                    // if(root.y ===0){
                    //     yAnimation.to = -450
                    //     yAnimation.start()
                    // }else{
                    //     yAnimation.to = 0
                    //     yAnimation.start()
                    // }
                }
                onPressed: {
                    rec.startY = mouse.y
                    console.log("onpressed startY = " + rec.startY)
                }

                onPressAndHold: {

                }

                onReleased: {
                    var curY = mouse.y
                    console.log("curY = " + curY)
                    // console.log("curY = " + curY)
                    // console.log("curY-rec.startY = " + (curY-rec.startY))
                    // console.log("rec.startY-curY= " + (rec.startY-curY))
                    console.log("rec.isopen = " + rec.isopen)
                    if(rec.isopen){
                        if((rec.startY-curY)>rec.threshold){
                            // 开始上滑
                            rec.isopen  = false
                            yAnimation.to = -450
                            yAnimation.start()
                        }
                    }else{
                        if((curY-rec.startY)>rec.threshold){
                            // 开始下滑
                            rec.isopen = true
                            yAnimation.to = 0
                            yAnimation.start()
                        }
                    }
                    console.log("rec.isopen = " + rec.isopen)
                }

                onPositionChanged:{

                }
            }
        }
        
        Button {
            id: btn
            text: "btn"
            anchors.top: rec.bottom
            onClicked: {
                console.log("flickable.contentHeight = " + flickable.contentHeight)
                console.log("contentColumn.height = " + contentColumn.height)
                console.log("root.y = " + root.y)
                console.log("rec.isopen = " + rec.isopen)
                console.log("rec.startY = " + rec.startY)
            }
        }

        NumberAnimation {
            id: yAnimation
            target: root
            property: "y"
            duration: 200
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值