PDA

View Full Version : QProcess in a loop, works but....



Joelito
24th November 2011, 18:02
This my code:


class MyQTForm : public QMainWindow {
private:
Q_OBJECT
QProcess *process;
//...

Now the part I want to process:


void MyQTForm::cb_process_dir(const QString& _in, const QString& _out) {
QDir in_dir (_in);
QStringList filters;
filters << "*.mp3" << "*.flv" << "*.mp4";
QStringList dir_list = in_dir.entryList (filters);
int index = 0;
int total = dir_list.size();
for (; index < total; index++) {
QString file_in = _in + QString("/") + dir_list[index];
QFileInfo fi(file_in);
QString file_out = _out + QString("/") + fi.baseName() + QString(".mp3");
cb_process (file_in, file_out); // a lot of files to process
}
}

void MyQTForm::cb_process (const QString& file_in, const QString& file_out) {
QString cmd;
QTextStream(&cmd) << "ffmpeg -i \"" << file_in << "\" -f mp3 -ab 128k \"" << file_out << "\"";
process = new QProcess(this);
process->setStandardOutputFile(tr("/dev/null"));
connect(process, SIGNAL(started()),this, SLOT(onProcessStarted ()));
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),this, SLOT(onProcessEnded ()));
connect(process, SIGNAL(finished(int,QProcess::ExitStatus)), process, SLOT(deleteLater()) );
process->start(cmd);
}

void MyQTForm::onProcessStarted() {
activo = true;
menuBar()->setEnabled(!activo);
statusBar()->showMessage(tr("Convirtiendo..."));
}

void MyQTForm::onProcessEnded() {
activo = false;
menuBar()->setEnabled(!activo);
if (check1->isChecked()) {
//QFile::remove (_out_file_tmp);
}
statusBar()->showMessage(tr("Hecho"));
}

This are the results:
1.- ffmpeg is launch as many times as many files are passed to cb_process, bad idea if we found a lot :\
2.- at some point I get: QProcess: Destroyed while process is still running.

My idea is that " process->start(cmd);" sould wait untill the current ffmpeg operation is done, so it can continue the loop...any help, please?

Santosh Reddy
25th November 2011, 00:39
Then avoid the for loop and start the next process in the slot "onProcessEnded()"

Joelito
25th November 2011, 01:53
How should I avoid the for loop if I want to convert files in "dir_list" from the method "cb_process_dir"? :confused:

myta212
27th November 2011, 15:43
Hi,
You can use void QProcess::finished ( int exitCode, QProcess::ExitStatus exitStatus ) [signal] to running QProcess in a loop. This signal will executed after the current QProcess active finished. This is my simple QProcess::finished function contents :

//this function will executed at the end of every process
void Qt_Loop_QProcess::finishedProcess ( int exitCode, QProcess::ExitStatus exitStatus )
{
ui.textReport->append("complete"); //finishid process command

if(idxProcess<nExtension) //run qprocess again until all extension finished
{
ui.listCommand->setCurrentRow(idxProcess);

/* create QProcess object */
proc= new QProcess();
QString strCommand = setCommand(listExtension.at(idxProcess));
proc->start("/bin/bash", QStringList() << "-c" << strCommand);

/* show output */
connect(proc, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage()) );
connect(proc, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage()) );
connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(finishedProcess(int, QProcess::ExitStatus)) );

idxProcess++;
}
}
I get this tutorial at here http://toto-share.com/2011/11/qt-qprocess-in-a-loop. So, if you interest, you can download the complete source code at that location :)

Best regards,

Myta