这是我的unanswered question here的后续。如下所示,该代码完全与segfault崩溃(将其复制/粘贴到您的系统中并运行它)。当我从一个或两个信号构造函数调用中删除type=QtCore.Qt.DirectConnection
(从而改为使用QtCore.Qt.AutoConnection
)时,所有内容均按应有的方式运行:出现一个小部件,显示五个进度条填满,然后以无穷循环的方式清空。
from PySide import QtCore, QtGui
import time
class Worker(QtCore.QThread):
sig_worker_update_progress = QtCore.Signal(int, int)
def __init__(self, thread_id, *args, **kwargs):
super(Worker, self).__init__(*args, **kwargs)
self.thread_id = thread_id
self.stop_requested = False
def slot_interrupt(self):
self.stop_requested = True
def run(self):
progress = 0
while(True):
self.sig_worker_update_progress.emit(self.thread_id, progress % 100)
progress += 1
if self.stop_requested:
break
else:
time.sleep(0.1)
class Controller(QtCore.QObject):
sig_controller_update_progress = QtCore.Signal(int, int)
def __init__(self, num_workers, *args, **kwargs):
super(Controller, self).__init__(*args, **kwargs)
self.workers = []
for i in range(num_workers):
self.workers.append(Worker(i))
self.workers[i].sig_worker_update_progress.connect(
self.slot_worker_update_progress,
type=QtCore.Qt.DirectConnection)
for worker in self.workers:
worker.start()
def slot_worker_update_progress(self, thread_id, progress):
# Do
# Stuff
self.sig_controller_update_progress.emit(thread_id, progress)
class Monitor(QtGui.QWidget):
def __init__(self, num_workers, *args, **kwargs):
super(Monitor, self).__init__(*args, **kwargs)
main_layout = QtGui.QVBoxLayout()
self.setLayout(main_layout)
self.progress_bars = []
for _ in range(num_workers):
progress_bar = QtGui.QProgressBar()
main_layout.addWidget(progress_bar)
self.progress_bars.append(progress_bar)
self.controller = Controller(num_workers)
self.controller.sig_controller_update_progress.connect(
self.slot_controller_update_progress,
type=QtCore.Qt.DirectConnection)
def slot_controller_update_progress(self, thread_id, progress):
self.progress_bars[thread_id].setValue(progress)
if __name__ == "__main__":
app = QtGui.QApplication([])
monitor = Monitor(5)
monitor.show()
app.exec_()
为什么使用两个嵌套的
DirectConnection
信号会导致段错误?如果Qt不希望您这样做,为什么没有给出更多有用的错误?我正在使用包装Qt 4.8框架的PySide v1.2.2。
最佳答案
我找到了令人满意的解释here。显然,发出DirectConnection
类型的信号等效于直接函数调用。因此,当两个信号都经过Worker
编码时,GUI毕竟是在DirectConnect
线程上更新的。如我的other question所述,不允许线程更改GUI。问题不在于本身嵌套DirectConnection
。
关于c++ - 嵌套的DirectConnection信号导致段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27690865/