3

我使用 QProcess 并将它的 readyReadStandardOutput 连接到插槽。但是在启动插槽后执行两次。请告诉我这是为什么?

{
    myProcess = new QProcess(parent);
    myProcess->start("mayabatch.exe -file "+scene);
    connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
}

void MainWindow::readOutput()
{
    qDebug()<<"Read";
    QByteArray outData = myProcess->readAllStandardOutput();
    qDebug()<<QString(outData);
}

输出:

Read 
"File read in 0 seconds.
" 
Read 
"cacheFi" 
Read 
"le -attachFile -fileName "nClothShape1" -directory ...

最后一根弦断了。“阅读”出现在单词之间。

4

1 回答 1

8

从文档QProcess::readyReadStandardOutput()

当进程通过其标准输出通道 (stdout) 提供新数据时,会发出此信号。无论当前读取通道如何,都会发出它。

槽执行不止一次,原因很简单,即底层进程以单独和随机的方式刷新输出。你不应该关心这个,因为它取决于你无法控制的事情。

如果你想保存整个输出你应该做

void MainWindow::readOutput(){
   bigbuffer.append(myProcess->readAllStandardOutput();)
}

如果你想逐行阅读,那么

void MainWindow::readOutput(){
   while(myProcess.canReadLine()){
       qDebug() << myProcess.readLine();
  }
}

第二次调用会将数据留在进程缓冲区中,这样您就不会像cacheFi.

于 2013-02-01T10:18:41.860 回答