PySide and QProgressBar update in a different thread

前端 未结 1 531
一生所求
一生所求 2021-01-06 08:51

This may be a little long post, so, thanks in advance to be with me till the end. Here is the problem, (i think a fairly basic one, just my inexperience with PiSide and Qt m

相关标签:
1条回答
  • 2021-01-06 09:03

    The problem lies in how you connect the signal to the slot:

          QtCore.QObject.connect(self.processThread, QtCore.SIGNAL("progress(int)"),self.progressBar, QtCore.SLOT("setValue(int)"), QtCore.Qt.DirectConnection)
    

    Here, you explicitely enforce a DirectConnection. This is wrong! It makes the slot be processed in the caller's thread. This means that the GUI update of the progressBar happens in the process thread, not in the GUI thread. However, drawing is only allowed in the GUI thread.

    It is perfectly fine to connect a signal from one thread to a slot from another. However, you need either AutoConnection (the default, which will recognize the threads and use QueuedConnection) or QueuedConnection.

    This line should fix your problem:

          QtCore.QObject.connect(self.processThread, QtCore.SIGNAL("progress(int)"),self.progressBar, QtCore.SLOT("setValue(int)"), QtCore.Qt.QueuedConnection)
    

    See http://doc.qt.io/archives/qt-4.7/qt.html#ConnectionType-enum.

    0 讨论(0)
提交回复
热议问题