Skip to content

Commit 133c855

Browse files
fix: Improve progress tracking and UI update efficiency in ProgressPage
- Modified the progress update logic to accept sub-percent values while ensuring monotonic progress. - Introduced a throttling mechanism to limit UI updates, reducing potential performance issues from frequent progress changes. - Enhanced reset and timer restart functions to invalidate the UI update timer appropriately. These changes enhance the responsiveness and accuracy of the progress display, improving user experience during long-running tasks. bug: TASK: https://pms.uniontech.com/task-view-388835.html
1 parent 6f47091 commit 133c855

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/source/page/progresspage.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,35 @@ void ProgressPage::setProgress(double dPercent)
100100
dPercent = 0;
101101
}
102102

103-
// 原先用「整数百分比是否变大」过滤更新,会导致子百分比进度(如 1.1%、1.2%)全部被丢弃,
104-
// 从而永远不调用 calSpeedAndRemainingTime,速度与剩余时间一直停在「计算中」。
105-
// 这里改为:进度必须单调递增(浮点),进度条仍可按四舍五入刷新。
103+
// Accept sub-percent progress, but keep it monotonic.
106104
if (dPercent <= m_dProgressPercent) {
107105
qDebug() << "Progress not increased, current:" << m_dProgressPercent << "new:" << dPercent;
108106
return;
109107
}
110-
111108
m_dProgressPercent = dPercent;
112109

113110
const int iPercent = qMin(100, qRound(dPercent));
114-
if (iPercent != m_iPerent) {
111+
const bool percentChanged = (iPercent != m_iPerent);
112+
if (percentChanged) {
115113
m_iPerent = iPercent;
116114
m_pProgressBar->setValue(m_iPerent); // 进度条刷新值
117115
m_pProgressBar->update();
118116
}
119117

120-
qInfo() << "Updating progress:" << dPercent << "% (bar" << m_iPerent << "%)";
118+
// 节流:进度可能非常密集(子百分比频繁变化),每次都刷新速度/剩余时间会拖慢任务并造成显示抖动。
119+
// 规则:整数进度变化必刷;否则最多每 200ms 刷一次速度/剩余时间。
120+
if (!m_uiUpdateTimer.isValid()) {
121+
m_uiUpdateTimer.start();
122+
}
123+
const bool timeToUpdate = (m_uiUpdateTimer.elapsed() >= 200);
124+
if (!percentChanged && !timeToUpdate) {
125+
return;
126+
}
127+
if (timeToUpdate) {
128+
m_uiUpdateTimer.restart();
129+
}
130+
131+
qDebug() << "Updating progress:" << dPercent << "% (bar" << m_iPerent << "%)";
121132
// 刷新界面显示
122133
double dSpeed = 0.0;
123134
qint64 qRemainingTime = 0;
@@ -171,13 +182,19 @@ void ProgressPage::resetProgress()
171182
m_iPerent = 0;
172183
m_dProgressPercent = -1.0;
173184
m_qConsumeTime = 0;
185+
if (m_uiUpdateTimer.isValid()) {
186+
m_uiUpdateTimer.invalidate();
187+
}
174188
}
175189

176190
void ProgressPage::restartTimer()
177191
{
178192
qDebug() << "restartTimer";
179193
m_qConsumeTime = 0;
180194
m_timer.restart();
195+
if (m_uiUpdateTimer.isValid()) {
196+
m_uiUpdateTimer.invalidate();
197+
}
181198
}
182199

183200
void ProgressPage::setPushButtonCheckable(bool a, bool b)

src/source/page/progresspage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private Q_SLOTS:
149149
double m_dProgressPercent = -1.0;
150150
QElapsedTimer m_timer; //计时器
151151
qint64 m_qConsumeTime = 0; //消耗时间
152+
QElapsedTimer m_uiUpdateTimer; // UI 刷新节流计时器(避免解压高频进度拖慢)
152153

153154
QString m_strArchiveName;
154155
};

0 commit comments

Comments
 (0)