【问题标题】:Signal from QThread doesn't get emitted来自 QThread 的信号不会发出
【发布时间】:2015-08-30 17:36:58
【问题描述】:

我尝试按照this question 中列出的解决方案进行操作,但是一旦应该发出信号,似乎什么都没有发生。

这不是全部代码,但应该足以证明我正在尝试做的事情:

class StartQT5(QMainWindow):
    def __init__(self):
        super().__init__()
        [...]
        self.getFileProperties(path, batch)

    def getFileProperties(self, path, batch):
        self.thread = QThread()
        self.w = probeThread(self)        
        self.w.moveToThread(self.thread)
        self.w.finished[dict].connect(self.setProbeOutput)
        self.thread.started.connect(self.w.run)
        self.thread.start()

    @pyqtSlot(dict)    
    def setProbeOutput(self, data):
        print("gotOutput")
        self.probeOutput = data
        print(data)

class probeThread(QObject):
    finished = pyqtSignal(dict)

    def __init__(self, parent):
        super().__init__()
        self.parent = parent
        self.output = {}

    def run(self):
        self.output = json.loads(subprocess.check_output('%s -v 0 -print_format json -show_format -show_streams -count_frames "%s"' % (self.parent.ffprobePath, self.parent.file)).decode("utf-8"))
        print("finished")
        self.finished.emit(self.output)

线程运行良好,但 setProbeOutput 永远不会被调用。对不起,如果这是微不足道的,但我似乎无法让它工作,谢谢! :)

【问题讨论】:

    标签: multithreading python-3.x pyqt5


    【解决方案1】:

    干脆干啥:

    self.w.finished.connect(self.setProbeOutput)
    

    您正在定义一个新信号并将其附加到插槽,您已经在信号和插槽声明中声明了参数类型。

    【讨论】:

      猜你喜欢
      • 2012-10-08
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2011-07-26
      • 1970-01-01
      相关资源
      最近更新 更多