Pyqt: "dynamically" add to qtextedit from function

In my pyqt gui there is a button that, when clicked, launches a function that performs several lengthy mathematical calculations. Inside this function there were many print statements, such as:

print "finished calculating task1 going on to task2" 

Thus, using print instructions similar to what I did not need to indicate, for example, a progress indicator to indicate program progress. I added a QTextEdit widget to my gui and replaced all print statements in this function:

MyTextEdit.append('message')

where MyTextEdit is a QTextEdit widget and message is the message I would like 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 press the button and the function starts, all calculations inside this function should end, and then all messages are added to the QTextEdit widget.

I thought that every time MyTextEdit.append('message') is executed, it starts immediately and the widget will display the message at the very moment, and not at the end for all other messages.

What am I doing wrong?

I had an idea to do this by reading this post.

+4
source share
1 answer

Just call QCoreApplication.processEvents after each append

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

This will ask Qt to update your gui before completing tasks, as the team processes all pending events.

+3
source

Source: https://habr.com/ru/post/1436368/


All Articles