Pyside QProcess 需要帮助 [英] Pyside QProcess Need Help
问题描述
注意:class MyWindow(QWidget):
NOTE: class MyWindow(QWidget):
在init
self.proc = QtCore.QProcess(self)
self.te = QTextEdit(self)
self.btn = QPushButton("Execute", self)
self.btn.clicked.connect(self.__event_btn)
现在我有了这个:
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.
现在我的问题是,有什么方法可以不等待进程完成而捕获 gcc 的输出并在 TextEdit 中显示它们?(cmd.exe 或终端的方式.它们在程序运行时显示输出)
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)
谢谢
--标记
推荐答案
您需要确保程序(在本例中为 gcc
)以无缓冲的标准输出运行.大多数控制台应用程序都会缓冲,除非写入控制台(cmd.exe
或终端),因为这样可以提高性能.大概 Qt 用来缓冲 QProcess
' 输出的内部流不被视为 tty,这就是为什么你得到缓冲并且只在最后看到输出.
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.
通常可以使 C 程序关闭缓冲 (setvbuf
),但大多数不会这样做.由于您需要使用 gcc
来工作,它可能为非 ttys 缓冲,因此您必须使用像 unbuffer
这样的实用程序.请参阅此答案.
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.
这篇关于Pyside QProcess 需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!