Skip to content

Commit a412194

Browse files
Pasi PentikainenFriedemann Kleint
authored andcommitted
Fix mouse wheel page-by-page scrolling on windows
In windows, the page-by-page mouse wheel scrolling is configured with scroll lines value of -1, which maps to INT_MAX. The scroll calculations had an integer overflow issue which caused the wheel scrolling to scroll only downwards when configured with large enough value like this. Task-number: QTBUG-11336 Change-Id: Ib4440367ce2617f96797c1f8cc8ec9e6a2f8467c Reviewed-by: Friedemann Kleint <[email protected]> Reviewed-by: Andrew Semenenko Reviewed-by: Shane Kearns <[email protected]> (cherry picked from commit 21b9d81)
1 parent a96672b commit a412194

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/gui/widgets/qabstractslider.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,16 @@ void QAbstractSlider::sliderChange(SliderChange)
689689
update();
690690
}
691691

692+
/*!
693+
\internal
694+
695+
Truncate qreal to int without flipping on overflow.
696+
*/
697+
static inline int clampScrollStep(qreal x)
698+
{
699+
return int(qBound(qreal(INT_MIN), x, qreal(INT_MAX)));
700+
}
701+
692702
bool QAbstractSliderPrivate::scrollByDelta(Qt::Orientation orientation, Qt::KeyboardModifiers modifiers, int delta)
693703
{
694704
Q_Q(QAbstractSlider);
@@ -700,7 +710,7 @@ bool QAbstractSliderPrivate::scrollByDelta(Qt::Orientation orientation, Qt::Keyb
700710

701711
if ((modifiers & Qt::ControlModifier) || (modifiers & Qt::ShiftModifier)) {
702712
// Scroll one page regardless of delta:
703-
stepsToScroll = qBound(-pageStep, int(offset * pageStep), pageStep);
713+
stepsToScroll = qBound(-pageStep, clampScrollStep(offset * pageStep), pageStep);
704714
offset_accumulated = 0;
705715
} else {
706716
// Calculate how many lines to scroll. Depending on what delta is (and
@@ -718,14 +728,14 @@ bool QAbstractSliderPrivate::scrollByDelta(Qt::Orientation orientation, Qt::Keyb
718728
offset_accumulated += stepsToScrollF;
719729
#ifndef Q_WS_MAC
720730
// Don't scroll more than one page in any case:
721-
stepsToScroll = qBound(-pageStep, int(offset_accumulated), pageStep);
731+
stepsToScroll = qBound(-pageStep, clampScrollStep(offset_accumulated), pageStep);
722732
#else
723733
// Native UI-elements on Mac can scroll hundreds of lines at a time as
724734
// a result of acceleration. So keep the same behaviour in Qt, and
725735
// don't restrict stepsToScroll to certain maximum (pageStep):
726-
stepsToScroll = int(offset_accumulated);
736+
stepsToScroll = clampScrollStep(offset_accumulated);
727737
#endif
728-
offset_accumulated -= int(offset_accumulated);
738+
offset_accumulated -= clampScrollStep(offset_accumulated);
729739
if (stepsToScroll == 0)
730740
return false;
731741
}

0 commit comments

Comments
 (0)