Results 1 to 4 of 4

Thread: Sequential execution of QProcesses

  1. #1
    Join Date
    Mar 2017
    Posts
    12
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Sequential execution of QProcesses

    I have 5 QT processes in a slot, which invokes 5 shell scripts on linux system. I have arranged them in a sequential manner but when I had a look at logs, came to know that the processes are not running in a sequential manner. The order of execution gets change
    For eg
    Qt Code:
    1. void Window::slotProcess(){
    2.  
    3. statusWindow->setText("Running process");
    4.  
    5. /******************* STEP 1: START A NEW PROCESS 1 ***************/
    6. Process_1 = new QProcess(this);
    7. // Connet SIGNAL::STANDARD ERROR with SLOT::UPDATE ERROR, to capture std output
    8. qDebug() << connect(Process_1, SIGNAL(readyReadStandardError()), this, SLOT(UpdateError()));
    9. // Connect SIGNAL::STANDARD OUTPUT with SLOT::UPDATE OUTPUT to capture std error
    10. qDebug() << connect(Process_1, SIGNAL(readyReadStandardOutput()), this, SLOT(UpdateOutput()));
    11. Process_1->start("/home/root/scripts/script_1.sh");
    12.  
    13. /******************* STEP 2: START A NEW PROCESS 2 ***************/
    14. Process_2 = new QProcess(this);
    15. // Connet SIGNAL::STANDARD ERROR with SLOT::UPDATE ERROR, to capture std output
    16. qDebug() << connect(Process_2, SIGNAL(readyReadStandardError()), this, SLOT(UpdateError()));
    17. // Connect SIGNAL::STANDARD OUTPUT with SLOT::UPDATE OUTPUT to capture std error
    18. qDebug() << connect(Process_2, SIGNAL(readyReadStandardOutput()), this, SLOT(UpdateOutput()));
    19. Process_2->start("/home/root/scripts/script_2.sh");
    20.  
    21. .....
    To copy to clipboard, switch view to plain text mode 

    But when I see debug messages, it shows there is no sequence in execution. How can I control the sequence of execution of my processes?
    The wait for finish method will block the code, is there an alternative ?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sequential execution of QProcesses

    Use QProcess::waitForFinished before starting next process.
    Oops : You do not want to use a waitForFinished.

    Create one master script which will call subscripts.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sequential execution of QProcesses

    Lesiok's option is probably the least effort. Here are some other options:

    Create a slot for each step. In the slot for step 1 start the relevant process and connect() its finished() signal to the slot for step 2... and so on. You should check the exit state of step n at the top of step n+1 before starting the new step's process.

    Or, between steps:
    Qt Code:
    1. connect(process, SIGNAL(finished(...)), &loop, SLOT(quit()));
    2. loop.exec();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2007
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sequential execution of QProcesses

    Quote Originally Posted by ChrisW67 View Post
    Lesiok's option is probably the least effort. Here are some other options:

    Create a slot for each step. In the slot for step 1 start the relevant process and connect() its finished() signal to the slot for step 2... and so on. You should check the exit state of step n at the top of step n+1 before starting the new step's process.

    Or, between steps:
    Qt Code:
    1. connect(process, SIGNAL(finished(...)), &loop, SLOT(quit()));
    2. loop.exec();
    To copy to clipboard, switch view to plain text mode 

    The problem here, is you are assuming that the QProcess is still running when you start the event loop.

    With the following code
    Qt Code:
    1. QProcess process;
    2. connect(process, SIGNAL(finished(...)), &loop, SLOT(quit()));
    3. process.start();
    4. loop.exec()
    To copy to clipboard, switch view to plain text mode 

    its possible for short running executables that the process ends before the loop.exec command. This has bit me in the butt more than once.

    The best solution I have come up with is the following
    Qt Code:
    1. QProcess process;
    2. bool processFinished = false;
    3. connect(process, &QProcess::finished,
    4. [&loop, &processFinished]()
    5. {
    6. processFinished = true;
    7. if ( loop.isRunning() )
    8. loop.exit();
    9. }
    10. )
    11. process.start();
    12. loop.exec()
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Instructions are NOT sequential in qt/qml?!?
    By JaySDC in forum Qt Quick
    Replies: 9
    Last Post: 24th October 2015, 18:41
  2. Sequential animations on each listitem
    By rama.kesi in forum Qt Quick
    Replies: 3
    Last Post: 12th October 2012, 04:36
  3. Sequential animations on each listitem
    By rama.kesi in forum Qt Quick
    Replies: 0
    Last Post: 8th October 2012, 06:24
  4. Multiple QProcesses running in parallel
    By doggrant in forum Qt Programming
    Replies: 4
    Last Post: 3rd November 2010, 17:41
  5. How should I thread these QProcesses?
    By last2kn0 in forum Qt Programming
    Replies: 8
    Last Post: 9th October 2007, 05:56

Tags for this Thread

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.