Search code examples
pythoncrashpyqt4qtextedit

PyQt4 TextEdit.clear causes crash


I have a deque type list (a queue) which I'd like to show and update in QTextEdit. There is a function uuenda_kama in class MyForm which should do this (and some other s*** too). First pass of this function when textEdit is empty, it works like a charm, all necessary fields are updated. But on second pass as there has some text added to it, it crashes throwing me a Visual Studio debugger in face. Tried commenting different parts out and came out that line "self.ui.textEdit.clear()" is causing this. What is wrong with it and why is it working on first pass? What can I do to fix it? Code I have right now:

class MyForm(QtGui.QMainWindow):
    ...
    def uuenda_kama(self):
        while True:
        ...
        if vana_que != list(que):
            self.ui.textEdit.clear()
            for i in que:
                self.ui.textEdit.append(i)
            vana_que = list(que)
        sleep(1)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    uuendamine = Thread(target=myapp.uuenda_kama)
    uuendamine.start()
    myapp.show()
    sys.exit(app.exec_())

Solution

  • You should not be creating a standard python thread outside of the entire app that runs methods on your qwidgets. Instead you should have a QThread that runs non-gui related logic and then emits a signal when it wants the main thread to affect the GUI

    Refer to this other question for a good example: Howto change progress by worker thread

    You should never call gui methods directly outside of the main thread.