PDA

View Full Version : Maximum value of ProgressBar



jesse_mark
8th January 2013, 23:16
Hello,

I want to ask about whether i can or not to set the maximum value of progress bar to value bigger than the max int value.

for example,

I have this case and I want to use prgressbar for it.

I have 2 differetnt loops.

the first loop is O(n)

the 2nd loop is O(n*m)
(m !=n)


for(i .... n )
{
...
...
..

}

for (i m)
{

for(j n)
{
......
......
}

}


These loops takes long time to finish, so i want to set progress bar

I tried to set the maximum value to

n + (n*m)

and and the increse its value by 1 for each iteration, this way works fine when the n and m are not too big, but when n and m are really big such as ( 2 M) this way wont works
as the

QProgressBar.setMaximum(int)
take "int" and int type can not hold big values.

so plz, dose any one have an idea of how I can solve this issue.

thanks

wysota
9th January 2013, 02:10
If you have such big loops then setting value for the progress bar inside the most inner loop is not a good idea. Each time you set a new value and want it to be reflected in your UI, you need to force Qt to process its events. If you do that inside the most-inner loop, events are going to be processed a lot. Each time they are processed, the application is not doing the tasked you wanted it to but instead spins the event loop. This makes your task execute much longer (for some tasks it might take longer than tasks themselves). Consider setting new value for the progress bar in the most outer loop. This will reduce processing time significantly. You can't force the default QProgressBar to accept anything else than an int, you'd have to roll out your own progress bar implementation (it's not that complicated, you could subclass QProgressBar and provide your own counters) but I strongly suggest to avoid it.

jesse_mark
9th January 2013, 15:59
the issue is, that I have two separate loops, the first one use "n" and the other loop "nested loops" the outer loop use "m".

so if i just used (n+m) as my maximum value for progress Bar, i end up with not really reflective progress bar,
In case of N away bigger than M, the progress bar will almost reach 90% from the first loop, and come the 2nd loop and feel its stuck as the inner loop will be too huge compared to the outer loop.

wysota
9th January 2013, 17:03
You can always go for a hybrid approach. Calculate m*n and divide it by some constant (e.g. 1000 or 100000). Then increment the progress bar value by 1 for each 1000 or 100000 iterations of the inner loop.

anda_skoa
10th January 2013, 15:27
Or store progress and total in double and use the ratio as a percentage of a fixed, large integer value.

Cheers,
_