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(se开发者_运维知识库lf.__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
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.
精彩评论