Search code examples
pythonpyqtpyqt4qtextedit

How to dynamically update QTextEdit


so I have a QTextEdit within a Main Window in my GUI. I want to live update the text in this by pulling from a remotely updating list. I don't know how to infinitely check this list, without either a) doing an infinite loop or b) thread.

a) Crashes the GUI, as it is an infinite loop b) produces an error saying:

QObject: Cannot create children for a parent that is in a different thread.

Which I understand.

What could I do to fix this?


Solution

  • this is how it works without threads :)

    1) Create pyqt textEditor logView:

    self.logView = QtGui.QTextEdit()
    

    2)add pyqt texteditor to layout:

    layout = QtGui.QGridLayout()
    layout.addWidget(self.logView,-ROW NUMBER-,-COLUMN NUMBER-)
    self.setLayout(layout)
    

    3) the magic function is:

    def refresh_text_box(self,MYSTRING): 
        self.logView.append('started appending %s' % MYSTRING) #append string
        QtGui.QApplication.processEvents() #update gui for pyqt
    

    call above function in your loop or pass concatenated resultant string directly to above function like this:

    self.setLayout(layout)
    self.setGeometry(400, 100, 100, 400)
    QtGui.QApplication.processEvents()#update gui so that pyqt app loop completes and displays frame to user
    while(True):
        refresh_text_box(MYSTRING)#MY_FUNCTION_CALL
        MY_LOGIC
    #then your gui loop
    if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog = MAIN_FUNCTION()
    sys.exit(dialog.exec_())