QProcess

QProcess类用于启动另一个外部应用程序,并可与之进行通信。该类的运行方式是异步的。QProcess类在外部进程拥有数据或完成时发送一个信号以通知相关的对象。

QProcess的使用模板如下:

class XXX : public XXXX
{
     Q_OBJECT
…
private slots:
     void errorHandler(QProcess::ProcessError);
     void catchOutput();
     void catchError();
     void stateMonitor(QProcess::ProcessState newState);
     void slotStarted();
     void slotFinished(int, QProcess::ExitStatus);
…
private:
     QProcess p;
…
};

XXX::XXX(…)
{
…
connect(&p, SIGNAL(started()), SLOT(slotStarted()));
connect(&p, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(slotFinished(int, QProcess::ExitStatus));
connect(&p, SIGNAL(error(QProcess::ProcessError)), SLOT(errorHandler (QProcess::ProcessError)));
connect(&p, SIGNAL(readyReadStandardError()), SLOT(catchError()));
connect(&p, SIGNAL(readyReadStandardOutput()), SLOT(catchOutput()));
…
}

XXX:: slotStarted()
{
   qDebug()<<”process has started”;
}

XXX:: slotFinished(int exitcode, QProcess::ExitStatus status)
{
if (exitStatus == QProcess::CrashExit) {
       …;
    } else if (exitCode != 0) {
        …;
    } else {
       …;
}
}

XXX:: errorHandler(QProcess::ProcessError error)
{
        if (error == QProcess::FailedToStart) {
                  …
        }
       else if( error == QProcess:: Crashed )
       {
             …
       }
       else if( error == QProcess:: Timedout )
       {
          …
       }
       else if( error == QProcess:: WriteError )
      {
          …
       }
      else if( error == QProcess::ReadError )
      {
        …
      }
      else if( error == QProcess:: UnknownError )
      {
         …
       }
}

XXX:: catchError()
{
      QByteArray newData = process.readAllStandardError();
     …

}

XXX::catchOutput()
{
      QByteArray newData = process.readAllStandardOutput();
   …
}


你可能感兴趣的:(object,Class,Signal)