ImageGestures example: decide direction based on swipeAngle

swipeTriggered() often chose the wrong direction, because the logic
was allowing either vertical or horizontal decisions to override each
other: if you swipe to the right, but also a little upwards for example,
"up" overrode "right" and you would go back instead of forward.
It worked only if you made an effort to swipe both down and right to
go forwards, and both up and left to go back.

The docs are not wrong at least: horizontalDirection or
verticalDirection is set to NoDirection only in the case that the angle
doesn't have "a horizontal component" or "a vertical component",
respectively (assuming that's even possible). So those enum properties
are only useful if you want to detect swiping on only one axis.

Instead, we now check which side of a 45 degree line the angle is on.

Pick-to: 6.10 6.9 6.8 6.5
Task-number: QTBUG-37759
Task-number: QTBUG-46195
Change-Id: Ibd78071dc78ccaa13d63200e26a614b7017a9b1b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Shawn Rutledge 2025-07-23 22:58:45 +02:00
parent 66cc853035
commit 646cef5a86
1 changed files with 9 additions and 6 deletions

View File

@ -132,13 +132,16 @@ void ImageWidget::pinchTriggered(QPinchGesture *gesture)
void ImageWidget::swipeTriggered(QSwipeGesture *gesture)
{
if (gesture->state() == Qt::GestureFinished) {
if (gesture->horizontalDirection() == QSwipeGesture::Left
|| gesture->verticalDirection() == QSwipeGesture::Up) {
qCDebug(lcExample) << "swipeTriggered(): swipe to previous";
goPrevImage();
} else {
qCDebug(lcExample) << "swipeTriggered(): swipe to next";
if (gesture->swipeAngle() < 45 || gesture->swipeAngle() > 225) {
// swipe direction right or down
qCDebug(lcExample) << "swipeTriggered(): angle"
<< gesture->swipeAngle() << "; swipe to next";
goNextImage();
} else {
// swipe direction left or up
qCDebug(lcExample) << "swipeTriggered(): angle"
<< gesture->swipeAngle() << "; swipe to previous";
goPrevImage();
}
update();
}