Search code examples
pythondynamicpyqt4stdoutqtextedit

Pyqt: 'dynamically' append to qtextedit from function


There is a button in my pyqt gui that when clicked runs a function that does some lengthy math calculations. Inside this function there were a lot of print statements like:

print "finished calculating task1 going on to task2"

So by using print statements like that i didn't need to have let's say a progressbar for example to indicate program progress. I added a QTextEdit widget in my gui and replaced all print statements in that function with:

MyTextEdit.append('message')

where MyTextEdit is a QTextEdit widget and message is the message i would like the function to print.

Example:

MyTextEdit.append('finished calculating task1 going on to task2')
task2 #lengthy second task
MyTextEdit.append('finished calculating task2 going on to task3')
task3 #lengthy third task

When i click the button and the function runs, all calculations inside that function have to finish and then all messages are appended to the QTextEdit widget.

I thought that each time a MyTextEdit.append('message') is executed it would run immediately and the widget would display the message at that very instant and not at the end along all other messages.

What am i doing wrong?

I had the idea of doing this by reading this post


Solution

  • Just make a call to QCoreApplication.processEvents after each append

    You can get your instance of QCoreApplication with the static method QCoreApplication.instance

    This will ask Qt to "refresh" your gui before finishing the tasks that are being executed, as the command processes all pending events.