Pyside: Multiple QProcess output to TextEdit

試著忘記壹切 提交于 2019-12-24 10:35:39

问题


I have a pyside app that calls an exectuable. I want to run this executable asynchronously in n processes and capture the output of each process in a QTextEdit.

At the moment I have:

def run(self, args, worklist):        

    self.viewer = OutputDialog(self)

    self.procs = []
    for path in worklist:
        final_args = args + path

        p = QtCore.QProcess(self)
        p.readyReadStandardOutput.connect(self.write_process_output)
        self.procs.append(p)
        p.start(self.exe, final_args)

def write_process_output(self):
    for p in self.procs:
        self.viewer.text_edit.append(p.readAllStandardOutput())

Which is way too clunky as each time a process sends the 'ready' signal, it attempts to get the output for ALL processes.

How can I get the output just for the process that sent the signal?


回答1:


Connect the signal using a lambda so that the relevant process is passed to the slot:

        p.readyReadStandardOutput.connect(
            lambda process=p: self.write_process_output(process))


    def write_process_output(self, process):
        self.viewer.text_edit.append(process.readAllStandardOutput())


来源:https://stackoverflow.com/questions/27314571/pyside-multiple-qprocess-output-to-textedit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!