PDA

View Full Version : Adding a Progress bar



srohit24
27th May 2009, 08:13
Hi

I have built a app that needs about 25 seconds to complete the execution(it needs to read from a device)

When its executing, my app window just goes white.

I want to add a progress bar to indicate how many seconds are left before it completes executing.

I have added the progess bar in the ui design, but i have no idea as tohow to call it in my function and display % left befor completion.

i have used this only.

ui->progressBar->reset();
ui->progressBar->setRange(0,100);
ui->progressBar->setValue(50);

I have added the last command, in the function that needs 25 seconds, but as the app freezes, the increment is not shown.


I have seen the documentation, but it doesnt actually help me as to how my progress bar should look like. i am new to Qt, so i am hoping someone can help.

I need a progress bar, that keeps increasing from 0% to 5% to 10% and so on till 100 is reached and in 25 seconds. A constant moving bar.

When the control goes inside the function, all the ui commands in there arent shown in realtime. i.e. the execution is not shown as the app has only a white screen.

can anybody help?

thanks

pospiech
27th May 2009, 09:25
call update() on the widget (in your case the progressbar) to make sure your changes are updated directly.

aamer4yu
27th May 2009, 09:38
Either use threads for the heavy computing..
or call qApp->processEvents() from your heavy computation loop...

wagmare
27th May 2009, 10:23
Hi

I need a progress bar, that keeps increasing from 0% to 5% to 10% and so on till 100 is reached and in 25 seconds. A constant moving bar.

i cant make QProgreeBar update in a regular fashion but htis code will be useful for u ..
use timer ..


timer = new QTimer();
timer->start(5000);
connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressBar()));

in update updateProgressBar slot


::updateProgressBar(){
int curVal = progressbar->value();
if(curVal == 100){
timer->stop();
return ;
}else
progressbar->setValue(curVal + 25);
}



When the control goes inside the function, all the ui commands in there arent shown in realtime. i.e. the execution is not shown as the app has only a white screen.

can anybody help?

thanks
use separate event loop for that function .. it is blocking the user interface ..
use QThread ..
use this link
http://doc.trolltech.com/qq/qq27-responsive-guis.html

srohit24
27th May 2009, 17:41
thanks for replying guys.

I went through your link, wagmare.

I think that Waiting in a Local Event Loop or Parallel Programming method will work fine for me.

but i am not sure as to how to use it.

My programs flow is something like this

I press a button, this in turn calls the function that reads the data from the device.

the data is read from the device from 2 separate locations. 1st time its very quick.

the 2nd once takes 25 seconds.

after the reading is over, it performs some calculations and displays the result within 2 seconds.

i dont know where to and how to modify this code



QNetworkAccessManager manager;
QEventLoop q;
QTimer tT;

tT.setSingleShot(true);
connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
connect(&manager, SIGNAL(finished(QNetworkReply*)),
&q, SLOT(quit()));
QNetworkReply *reply = manager.get(QNetworkRequest(
QUrl("http://www.qtcentre.org")));

tT.start(5000); // 5s timeout
q.exec();

if(tT.isActive()){
// download complete
tT.stop();
} else {
// timeout
}


or


QList<QImage> images = loadImages(directory);
ThumbThread *thread = new ThumbThread;
connect(thread, SIGNAL(finished(QList<QImage>)),
this, SLOT(showThumbnails(QList<QImage>)));
thread->start(images);


it would be great if you can help.

wagmare
28th May 2009, 06:15
QThread is the much better one to use ..... because u need to wait for 25 sec ... so ur event loop will resist or block the user interface for 25 seconds freezing the GUI ..to update()
best go for QThread ... a separate event loop ...

this example does what u need exactly ....
http://doc.trolltech.com/4.5/network-blockingfortuneclient.html

have a good day ....

srohit24
28th May 2009, 15:14
thanks for the promt reply mate. :)

but the whole program is way over my head.

I have a huge application now, about 600 lines of code, to redo everything is a bit tough.

Is there a simple function, using which i can get back the control for the GUI while the reading takes place in the background??

can i use the sleep command in the the reading function to get a small amount of work done?

thanks.

wagmare
28th May 2009, 16:16
thanks for the promt reply mate. :)
Is there a simple function, using which i can get back the control for the GUI while the reading takes place in the background??

can i use the sleep command in the the reading function to get a small amount of work done?



no sleep will do no good ... it still freeze the gui main event loop

u can use qApp->processEvents()

void QCoreApplication::processEvents ( QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents ) [static]

aamer4yu
29th May 2009, 05:36
Only if u had read Post #3 a little carefully :p ;)

srohit24
29th May 2009, 15:04
no sleep will do no good ... it still freeze the gui main event loop

u can use qApp->processEvents()

void QCoreApplication::processEvents ( QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents ) [static]

should i place this code in the main.cpp file? or in the class file that i am using to get the work done?

can you be more specific as to what i must do to get the freezing to go away?

Should i place the 25 seconds execution function in the above mentioned function?

srohit24
1st June 2009, 04:23
can anyone guide me as to where i should place the processevents() function so that the GUI doesnt freeze?

aamer4yu
1st June 2009, 05:39
Something like ..

do
{
...
// Do you stuff
qApp->processEvents(); // Call process events here..
} while ( ! 25 seconds);

srohit24
1st June 2009, 06:34
ok. thanks

i have build my app both in release and debug mode.

when i double click on the exe in release folder, i get this error

du.exe - Entry point not founf
The procedure entry point ?? 0QCoreapplicaiton @@QAE@ABV0@@Z could not be located in the dynamic link library QtCore4.dll

I have added #include <QCoreApplication> as a header in both main and the class file i have used.

the exe in the debug folder works fine.

how can i solve this?

nish
1st June 2009, 06:50
you may be having more than one version of Qt dlls in your system...

check your PATH variable.
also check where is QtCore4d.dll(or QtCored4.dll i dont remember whats the name) ...

make changes to your PATH and delete the old path to QtCore4.dll... and point it to the one where the debug dlls are located.. because there you will find the exact release dlls also

or u can simply check your compiler/linker flags to see the paths

pospiech
1st June 2009, 07:24
As soon as you have one Qt application on your system which requires to be in the path, the dlls of your Qt Installation are likely not to be used. Therefor you must put all necessary dlls into your binary folder.

srohit24
1st June 2009, 09:52
thanks it solved the problem.