QPixmapStyle: do not assume minimum == 0 when painting progress bars

The arithmetic used to calculate the size of the progress bar fill in
QPixmapStyle assumed minimum == 0, but that does not necessarily
hold, since the minimum value is user-defined.

So, fix the arithmetic to take the minimum into account, taking care,
as done elsewhere, to avoid signed integer and qreal=float overflows,
by using qint64 and double, respectively.

[ChangeLog][QtWidgets][QPixmapStyle] Now handles progress bars with
minimum != 0 correctly.

Change-Id: I738ded56e8234716c36a5e9fde15bae691c43a35
Reviewed-by: David Faure <david.faure@kdab.com>
bb10
Marc Mutz 2017-01-05 09:12:10 +01:00
parent a3d0983f59
commit 3f996edbb6
1 changed files with 7 additions and 4 deletions

View File

@ -819,11 +819,14 @@ void QPixmapStyle::drawProgressBarFill(const QStyleOption *option,
drawCachedPixmap(vertical ? PB_VComplete : PB_HComplete, option->rect, painter);
} else {
if (pbar->progress == 0)
if (pbar->progress == pbar->minimum)
return;
const int maximum = pbar->maximum;
const qreal ratio = qreal(vertical?option->rect.height():option->rect.width())/maximum;
const int progress = pbar->progress*ratio;
const auto totalSteps = qint64(pbar->maximum) - pbar->minimum;
const auto progressSteps = qint64(pbar->progress) - pbar->minimum;
const auto availablePixels = vertical ? option->rect.height() : option->rect.width();
const auto pixelsPerStep = double(availablePixels) / totalSteps;
const auto progress = static_cast<int>(progressSteps * pixelsPerStep); // width in pixels
QRect optRect = option->rect;
if (vertical) {