QML Flickable 在鼠标位置处放大缩小

         使用Flickable显示图片、自绘内容或者其他窗口控件时,需要对其内容进行放大缩小时,使用MouseArea修改Flickable的contentWidth即可,但此时缩放后的显示位置有偏移,解决方法为移动缩放内容位置,代码(flickView为Flickable):

MouseArea{
    anchors.fill: parent
    onWheel:{
        if(wheel.angleDelta.y>0){
            flickView.contentWidth = flickView.contentWidth*1.2
            flickView.contentHeight = flickView.contentHeight*1.2
            flickView.contentX = flickView.contentX+0.2*wheel.x
            flickView.contentY = flickView.contentY+0.2*wheel.y

        }
        else{
            flickView.contentWidth = flickView.contentWidth*0.8
            flickView.contentX = flickView.contentX-0.2*wheel.x
            flickView.contentHeight = flickView.contentHeight*0.8
            flickView.contentY = flickView.contentY-0.2*wheel.y
        }
    }
}

效果图:

QML Flickable 在鼠标位置处放大缩小

此方法会出现缩放后contentX或contentY小于Flickable的宽高时仍继续缩放的情况,可自定义判定,例如:

MouseArea{
    anchors.fill: parent
    onWheel:{
        if(wheel.angleDelta.y>0){
            flickView.contentWidth = flickView.contentWidth*1.2
            flickView.contentHeight = flickView.contentHeight*1.2
            flickView.contentX = flickView.contentX+0.2*wheel.x
            flickView.contentY = flickView.contentY+0.2*wheel.y

        }
        else{
            var newWidth = flickView.contentWidth*0.8
            if(newWidth>flickView.width){//判断缩放后的contentWidth是否会小于Flickable的width
                flickView.contentWidth = newWidth
                flickView.contentX = flickView.contentX-0.2*wheel.x
                if(flickView.contentX<0){//如果此时内容已经到达边界,则不继续移动
                    flickView.contentX=0
                }
            }
            var newHeight = flickView.contentHeight*0.8
            if(newHeight>flickView.height){
                flickView.contentHeight = newHeight
                flickView.contentY = flickView.contentY-0.2*wheel.y
                if(flickView.contentY<0){
                    flickView.contentY=0
                }
            }
        }
    }
}

 

上一篇:Windows快捷键+Dos命令


下一篇:windonw10 ,python3.7安装gevent