Pyside QProcess 需要帮助

发布于 2024-11-13 08:00:55 字数 901 浏览 3 评论 0原文

注意:类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)

上面的代码在进程执行完成之前不会显示任何内容。

现在我的问题是,有什么方法可以捕获 gcc 的输出并在 TextEdit 中显示它们,而无需等待该过程完成? (cmd.exe 或 teminal 的方式。它们在程序运行时显示输出)

谢谢

-- 标记

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-11-20 08:00:55

您需要确保程序(在本例中为 gcc)以无缓冲的 stdout 运行。大多数控制台应用程序都会缓冲,除非写入控制台(cmd.exe 或终端),因为这会提高性能。据推测,Qt 用于缓冲 QProcess 输出的内部流不会被视为 tty,这就是为什么您得到缓冲并且只能在最后看到输出的原因。

通常,C 程序可以关闭缓冲 (setvbuf),但大多数程序不会这样做。由于您需要与 gcc 一起工作,而 gcc 可能会为非 ttys 提供缓冲,因此您必须使用像 unbuffer 这样的实用程序。请参阅此答案

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文