readAll() 或 readAllStandardOutput() 在执行之前也返回一个空字符串

readAll() or readAllStandardOutput() is returning an empty string that too before execution

本文关键字:返回 字符串 一个 readAllStandardOutput 执行 readAll      更新时间:2023-10-16

正如标题中所定义的那样,这两个函数都返回一个空字符串。 让我描述一下我的场景,我正在执行一个 python 文件,它最后正在打印文本,文本在执行后发布在应用程序输出上,但给定的输出不会被复制。 此外,我的 Python 脚本需要 30 到 40 秒才能执行,但 readAll 函数在加载脚本后立即执行。

我的函数正在执行 Python 脚本:

QString text = ui->textEdit->toPlainText();
QString path = "D:/DS Project/Treegramming";
QString  command("py");
QStringList params = QStringList() << "nlp.py";
params << text;
QProcess *process = new QProcess();
process->startDetached(command, params, path);
process->waitForFinished(30000);
QByteArray ba = "";
process->waitForReadyRead(30000);
ba += process->readAllStandardOutput();
qDebug() << ba ;

再次重复,qDebug(( 在整个脚本执行之前给出空输出。

考虑这条线...

process->startDetached(command, params, path);

在这里,您实际上是在调用静态startDetached成员,该成员与您新创建的QProcess无关。 因此,您不会看到任何标准输出/错误。

相反,您应该将非静态start成员与类似(未经测试(的内容一起使用...

process->setWorkingDirectory(path);
process->start(command, params);