Results 1 to 4 of 4

Thread: QProcess in a loop, works but....

  1. #1
    Join Date
    Oct 2010
    Location
    Tijuana, Baja California, México
    Posts
    19
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy QProcess in a loop, works but....

    This my code:
    Qt Code:
    1. class MyQTForm : public QMainWindow {
    2. private:
    3. Q_OBJECT
    4. QProcess *process;
    5. //...
    To copy to clipboard, switch view to plain text mode 
    Now the part I want to process:
    Qt Code:
    1. void MyQTForm::cb_process_dir(const QString& _in, const QString& _out) {
    2. QDir in_dir (_in);
    3. QStringList filters;
    4. filters << "*.mp3" << "*.flv" << "*.mp4";
    5. QStringList dir_list = in_dir.entryList (filters);
    6. int index = 0;
    7. int total = dir_list.size();
    8. for (; index < total; index++) {
    9. QString file_in = _in + QString("/") + dir_list[index];
    10. QFileInfo fi(file_in);
    11. QString file_out = _out + QString("/") + fi.baseName() + QString(".mp3");
    12. cb_process (file_in, file_out); // a lot of files to process
    13. }
    14. }
    15.  
    16. void MyQTForm::cb_process (const QString& file_in, const QString& file_out) {
    17. QString cmd;
    18. QTextStream(&cmd) << "ffmpeg -i \"" << file_in << "\" -f mp3 -ab 128k \"" << file_out << "\"";
    19. process = new QProcess(this);
    20. process->setStandardOutputFile(tr("/dev/null"));
    21. connect(process, SIGNAL(started()),this, SLOT(onProcessStarted ()));
    22. connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),this, SLOT(onProcessEnded ()));
    23. connect(process, SIGNAL(finished(int,QProcess::ExitStatus)), process, SLOT(deleteLater()) );
    24. process->start(cmd);
    25. }
    26.  
    27. void MyQTForm::onProcessStarted() {
    28. activo = true;
    29. menuBar()->setEnabled(!activo);
    30. statusBar()->showMessage(tr("Convirtiendo..."));
    31. }
    32.  
    33. void MyQTForm::onProcessEnded() {
    34. activo = false;
    35. menuBar()->setEnabled(!activo);
    36. if (check1->isChecked()) {
    37. //QFile::remove (_out_file_tmp);
    38. }
    39. statusBar()->showMessage(tr("Hecho"));
    40. }
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QProcess in a loop, works but....

    Then avoid the for loop and start the next process in the slot "onProcessEnded()"
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Oct 2010
    Location
    Tijuana, Baja California, México
    Posts
    19
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess in a loop, works but....

    How should I avoid the for loop if I want to convert files in "dir_list" from the method "cb_process_dir"?

  4. #4
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess in a loop, works but....

    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 :
    Qt Code:
    1. //this function will executed at the end of every process
    2. void Qt_Loop_QProcess::finishedProcess ( int exitCode, QProcess::ExitStatus exitStatus )
    3. {
    4. ui.textReport->append("complete"); //finishid process command
    5.  
    6. if(idxProcess<nExtension) //run qprocess again until all extension finished
    7. {
    8. ui.listCommand->setCurrentRow(idxProcess);
    9.  
    10. /* create QProcess object */
    11. proc= new QProcess();
    12. QString strCommand = setCommand(listExtension.at(idxProcess));
    13. proc->start("/bin/bash", QStringList() << "-c" << strCommand);
    14.  
    15. /* show output */
    16. connect(proc, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage()) );
    17. connect(proc, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage()) );
    18. connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)),
    19. this, SLOT(finishedProcess(int, QProcess::ExitStatus)) );
    20.  
    21. idxProcess++;
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 
    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

Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 01:40
  2. Replies: 3
    Last Post: 6th July 2011, 06:59
  3. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  4. Replies: 0
    Last Post: 26th August 2010, 10:44
  5. QProcess wo event loop
    By brazso in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2009, 08:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.