qml中可以下拉查看的Text

当Text中文本过多,但是Text占用空间有限时,可把Text超出控件的部分隐藏掉,并添加一个滚动条,通过触摸屏上下翻看,或通过键盘上下键浏览,废话不多说。

            Flickable
            {
                id: flick
                anchors{left: parent.left;leftMargin: 20; verticalCenter: parent.verticalCenter;}
                anchors.centerIn: parent
                focus: true
                width: parent.width
                height: 700
                //可拖拽内容大小
                //contentWidth: textlabel.width
                contentHeight: textlabel.height
                //隐藏大于显示窗口的部分
                clip: true;
                MyComp.LightText{
                    id:textlabel
                   // anchors{left: parent.left; leftMargin: 20}
                    anchors.centerIn: parent
                    text: robotData.getVersion(true)
               }
            }

            //竖滚动条
            Rectangle
            {
                id: scrollbar_vertical
                visible: opacity>0
                opacity: 0
                Behavior on opacity{ NumberAnimation {duration: 500} }
                anchors.right: flick.right
                //当前可视区域的位置.为百分比值,0-1之间
                y: flick.visibleArea.heightRatio>=1?0:flick.visibleArea.yPosition * flick.height
                width: 10
                //当前可视区域的高度比例,为百分比值,0-1之间
                height: flick.visibleArea.heightRatio<1?flick.visibleArea.heightRatio * flick.height:0
                color: "lightsteelblue"

            }

//            //横滚动条
//            Rectangle
//            {
//                id: scrollbar_horizontal
//                anchors.bottom: flick.bottom
//                //当前可视区域的位置.为百分比值,0-1之间
//                x: flick.visibleArea.xPosition * flick.width
//                height: 10
//                //当前可视区域的宽度比例,为百分比值,0-1之间
//                width: flick.visibleArea.widthRatio * flick.width
//                color: "lightsteelblue"
//            }
            Keys.onPressed: {
                if(flick.visibleArea.heightRatio>=1)
                    return;
                if(event.key==Qt.Key_Down)
                {
                    flick.contentY+=50
                    if(flick.contentY>textlabel.height-flick.height)
                        flick.contentY=textlabel.height-flick.height
                    flipPanel.show()
                }
                if(event.key==Qt.Key_Up)
                {
                    flick.contentY-=50
                    if(flick.contentY<0)
                        flick.contentY=0
                    flipPanel.show()
                }
            }

function show(){
            scrollbar_vertical.opacity = 1
            showTimer.restart()
        }

        function hide(){
            scrollbar_vertical.opacity = 0
        }
        Timer {
            id: showTimer
            interval: 3000
            onTriggered: flipPanel.hide()
        }

在上面代码中我只需要能够上下滚动,左右滚动雷同,所以不完善了,不足之处望指教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值