SteeveDroz
SteeveDroz

Reputation: 6136

QProcess::readAllStandardOutput() truncates result

My Qt program needs to send a command line with a QProcess and retrieve the result, which will then be stored in a QString.

Here is my code:

MainWindow.h

class MainWindow : public QMainWindow
{
  Q_OBJECT

private:
  QProcess p;

  void sendCommand(QString command);

private slot:
  void fetchResult();

  // ...
}

MainWindow.cpp

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  connect(&p, SIGNAL(readyReadStandardOutput()), this, SLOT(fetchResult()));

  // ... 
}

void MainWindow::fetchResult()
{
  QString result = p.readAllStandardOutput();

  // ...
}

void MainWindow::sendCommand(QString command)
{
  p.start(command);
  p.waitForFinished();
}

// ...

I then send commands like this: sendCommand("cat " + filename); (for example) and I expect to get the result in the result variable located in fetchResult().

Everything works like a charm, but... if the result is too big (~700 chars) , it is trucated. Strange thing: the variable contains the end of the string I am expecting.

Where do I miss something.

Upvotes: 0

Views: 442

Answers (1)

Tom Kim
Tom Kim

Reputation: 436

As you want to wait until the end of the execution, try this, it works for me:

  • add Slot like private slots: void cmdFinished(); to receive QProcess::finished signal:
void MainWindow::cmdFinished()
{
    // process Standard Output result
    fetchResult();

    // process Standard Error result
    //fetchErrResult(); // add it if you want to process Errors (p.readAllStandardError())
}
  • connect only QProcess::finished(int) (remove connection to the signal readyReadStandardOutput()):
connect( &p, SIGNAL(finished(int)),
         this, SLOT(cmdFinished()) );

Hope it helps you.

Upvotes: 1

Related Questions