QML时间滚筒控件

文章详细介绍了如何在QML中使用PathView组件创建一个动态的时间滚筒控件,包括设置模型、路径、文本显示和焦点处理。
摘要由CSDN通过智能技术生成

QML时间滚筒控件

使用PathView来实现,重要代码如下:
///时的数值
    Rectangle{
        id: _hourRec
        width: 80; height: parent.height
        anchors.left: parent.left; anchors.leftMargin: 0
        anchors.top: parent.top; anchors.topMargin: 0
        color: "white"

        Component {
                id: _hourDelegate

                Text {
                    opacity: PathView.isCurrentItem ? 1 : 1
                    text: name;
                    font.pixelSize: 24
                    anchors.leftMargin: 2
                    width: 80

                    leftPadding: 30
                }

            }
        PathView{
                id:_hourPathview
                anchors.fill: parent
                model:_hourModel
                delegate: _hourDelegate
                pathItemCount: 5  ///设置在路径上显示项的个数
                path: Path{
                    startX: _hourRec.x + 40; startY: _hourRec.y + 15
                    PathLine { x: _hourRec.x + 40; y: _hourRec.y + 185 }
                }

//                highlight: Rectangle{
//                    width: 20;height: 20
//                    color: "lightBlue"
//                }

                focus: true //获取焦点
                //Keys.onLeftPressed: decrementCurrentIndex()//添加左右键盘移动
                //Keys.onRightPressed: incrementCurrentIndex()
                Keys.onUpPressed: decrementCurrentIndex()
                Keys.onDownPressed: incrementCurrentIndex()

				///将当前项下移两位
                onCurrentIndexChanged: {
                    var tempNum = 0
                    if(currentIndex + 2 < 100)
                    {
                        tempNum = currentIndex + 2
                    }else{
                        tempNum = currentIndex + 2 - 100
                    }

                    if(tempNum < 10)
                        _hourMiddleText.text = "0" + tempNum
                    else{
                         _hourMiddleText.text = tempNum
                    }

                }


                Component.onCompleted: {
                    positionViewAtIndex(count,PathView.Beginning)
                    _hourMiddleText.text = "02"
                }

            }

		///因为currentItem是在第一个,要下移
        Rectangle{
            id: _hourMiddle
            width: parent.width;height: 30
            color: "lightBlue"
            z: 1
            anchors.centerIn: parent

            Text {
                id: _hourMiddleText
                anchors.centerIn: parent
                font.bold: true
                font.pixelSize: 20
            }

        }

    }


在这里插入图片描述
在这里插入图片描述

整个文件代码:使用积分下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值