qt - 来自 QProcess 的标准输出离实时很远

我想从 QProcess 中获取标准输出到 QTextEdit 中。 如果我这样做,它会起作用:

QObject::connect(process, SIGNAL(readyReadStandardOutput()),
                 this, SLOT(readStdOutput()));

void myClass::readStdOutput()
{
  teOutput->insertPlainText(process->readAllStandardOutput());
}

虽然子程序不断打印到标准输出(在终端中测试) QTextEdit 每隔几秒更新一次输出 block 。

最佳答案

来自QIODevice手册:

Certain subclasses of QIODevice, such as QTcpSocket and QProcess, are asynchronous. This means that I/O functions such as write() or read() always return immediately, while communication with the device itself may happen when control goes back to the event loop. QIODevice provides functions that allow you to force these operations to be performed immediately, while blocking the calling thread and without entering the event loop.

要尝试的另一件事是禁用任何缓冲区,手册没有针对 QProcess 提及它,但您可以尝试一下:

Some subclasses, such as QFile and QTcpSocket, are implemented using a memory buffer for intermediate storing of data. This reduces the number of required device accessing calls, which are often very slow.

...

QIODevice allows you to bypass any buffering by passing the Unbuffered flag to open().

https://stackoverflow.com/questions/10223777/

相关文章:

ruby-on-rails - Rails 中的 ActiveRecord::MissingAttr

r - .onLoad 和交互式 - 意外行为?

c# - 在 BackgroundWorker 线程上创建 FlowDocument

ruby-on-rails - Rails Association Validations : Th

typo3 - 将 locallang 值插入 TypoScript [stdWrap]

python - beautiful soup 从谷歌搜索中提取一个 href

imagemagick - 使用 imagemagick 从图像中裁剪矩形区域

python - Pip 安装但找不到模块

ruby-on-rails - Rails 路由 : override the action nam

spring-mvc - 如何在 spring MVC 应用程序中的真实请求之外激活请求范围?