简体   繁体   English

PyQt4:QProcess readyRead并不总是正确发出

[英]PyQt4: QProcess readyRead does not always emit correctly

When I use pyqt to run a program I cannot get the output correctly every time. 当我使用pyqt运行程序时,每次都无法正确获取输出。 Here is an example: 这是一个例子:

from PyQt4 import QtCore, QtGui
import sys


class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        program = "ping"

        self.process = QtCore.QProcess()
        self.process.readyRead.connect(self.readoutput)
        self.process.start(program)

    def readoutput(self):
        print str(self.process.readAll())

def main():
    app = QtGui.QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

In this case, the output is the helper of the ping command, as I would expect. 在这种情况下,输出就是ping命令的助手,正如我所期望的那样。 Although if I change the program variable to some other value it doesn't always work, for example if i do: 虽然如果我将程序变量更改为其他值,则它并不总是有效,例如,如果我这样做:

program = "pyinstaller"

it does not print the helper of pyinstaller as it happens in the console. 它不会在控制台中显示pyinstaller的帮助程序。 How am I supposed to get the output in this case? 在这种情况下,我应该如何获得输出?

pyinstaller might be printing to stderr instead of stdout. pyinstaller可能正在打印到stderr而不是stdout。 You can cause QProcess.readAll() to return both outputs by calling (before self.process.start(program) ) 您可以通过调用(在self.process.start(program)之前)使QProcess.readAll()返回两个输出。

setProcessChannelMode(QProcess.MergedChannels) setProcessChannelMode(QProcess.MergedChannels)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM