PDA

View Full Version : Using QProgressBar



gutiory
29th April 2010, 13:06
Hello everyone.

I'm developing an app that requires the control of the main thread (GUI thread) for a while (few seconds). Despite this time is not so longer, I would like to show an QProgressBar in the middle of the screen to show to the user that an operations is being executed and he must wait.

I tried to do it with a QThread, but I was not possible because You can't create gui objects ( a Qwidget with the QProgressBar inside) outside the Gui Thread.

Thanks a lot in advance.

aamer4yu
29th April 2010, 13:31
Where does you app get busy for the few seconds ??
You can call qApp->processEvents() from that loop...

OR

you can make a custom widget to show the progress.. show the widget before starting your operation and close it after the operation. You need not be accurate for the operation to complete. You simply need to show to the user that the application is busy

faldzip
29th April 2010, 13:33
You have to do it in the opposite way - perform your long operation in QThread (take only this part which not requires any GUI) and show your progress bar in main thread.

gutiory
30th April 2010, 07:08
Thanks everyone for your quick answers.


Where does you app get busy for the few seconds ??
You can call qApp->processEvents() from that loop...

OR

you can make a custom widget to show the progress.. show the widget before starting your operation and close it after the operation. You need not be accurate for the operation to complete. You simply need to show to the user that the application is busy

Now I'm going to try to create the custom widget. I'll answer again later with the results.




You have to do it in the opposite way - perform your long operation in QThread (take only this part which not requires any GUI) and show your progress bar in main thread.

I can't do what you've said, because the long operation comes from a GUI operation. I've a QTableView and I asign it a model that contains one hundred columns and fifteen rows. The asignment of the model to the view makes the app slow.

Thanks again.
Regards.

tbscope
30th April 2010, 07:26
I can't do what you've said, because the long operation comes from a GUI operation. I've a QTableView and I asign it a model that contains one hundred columns and fifteen rows. The asignment of the model to the view makes the app slow.

GUI operations can never take long! If they do, you're making a mistake, and the most common mistake with big lists is that all the items get drawn, even if they are not on screen. Thus: only draw those items that are on the screen. If you need to process the data, and it is a lot of data, then make sure you seperate the user interface and the data. Process the data in a seperate thread.

Even if your list is small, seperate the processing from the viewing. If you need to read data from a file or a socket, make sure it is at least done asynchronous or in a seperate thread. You can even add a signal or function to that thread to report progress so you can add a progressdialog or progressbar and link it to that signal or function.

Populate your model via a thread. You do not need to process your data inside the model, the model is just a container for your data, nothing more.

faldzip
30th April 2010, 08:37
I can't do what you've said, because the long operation comes from a GUI operation. I've a QTableView and I asign it a model that contains one hundred columns and fifteen rows. The asignment of the model to the view makes the app slow.


okay, so it means your model is containing a lot of data, so I would suggest you making your own model which would do some lazy/delayed data loading:
1. You set your model to a view when it is still empty, so as fast as light :]
2. Now fire some method in model - let's call it load() - to load the data, lets say row by row, the loading is done in separate thread.
3. Your view can be updated on each row added (rowsInserted() and something like this).
4. The progress bar can be displayed until loading is finished.

This probably will take longer then just setting model like you have done it now, but will not freeze your GUI.

gutiory
5th May 2010, 06:59
Problem solved guys.
Thanks a lot for your answer. You were completly right. The solution is creating a method that partially load data. The behavior of the app is the same I thought, but every content appears inmediatly.
I've learnt so many things because of this problem.

Thanks a lot again and sorry for answering so late.

Regards.