Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why QProcess signal readyReadStandardOutput() emited twice?

I use QProcess and connect it's readyReadStandardOutput to slot. But after starting slot execute twice. Tell me please why is it?

{
    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);
}

OUTPUT:

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

Last string was broken. "Read" appears between words.

like image 667
artberry Avatar asked Dec 21 '25 09:12

artberry


1 Answers

From the documentation of QProcess::readyReadStandardOutput()

This signal is emitted when the process has made new data available through its standard output channel (stdout). It is emitted regardless of the current read channel.

The slot is executed more than once for the simple reason that the underlying process flushed the output in separate and random ways. You should not be caring about this because it depends on things you cannot control.

If you want to save the whole output you should be doing

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

If you want to read line by line, then

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

The second call will leave data in the process buffer such that you don't have "broken" reads like cacheFi.

like image 58
UmNyobe Avatar answered Dec 23 '25 00:12

UmNyobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!