0

I have a slot that is called passing some arguments used in a QProcess. I execute a external program with some of this arguments. The problem is that I want to have a queue for these processes, waiting until the previous process is finished to launch the next.

What do you think is the best/easiest way?

Here my method/slot:

void FirstCase::CallApp(QString text, QString pathAndFileName)
{
   QString command = QString("App1.exe %1 -Z %2").arg(pathAndFileName).arg(message); 
   QProcess* process = new QProcess(this);
   process->start(command);
}

EDIT Here the solution, if you need it:

Following the QStringList idea, my CallApp method just adds the command to the QStringList like:

list << command;

And then calls to Execute();

void FirstCase::Execute()
{
   if(!list_.isEmpty()&&!executing_)
    {
        QProcess* p = new QProcess(this);
        QString c = list_.takeFirst();
        p->start(c);
        executing_=TRUE;
        connect(p, SIGNAL(finished(int)),this,SLOT(ExecFinished()));
    }
}

void FirstCase::ExecFinished()
{
    executing__=FALSE;
    Execute();
}
legami
  • 1,303
  • 6
  • 22
  • 31
  • I strongly suggest to use the QStringList overload of start() with separated arguments. Otherwise you'll run into quoting issues pretty soon (e.g. if the program you execute has a space in its path). The QStringList() version does the quoting automatically. – Frank Osterfeld Jan 17 '11 at 13:41
  • You are using the the finished signal wrong, see my answer below. – ismail Jan 17 '11 at 15:42

2 Answers2

4

You can use a QString queue to queue up your commands and store them. Then, start from the top of the queue, start a new process and connect its finished() signal to a slot. When that process finishes, start a new process if the queue is not empty and so forth.

erelender
  • 6,175
  • 32
  • 49
0

The correct signature for finished function is;

void QProcess::finished ( int exitCode, QProcess::ExitStatus exitStatus )

so you should connect like this;

connect(p, SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(ExecFinished()));
ismail
  • 46,010
  • 9
  • 86
  • 95