且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PySide和QProgressBar在不同的线程中更新

更新时间:2022-05-29 23:24:14

问题在于如何将信号连接到插槽:

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)

在这里,您明确地强制执行DirectConnection.这是错的!它使该插槽在调用者的线程中得到处理.这意味着progressBar的GUI更新发生在进程线程中,而不是在GUI线程中.但是,仅在GUI线程中允许绘图.

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.

将信号从一个线程连接到另一个线程的插槽是完全可以的.但是,您需要AutoConnection(默认值,它将识别线程并使用QueuedConnection)或QueuedConnection.

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.

此行应解决您的问题:

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

请参见 http://doc.qt.io/archives /qt-4.7/qt.html#ConnectionType-枚举.