简体   繁体   中英

PyQt4: QProcess readyRead does not always emit correctly

When I use pyqt to run a program I cannot get the output correctly every time. 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. 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. How am I supposed to get the output in this case?

pyinstaller might be printing to stderr instead of stdout. You can cause QProcess.readAll() to return both outputs by calling (before self.process.start(program) )

setProcessChannelMode(QProcess.MergedChannels)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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