Pyqt: 'dynamically' append to qtextedit from function

那年仲夏 提交于 2019-12-03 21:05:47

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/12602179/pyqt-dynamically-append-to-qtextedit-from-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!