【问题标题】:Why QProcess signal readyReadStandardOutput() emited twice?为什么 QProcess 信号 readyReadStandardOutput() 发出两次?
【发布时间】:2013-02-01 10:02:43
【问题描述】:

我使用 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 ...

最后一个字符串坏了。 “阅读”出现在单词之间。

【问题讨论】:

    标签: qt signals qprocess


    【解决方案1】:

    来自QProcess::readyReadStandardOutput()的文档

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

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

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

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

    如果你想逐行阅读,那么

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

    第二次调用会将数据留在进程缓冲区中,这样您就不会像cacheFi那样出现“损坏”的读取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      相关资源
      最近更新 更多