移动鼠标或手指时,Qml 按钮按住不触发
Qml button press and hold does not trigger when moving mouse or finger
用鼠标或手指(在支持触摸的设备上)按下 Qml 按钮并移动太多不会发出 pressAndHold() 信号。
pressAndHold()
This signal is emitted when the button is interactively pressed and
held down by the user via touch or mouse.
移动很少的像素会发出 pressAndHold() 信号,但似乎阈值非常小,并且在支持触摸的设备上这是非常明显的问题,按下按钮时手指自然会移动一点。因此 pressAndHold() 信号不会可靠地发出。
解法:
将 startDragDistance 属性 设置为高于默认值 (10)
QGuiApplication::styleHints()->setStartDragDistance(100);
解释:
查看QQuickAbstractButton源码可以找到方法:
void QQuickAbstractButtonPrivate::handleMove(const QPointF &point)
void QQuickAbstractButtonPrivate::handleMove(const QPointF &point)
{
Q_Q(QQuickAbstractButton);
QQuickControlPrivate::handleMove(point);
setMovePoint(point);
q->setPressed(keepPressed || q->contains(point));
if (!pressed && autoRepeat)
stopPressRepeat();
else if (holdTimer > 0 && (!pressed || QLineF(pressPoint, point).length() > QGuiApplication::styleHints()->startDragDistance()))
stopPressAndHold();
}
当起点到移动点的距离大于QGuiApplication::styleHints()->startDragDistance()
阈值时stopPressAndHold()
称为取消按住动作。
用鼠标或手指(在支持触摸的设备上)按下 Qml 按钮并移动太多不会发出 pressAndHold() 信号。
pressAndHold()
This signal is emitted when the button is interactively pressed and held down by the user via touch or mouse.
移动很少的像素会发出 pressAndHold() 信号,但似乎阈值非常小,并且在支持触摸的设备上这是非常明显的问题,按下按钮时手指自然会移动一点。因此 pressAndHold() 信号不会可靠地发出。
解法:
将 startDragDistance 属性 设置为高于默认值 (10)
QGuiApplication::styleHints()->setStartDragDistance(100);
解释:
查看QQuickAbstractButton源码可以找到方法:
void QQuickAbstractButtonPrivate::handleMove(const QPointF &point)
void QQuickAbstractButtonPrivate::handleMove(const QPointF &point)
{
Q_Q(QQuickAbstractButton);
QQuickControlPrivate::handleMove(point);
setMovePoint(point);
q->setPressed(keepPressed || q->contains(point));
if (!pressed && autoRepeat)
stopPressRepeat();
else if (holdTimer > 0 && (!pressed || QLineF(pressPoint, point).length() > QGuiApplication::styleHints()->startDragDistance()))
stopPressAndHold();
}
当起点到移动点的距离大于QGuiApplication::styleHints()->startDragDistance()
阈值时stopPressAndHold()
称为取消按住动作。