QStylesheetStyle: don't lose precision when drawing a progress bar

When qreal is float, it cannot represent all values an int can take,
so we may lose precision in the expression

  qreal(a) / b

as opposed to the double result

  double(a) / b

For lack of trying, I do not know of a value where this would change
the resulting 'fillWidth' value, but better be safe than sorry, and
use double instead of qreal arithmetic.

Also, when calculating fillRatio, we were converting values back and
forth between qreal and double. Using double everywhere avoids that.

Found while reviewing integer arithmetic in QProgressBar as part of
the fix for QTBUG-57857.

Change-Id: I054cb11d35e3ecf5bf79b5c8ee39029bd23bcf49
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Marc Mutz 2017-01-05 08:10:53 +01:00
parent 555a0f3c51
commit 1d1b60dee4

View File

@ -3903,8 +3903,8 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q
if (inverted) if (inverted)
reverse = !reverse; reverse = !reverse;
const bool indeterminate = pb->minimum == pb->maximum; const bool indeterminate = pb->minimum == pb->maximum;
qreal fillRatio = indeterminate ? 0.50 : qreal(progress - minimum)/(maximum - minimum); const auto fillRatio = indeterminate ? 0.50 : double(progress - minimum) / (maximum - minimum);
int fillWidth = int(rect.width() * fillRatio); const auto fillWidth = static_cast<int>(rect.width() * fillRatio);
int chunkWidth = fillWidth; int chunkWidth = fillWidth;
if (subRule.hasContentsSize()) { if (subRule.hasContentsSize()) {
QSize sz = subRule.size(); QSize sz = subRule.size();