diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-01-05 08:10:53 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-01-07 08:19:31 +0000 |
commit | a3d0983f5909c37205cf0bdd4c4eba15d26d58c9 (patch) | |
tree | 73a87bd09a7e33ae0c64be2620210b13b3c54056 /src/widgets/widgets | |
parent | e35c993020228fc2f173d116bc8ba34d419ae7c9 (diff) |
QProgressBar: don't lose precision in text()
When qreal is float, it cannot represent all values an int can take,
so we may lose precision in the expression
qreal(d->value) - d->minimum
as opposed to the exact result
qint64(d->value) - d->minimum'
For lack of trying, I do not know of a value where this would change
the resulting 'progress' value, but better be safe than sorry, and use
the 64-bit integer expression instead.
Found while reviewing integer arithmetic in QProgressBar as part of
the fix for QTBUG-57857.
While touching the line, make the (intended) double → int truncation
explicit, by using a static_cast.
Change-Id: I03dbfce24c709310c3bbad9487a2bf0d1d78137a
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'src/widgets/widgets')
-rw-r--r-- | src/widgets/widgets/qprogressbar.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/src/widgets/widgets/qprogressbar.cpp b/src/widgets/widgets/qprogressbar.cpp index be6091f499..64f19047b6 100644 --- a/src/widgets/widgets/qprogressbar.cpp +++ b/src/widgets/widgets/qprogressbar.cpp @@ -485,7 +485,7 @@ QString QProgressBar::text() const return result; } - int progress = (qreal(d->value) - d->minimum) * 100.0 / totalSteps; + const auto progress = static_cast<int>((qint64(d->value) - d->minimum) * 100.0 / totalSteps); result.replace(QLatin1String("%p"), locale.toString(progress)); return result; } |