Progressbar problem
-
wrote on 15 Apr 2012, 20:21 last edited by
i m a beginner in Qt and i m trying to show process bar for my application's gui . i have written following code
@
void my_app::progress()
{
int val=1;
while(process.state()!=0)
{
ui->progressBar->setValue(val++);
}
ui->progressBar->setValue(100);
}@
but progress bar is hanging at 98% . Can any one suggest me some solution .
thank you ! -
wrote on 15 Apr 2012, 20:25 last edited by
Please use "code wrapping":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 for your post.
-
wrote on 15 Apr 2012, 20:27 last edited by
I would say, that process I loaded before it gets to 100. It depends on your processor speed. On some computers it would go over 100, and on some not even to 90.
If you take a look at the documentation, state function return float values from 0.0 to 1.0.
You can multiply that value times 100, and set it to progress bar. -
wrote on 15 Apr 2012, 20:41 last edited by
according to documentation
http://qt-project.org/doc/qt-4.8/qprocess.html#state
QProcess::state() retruns only 3 value 0,1and 2 so till process is running QProcess::state() will return 2 and after execution it will go to QProcess::NotRunning (0) state,so it will retrun 0. Then it should come out of while loop. but that's not happening and programs hangs. why ? Any solution ? -
wrote on 15 Apr 2012, 20:46 last edited by
Sorry.
I was looking the wrong function.
Do you know approximately how long your program is running or how long does it take to complete given task?Otherwise, it'll be hard to create accurate progress bar.
-
wrote on 15 Apr 2012, 21:02 last edited by
my program is taking very less time(~1sec) to execute .But if so Then progress bar should end rapidly ? where is problem arising in the code ??
-
wrote on 16 Apr 2012, 06:42 last edited by
The problem is, because you are countig val++, while your program is running. So, if it would take something to process 5 minutes for example, val would get extremly high value
If it takes less than a second, you can cheat a little and set it maualy to 100 when exits a loop. It will be hardly noticable if even will be.
If you want accurate progress bar, you'll need some data from the program, or mesaure time in micro seconds etc.
-
wrote on 16 Apr 2012, 06:54 last edited by
@
void my_app::progress()
{
int val=1;
while (process.state()!=0)
{
ui->progressBar->setValue(val++);
}
ui->progressBar->setValue(100);
}
@From the code snippet, my understanding is that as long as the process state is 'QProcess::Starting' or 'QProcess::Running', progress bar value is incremented starting from 1. Having said that what process are you starting and how long will that run? Remember the while loop will run until process state is changed to 'QProcess::NotRunning' i.e., process is completed.
When is my_app::progress() called or how is it called?
Perhaps you can use some of the QProcess signals to change progress bar - appreciate you to look into the documents of QProcess signals.
-
wrote on 16 Apr 2012, 07:08 last edited by
The main problem for this approach is, that you are using a busy loop. That means that you don't give the application the time to do the other relevant things, like processing input, drawing, etc. That in turn means that your progress bar will not be updated.
In your case, it seems you don't know how long your process will run for, as you're just blindly increasing the progress value. Why not just use the progress bar mode for that then? Just set min, max and value to 0, and your progress bar will automatically animate a busy animation.
-
wrote on 16 Apr 2012, 17:06 last edited by
thank sir for suggestion . i can use busy Progress bar but for the record i m invoking my_app::progress() using following signal/slot mechanism :
@
connect(&my_app,SIGNAL(started()),this,SLOT(progress()));
my_app.start(program,arguments);
@
1/10