gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:28:28 29 4
gpt4 key购买 nike

我想从 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().

关于qt - 来自 QProcess 的标准输出离实时很远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10223777/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com