Progressbar problem
-
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 ! -
Please use "code wrapping":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 for your post.
-
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. -
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 ? -
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.
-
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 ??
-
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.
-
@
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.
-
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.
-
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);
@