Alan
Alan

Reputation: 1265

QProcess ReadAllStandardError()

I need to connect a QProcess to an error handler, but I'm unsure how to pass the error string to the slot. What's below compiles, but doesn't work.

QString MainWindow::RunProcess(QString cstring)
{
 QProcess *process = new QProcess(this);
 connect(process,SIGNAL(readyReadStandardError()),this,SLOT( adberror(process::ReadAllStandardError() ) ))
 process->start(cstring);    
}



void MainWindow::adberror(QString errtxt)
{  
  qDebug() << "error handler"; 
  qDebug() << errtxt;
 }

I can induce a a process error, but adberror() never triggers.

When run, in the Application Output pane I see:

QObject::connect: No such slot MainWindow::adberror(process::ReadAllStandardError() )
QObject::connect:  (receiver name: 'MainWindow')

edit: this is Qt 5.6. I did a new qmake/clean.

Upvotes: 0

Views: 1896

Answers (2)

HazemGomaa
HazemGomaa

Reputation: 1630

you have two options

1- wait before reading the output

QString MainWindow::RunProcess(QString cstring)
{
  QProcess process;
  process.start(cstring);
  process.waitForFinished();
  QString str = process.readAllStandardOutput();
}

2- make you process a member variable and remove your 1st argument from adberror. So,

in RunProcess

connect(process,SIGNAL(readyReadStandardError()),this,SLOT(adberror()))

then in adberror

QString str = process->readAllStandardOutput();

note that in your code you have a problem since your signal and slot args don't to match .. Also, ReadAllStandardError is not going to be ready anyways !

Edit: more code for the 2nd solution

mainwindow.h

class MainWindow
{
   private://methods
      void adberror();

   private://attributes
      QProcess* process;

};

mainwindow.cpp

QString MainWindow::RunProcess(QString cstring)
{
   process = new QProcess(this);
   connect(process,SIGNAL(readyReadStandardError()),this,SLOT(adberror()));   
   connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater())); 
   process->start(cstring);    
}

void MainWindow::adberror()
{  
   QString str = process->readAllStandardOutput();
   qDebug() << str;
}

Upvotes: 0

Nikita
Nikita

Reputation: 6427

To process the readyReadStandardError() signal you should define the slot as:

private slots:
  void adberror();

and use it:

connect(process,SIGNAL(readyReadStandardError()),this,SLOT( adberror() ));

i.e. with no arguments. Keep child process as a field of your MainWindow class to read data when it will be available.

Upvotes: 0

Related Questions