Skip to content

Commit d16b5db

Browse files
committed
Fix parent task progress report calculation when subtask progress changes
(cherry-picked from 95438cc)
1 parent 2b6ef28 commit d16b5db

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/core/qgstaskmanager.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ QgsTask::QgsTask( const QString &name, Flags flags )
3333

3434
QgsTask::~QgsTask()
3535
{
36-
Q_ASSERT_X( mStatus != Running, "delete", QString( "status was %1" ).arg( mStatus ).toLatin1() );
36+
Q_ASSERT_X( mStatus != Running, "delete", QStringLiteral( "status was %1" ).arg( mStatus ).toLatin1() );
3737

3838
Q_FOREACH ( const SubTask &subTask, mSubTasks )
3939
{
@@ -213,10 +213,12 @@ void QgsTask::setProgress( double progress )
213213
}
214214

215215
// avoid flooding with too many events
216-
if ( static_cast< int >( mTotalProgress * 10 ) != static_cast< int >( progress * 10 ) )
217-
emit progressChanged( progress );
218-
216+
double prevProgress = mTotalProgress;
219217
mTotalProgress = progress;
218+
219+
// avoid spamming with too many progressChanged reports
220+
if ( static_cast< int >( prevProgress * 10 ) != static_cast< int >( mTotalProgress * 10 ) )
221+
emit progressChanged( progress );
220222
}
221223

222224
void QgsTask::completed()

tests/src/core/testqgstaskmanager.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ void TestQgsTaskManager::addTask()
375375

376376
task->cancel();
377377
task2->cancel();
378+
379+
while ( manager.countActiveTasks() > 0 )
380+
{
381+
QCoreApplication::processEvents();
382+
}
378383
}
379384

380385
void TestQgsTaskManager::taskTerminationBeforeDelete()
@@ -396,6 +401,7 @@ void TestQgsTaskManager::taskTerminationBeforeDelete()
396401
// if task is not terminated assert will trip
397402
delete manager;
398403
QApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
404+
flushEvents();
399405
}
400406

401407
void TestQgsTaskManager::taskFinished()
@@ -404,9 +410,8 @@ void TestQgsTaskManager::taskFinished()
404410
// from main thread
405411
QgsTaskManager manager;
406412

407-
bool *resultObtained = new bool;
408-
*resultObtained = false;
409-
FinishTask *task = new FinishTask( resultObtained );
413+
bool resultObtained = false;
414+
FinishTask *task = new FinishTask( &resultObtained );
410415
task->desiredResult = true;
411416
manager.addTask( task );
412417
while ( task->status() == QgsTask::Running
@@ -419,9 +424,9 @@ void TestQgsTaskManager::taskFinished()
419424
QCoreApplication::processEvents();
420425
}
421426
flushEvents();
422-
QCOMPARE( *resultObtained, true );
427+
QCOMPARE( resultObtained, true );
423428

424-
task = new FinishTask( resultObtained );
429+
task = new FinishTask( &resultObtained );
425430
task->desiredResult = false;
426431
manager.addTask( task );
427432

@@ -432,7 +437,7 @@ void TestQgsTaskManager::taskFinished()
432437
QCoreApplication::processEvents();
433438
}
434439
flushEvents();
435-
QCOMPARE( *resultObtained, false );
440+
QCOMPARE( resultObtained, false );
436441
}
437442

438443
void TestQgsTaskManager::subTask()
@@ -484,13 +489,15 @@ void TestQgsTaskManager::subTask()
484489
// test progress calculation
485490
QSignalSpy spy( parent, &QgsTask::progressChanged );
486491
parent->emitProgressChanged( 50 );
492+
flushEvents();
487493
QCOMPARE( std::round( parent->progress() ), 17.0 );
488-
//QCOMPARE( spy.count(), 1 );
494+
QCOMPARE( spy.count(), 1 );
489495
QCOMPARE( std::round( spy.last().at( 0 ).toDouble() ), 17.0 );
490496

491497
subTask->emitProgressChanged( 100 );
498+
flushEvents();
492499
QCOMPARE( std::round( parent->progress() ), 50.0 );
493-
//QCOMPARE( spy.count(), 2 );
500+
QCOMPARE( spy.count(), 2 );
494501
QCOMPARE( std::round( spy.last().at( 0 ).toDouble() ), 50.0 );
495502

496503
subTask2->finish();
@@ -500,12 +507,12 @@ void TestQgsTaskManager::subTask()
500507
}
501508
flushEvents();
502509
QCOMPARE( std::round( parent->progress() ), 83.0 );
503-
//QCOMPARE( spy.count(), 3 );
510+
QCOMPARE( spy.count(), 3 );
504511
QCOMPARE( std::round( spy.last().at( 0 ).toDouble() ), 83.0 );
505512

506513
parent->emitProgressChanged( 100 );
507514
QCOMPARE( std::round( parent->progress() ), 100.0 );
508-
//QCOMPARE( spy.count(), 4 );
515+
QCOMPARE( spy.count(), 4 );
509516
QCOMPARE( std::round( spy.last().at( 0 ).toDouble() ), 100.0 );
510517
parent->terminate();
511518
subTask->terminate();

0 commit comments

Comments
 (0)