From 3d9a48ad37f4c7f33d6e0898e8f66fe07f27c403 Mon Sep 17 00:00:00 2001 From: Dheerendra Purohit Date: Fri, 27 Sep 2024 12:32:32 +0530 Subject: [PATCH] QProgressbar without text does not progress smoothly The jerk with QProgressBar occurs when setTextVisible(false) is used. Condition exists to handle repaint when text visibility is true but not handled when text visibility is false. Usage of auto leads to type confusions. Add the logic to repaint for every 1 pixel change in the value when text visibility is false. Avoid using auto when type is obvious. Fixes: QTBUG-45048 Change-Id: I0fdc62c7aabdaef52bc4bdc20e82141a4a7d29d5 Reviewed-by: Axel Spoerl (cherry picked from commit 913b98d4120b56487a14eb96b5e5252fdd939603) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qprogressbar.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/widgets/widgets/qprogressbar.cpp b/src/widgets/widgets/qprogressbar.cpp index 015523c4e57..c685ef2b634 100644 --- a/src/widgets/widgets/qprogressbar.cpp +++ b/src/widgets/widgets/qprogressbar.cpp @@ -111,29 +111,33 @@ bool QProgressBarPrivate::repaintRequired() const if (value == lastPaintedValue) return false; - const auto valueDifference = qAbs(qint64(value) - lastPaintedValue); - // Check if the text needs to be repainted + const int valueDifference = qAbs(value - lastPaintedValue); + if (value == minimum || value == maximum) return true; - const auto totalSteps = qint64(maximum) - minimum; + const int totalSteps = maximum - minimum; + + const int currentPercentage = (value - minimum) * 100 / totalSteps; + const int lastPaintedPercentage = (lastPaintedValue - minimum) * 100 / totalSteps; + + const int percentageChangeConstant = 1; + const bool percentageChanged = (qAbs(currentPercentage - lastPaintedPercentage) >= percentageChangeConstant); + if (textVisible) { if (format.contains("%v"_L1)) return true; - if (format.contains("%p"_L1) && valueDifference >= qAbs(totalSteps / 100)) + if (format.contains("%p"_L1) && percentageChanged) return true; } - // Check if the bar needs to be repainted + // Check if the bar needs to be repainted based on pixel-level differences QStyleOptionProgressBar opt; q->initStyleOption(&opt); - int cw = q->style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &opt, q); QRect groove = q->style()->subElementRect(QStyle::SE_ProgressBarGroove, &opt, q); - // This expression is basically - // (valueDifference / (maximum - minimum) > cw / groove.width()) - // transformed to avoid integer division. int grooveBlock = (q->orientation() == Qt::Horizontal) ? groove.width() : groove.height(); - return valueDifference * grooveBlock > cw * totalSteps; + const double pixelSize = static_cast(totalSteps) / grooveBlock; + return valueDifference > pixelSize; } /*!