From 4b4c5b06a0bb08b77e391a6efa4e3671d4eefd90 Mon Sep 17 00:00:00 2001 From: Wladimir Leuschner Date: Mon, 15 Jul 2024 10:47:09 +0200 Subject: [PATCH] QWindows11Style: Use QProgressStyleAnimation for QProgressBar Currently for an indeterminate QProgressBar in QWindows11Style, a busy drawing is triggered, consuming more CPU resources than necessary. This patch avoids the busy drawing by using QProgressStyleAnimation, which only triggers a redraw for specified Fps. Fixes: QTBUG-127112 Pick-to: 6.7 Change-Id: I46da8ffcb849563c771dbc72af4c7e8b306b4802 Reviewed-by: Oliver Wolff (cherry picked from commit 384acdc1cf2cc6472c9eb9d2d79077736ded0e92) Reviewed-by: Qt Cherry-pick Bot --- .../styles/modernwindows/qwindows11style.cpp | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/plugins/styles/modernwindows/qwindows11style.cpp b/src/plugins/styles/modernwindows/qwindows11style.cpp index 2f23a1ca0df..f18f8295323 100644 --- a/src/plugins/styles/modernwindows/qwindows11style.cpp +++ b/src/plugins/styles/modernwindows/qwindows11style.cpp @@ -1283,6 +1283,13 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op const qreal offset = (orientation == Qt::Horizontal && int(rect.height()) % 2 == 0) || (orientation == Qt::Vertical && int(rect.width()) % 2 == 0) ? 0.5 : 0.0; + if (isIndeterminate) { + if (!d->animation(option->styleObject)) + d->startAnimation(new QProgressStyleAnimation(d->animationFps, option->styleObject)); + } else { + d->stopAnimation(option->styleObject); + } + if (!isIndeterminate) { fillPercentage = ((float(progbaropt->progress) - float(progbaropt->minimum)) / (float(progbaropt->maximum) - float(progbaropt->minimum))); if (orientation == Qt::Horizontal) { @@ -1297,22 +1304,23 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op rect.setHeight(oldHeight * fillPercentage); } } else { - auto elapsedTime = std::chrono::time_point_cast(std::chrono::system_clock::now()); - fillPercentage = (elapsedTime.time_since_epoch().count() % 5000)/(5000.0f*0.75); - if (orientation == Qt::Horizontal) { - float barBegin = qMin(qMax(fillPercentage-0.25,0.0) * rect.width(), float(rect.width())); - float barEnd = qMin(fillPercentage * rect.width(), float(rect.width())); - rect = QRect(QPoint(rect.left() + barBegin, rect.top()), QPoint(rect.left() + barEnd, rect.bottom())); - rect.setHeight(progressBarThickness); - rect.moveTop(center.y() - progressBarHalfThickness - offset); - } else { - float barBegin = qMin(qMax(fillPercentage-0.25,0.0) * rect.height(), float(rect.height())); - float barEnd = qMin(fillPercentage * rect.height(), float(rect.height())); - rect = QRect(QPoint(rect.left(), rect.bottom() - barEnd), QPoint(rect.right(), rect.bottom() - barBegin)); - rect.setWidth(progressBarThickness); - rect.moveLeft(center.x() - progressBarHalfThickness - offset); + if (qobject_cast(d->animation(option->styleObject))) { + auto elapsedTime = std::chrono::time_point_cast(std::chrono::system_clock::now()); + fillPercentage = (elapsedTime.time_since_epoch().count() % 5000)/(5000.0f*0.75); + if (orientation == Qt::Horizontal) { + float barBegin = qMin(qMax(fillPercentage-0.25,0.0) * rect.width(), float(rect.width())); + float barEnd = qMin(fillPercentage * rect.width(), float(rect.width())); + rect = QRect(QPoint(rect.left() + barBegin, rect.top()), QPoint(rect.left() + barEnd, rect.bottom())); + rect.setHeight(progressBarThickness); + rect.moveTop(center.y() - progressBarHalfThickness - offset); + } else { + float barBegin = qMin(qMax(fillPercentage-0.25,0.0) * rect.height(), float(rect.height())); + float barEnd = qMin(fillPercentage * rect.height(), float(rect.height())); + rect = QRect(QPoint(rect.left(), rect.bottom() - barEnd), QPoint(rect.right(), rect.bottom() - barBegin)); + rect.setWidth(progressBarThickness); + rect.moveLeft(center.x() - progressBarHalfThickness - offset); + } } - const_cast(widget)->update(); } if (progbaropt->invertedAppearance && orientation == Qt::Horizontal) rect.moveLeft(originalRect.width() * (1.0 - fillPercentage));