PDA

View Full Version : QProgressDialog is slowing down loop?



marwooj
28th June 2011, 07:13
I have simple loop looking for expression in QStringList.
Whenever I add
progressDialog2.setLabelText(tr("Searching %1 of %2...").arg(i).arg(fields_size));

it slows down as hell


for (int i = 0; i < fields_size; ++i) {
progressDialog2.setValue(i);
progressDialog2.setLabelText(tr("Searching %1 of %2...").arg(i).arg(fields_size));
qApp->processEvents();

if (progressDialog2.wasCanceled()){
fields_step.append("Canceled");
fileName = fileName + "_Canceled";
break;
} else if (fields.at(i).contains(rx1)) {
fields_step.append(fields.at(i));
}
}

What I am doing wrong?

ChrisW67
28th June 2011, 07:55
You are setting the position of the progress bar (unavoidable) and triggering a translation lookup and two int to string conversions every time through the loop. Do you really need to repeat the progress figure in the label? If not, then the label should be set once outside the loop.

marwooj
28th June 2011, 12:08
If I comment out setLabelText it is just 20 times slower rather without progresbarr at all.
no progress bar = ~0.5s
progres bar no label = ~5s
bar and label = ~20s

maybe putting these in thread or something will help?

Rachol
28th June 2011, 12:26
Calling setValue on a progressBar on every iteration isn't good idea either if you have thousand iterations. Do it only if the step is bigger or equal 1% of the operation.

ChrisW67
29th June 2011, 02:18
Is there any point to a progress bar for a half-second process?

Santosh Reddy
29th June 2011, 05:18
Using a thread will not decrease the time required to execute to loop, unless you have multi-core / multi processor system, even in this case the time will not reduce if you update the QProgrssBar every iteration.

You can use the loop in another thread just to increase the GUI response, but still the loop will take its same time.

marwooj
29th June 2011, 06:46
Is there any point to a progress bar for a half-second process?

Is is 0.5 just in this example - test case.


Do it only if the step is bigger or equal 1% of the operation.
So I recount my counter to 100% and then just if update label if step is more than 1%?

stampede
29th June 2011, 06:47
What is the value of fields_size in this example ?

Rachol
29th June 2011, 08:29
So I recount my counter to 100% and then just if update label if step is more than 1%?


Yes, call setValue() and processEvents() only when the step is >= 1%

wysota
29th June 2011, 09:24
Updating a progress bar will always be slower than not updating it. Remember that processEvents() triggers all events and not just repaint of your progress dialog. Overall it is a time consuming process that would better be spent on doing the actual operation.