Pyside QProcess Need Help

青春壹個敷衍的年華 提交于 2020-03-24 03:56:32

问题


NOTE: class MyWindow(QWidget):

In init

self.proc = QtCore.QProcess(self)
self.te   = QTextEdit(self)
self.btn  = QPushButton("Execute", self)
self.btn.clicked.connect(self.__event_btn)

Now I have this:

def __event_btn(self):
    w_dir = "" # This set to my working directory where my C files are
    args  = ["-o", "MyFile", "MyFile.c"]
    cmd   = "gcc"

    self.proc.setWorkingDirectory(dir)
    self.proc.readyReadStandardOutput.connect(self.__read)
    self.proc.closeWriteChannel()
    self.proc.setReadChannel(QtCore.QProcess.StanfardOutput())
    self.proc.start(cmd, args)

def __read(self):
    self.te.setText(self.proc.readAllStandardOutput)

The code above won't show anything until the process done executing.

Now my question is, is there any way that I can capture the output from gcc and show them in TextEdit by not waiting for the process to be finished? (The way that cmd.exe or teminal does. They show the output as the program runs)

Thanks

-- Mark


回答1:


You need to ensure that the program (gcc in this case) runs with stdout unbuffered. Most console applications buffer unless writing to a console (cmd.exe or terminal) since that improves performance. Presumably the internal streams used by Qt to buffer the QProcess' output are not seen as ttys, which is why you get buffering and only see output at the end.

Normally C programs can be made to turn buffering off (setvbuf), but most don't do this. Since you need things to work with gcc, which presunably buffers for non-ttys, you'll have to use a utility like unbuffer. See this answer.



来源:https://stackoverflow.com/questions/6234687/pyside-qprocess-need-help

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