Qt Quick QML Flickable 禁用轻拂并仅启用滚动



我在一列中有两个Flickable,当用户滚动第二个可轻拂时,我想垂直滚动第一个可轻拂。

Rectangle { 
id: root
anchors.fill: parent
Flickable {
id: first
anchors.left: parent.left
width: parent.width/2 
height: parent.height
// Rest of the Contents
// I have also some TextFields and ComboBoxs here
}
Flickable {
id: second
anchors.left: first.right
width: parent.width/2
height: parent.height
onFlickEnded: {
first.contentY = second.contentY;
}
// Rest of the Contents
// I have also some TextFields and ComboBoxs here
}
}

我能够通过使用onFlickEnded事件来做到这一点,这是正确的方法吗? 当用户单击和拖动并使用鼠标滚轮仅具有滚动效果时,如何禁用轻弹效果?

我应该改用ScrollView吗,但我无法像使用Flickable那样滚动它。

使用 onFlickStarted,您可以立即在您想要禁用的可轻弹对象上调用 cancelFlick((。然后使用 onContentYChanged 处理它。

为了在可轻拂中禁用鼠标拖动,您需要先将交互式设置为 false,然后为其添加一个鼠标区域,如下所示:

MouseArea
{
id: mousearea
anchors.fill: parent
//so that flickable won't steal mouse event
//however, when the flickable is flicking and interactive is true
//this property will have no effect until current flicking ends and interactive is set to false
//so we need to keep interactive false and scroll only with flick()
preventStealing: true
//scroll velocity
property real nextVelocity: 0
property real curWeight: baseWeight
property real baseWeight: 4
property real maxWeight: 12
property real stepWeight: 2
property real maxVelocity: 2400
property real minVelocity: -2400
Timer
{
id: timer
interval: 1000 / 60
repeat: false
running: false
onTriggered: parent.scroll()
}
function scroll()
{
var velocity = -parent.verticalVelocity + nextVelocity
parent.flickDeceleration = Math.abs(velocity) * 2.7
console.log(nextVelocity, parent.verticalVelocity, velocity)
parent.flick(0, velocity)
nextVelocity = 0
curWeight = baseWeight
}
onWheel:
{
wheel.accepted = true
var deltay = wheel.angleDelta.y
nextVelocity += curWeight * deltay
if(nextVelocity > maxVelocity)
nextVelocity = maxVelocity
else if(nextVelocity < minVelocity)
nextVelocity = minVelocity
curWeight += stepWeight
if(curWeight > maxWeight)
curWeight = maxWeight
timer.start()
}
//make sure items can only be clicked when view is not scrolling
//can be removed if you don't need this limitation
onPressed:
{
mouse.accepted = parent.moving ? true : false
}
onReleased:
{
mouse.accepted = parent.moving ? true : false
}
onClicked:
{
mouse.accepted = parent.moving ? true : false
}
}

这可以防止鼠标单击传播到可轻拂对象,并且随着滚轮滚动速度的增加而以增加的速度滚动,同时将轻拂持续时间保持在固定数字附近。您可以根据需要进行调整。

将"交互式"设置为false应该会禁用轻拂。

您可以使用"onContentYChanged"代替"onFlickEnded"。

最新更新