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>
bb10
Marc Mutz 2017-01-05 08:10:53 +01:00
parent e35c993020
commit a3d0983f59
1 changed files with 1 additions and 1 deletions

View File

@ -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;
}