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
}
}
}
效果图:
此方法会出现缩放后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
}
}
}
}
}
标签:wheel,鼠标,0.2,Flickable,contentY,QML,flickView,contentWidth,contentX 来源: https://blog.csdn.net/zjgo007/article/details/120432558