PDA

View Full Version : Updating GUI while processing



ecce
9th May 2015, 14:01
I want to update widgets while my program is working, so the user can see the progress. For example, I would like to update the text in a column in a QTreeWidget when certain steps are completed, like "Conntecting to host...", "Logging in..." etc. Using QTreeWidget.setText() here and there does not work, I only see the last text when the whole process is completed. A couple of questions on this:

- Are multiple threads required for this?
- Do you have to call QTreeWidget.setText() from the main loop?

Any tips on where to read up on this is appriciated.

anda_skoa
9th May 2015, 14:31
- Are multiple threads required for this?

Not necessarily.
If the work can be done in steps, then each step could return to the event loop and the next step would be scheduled, e.g. using a single shot timer.
Or you all QCoreApplication::processEvents()

But a thread might still be nice if the processing should really happen in parallel.



- Do you have to call QTreeWidget.setText() from the main loop?

Yes, widget methods always need to be called by the main thread.

Cheers,
_

ecce
11th May 2015, 09:39
This is a stupid question and I've avoided id for a time but... where is the main loop? :) This is how I start my application:


if __name__ == '__main__':
try:
myApp = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
myApp.exec_()

....


class MainWindow(QtGui.QMainWindow):

def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle("Test Application")
self.setGeometry(0, 0, 1000, 900)
self.CreateStatusBar()
self.show()

....


So if I want to put code in the main loop, where do I put it?

wysota
11th May 2015, 10:18
You don't put your code "in" the main loop. The loop is implemented inside the application's exec() call. It waits for events and processes them. Such event can be a timer timing out or a queued slot invocation. The point is to not create a while loop (or similar) in your code as this prevents events (such as redrawing widgets) to be executed.