Results 1 to 4 of 4

Thread: start new command after finishing previous command in QProcess

  1. #1
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation start new command after finishing previous command in QProcess

    In mainwindow.h, i have a private QProcess p1.

    In mainwindow.cpp, I run a command by p1.start(cmd). Both <finished> and <started> signals have been handled. After getting <started> signal, "started" message is printed to output.

    When <finished> signal was received, in some times after that i run again cmd by p1.start(cmd).
    But now in output i see two "started" messages.

    If i run again p1.start(cmd) after finishing it for third times, i see three "started" message in output.

    I debug and find out that the function handles <started> signal, is called second times, third times, ... .

    Why does it happen?

    Thank you for helping.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: start new command after finishing previous command in QProcess

    are you sure to write right?

    can you post the code of your slot?
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: start new command after finishing previous command in QProcess

    Requested codes are in the below.

    Note:
    In UI, when i click on run menu item in menu bar, on_run_QAction_triggered() is called. Then run_QP that was defined as (QProcess run_QP) in mainwindow.h, runs CMD1 command.
    For run again CMD1 after finishing it, i click again on run menu item.

    print2Output function prints to output given str.
    Qt Code:
    1. void MainWindow::on_run_QAction_triggered()
    2. {
    3. run_QP.start(CMD1);
    4. connect(&run_QP, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(run_HandleFinished(int, QProcess::ExitStatus)));
    5. connect(&run_QP, SIGNAL(started()), this, SLOT(run_HandleStarted()));
    6. }
    7.  
    8. void MainWindow::run_HandleFinished(int ecode, QProcess::ExitStatus estat)
    9. {
    10. handleFinished("comp1", ecode, estat);
    11. }
    12.  
    13. void MainWindow::run_HandleStarted()
    14. {
    15. handleStarted("comp1");
    16. }
    17.  
    18. void MainWindow::handleFinished(QString comp, int exitCode, QProcess::ExitStatus exitStat)
    19. {
    20. QString tmp = QString("%1 %2 %3.");
    21. switch (exitStat)
    22. {
    23. case QProcess::NormalExit:
    24. print2Output(tmp.arg(comp, "exited normaly with ", QString::number(exitCode)));
    25. break;
    26. case QProcess::CrashExit:
    27. print2Output(tmp.arg(comp, "crashed with ", QString::number(exitCode)));
    28. break;
    29. }
    30. }
    31.  
    32. void MainWindow::handleStarted(QString comp)
    33. {
    34. print2Output(comp + " was started");
    35. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: start new command after finishing previous command in QProcess

    Thi is your code

    Qt Code:
    1. void MainWindow::on_run_QAction_triggered()
    2. {
    3. run_QP.start(CMD1);
    4. connect(&run_QP, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(run_HandleFinished(int, QProcess::ExitStatus)));
    5. connect(&run_QP, SIGNAL(started()), this, SLOT(run_HandleStarted()));
    6. }
    To copy to clipboard, switch view to plain text mode 
    You connect QProcess::started and QProcess::finished signal each time you call MainWindow:n_run_QAction_triggered.

    Try Connecting signals once outside the slot (in the constructor for example)
    A camel can go 14 days without drink,
    I can't!!!

  5. The following user says thank you to mcosta for this useful post:

    alak (23rd February 2011)

Similar Threads

  1. Shell command from Qt 4.5.2 by QProcess
    By Rajeshsan in forum Newbie
    Replies: 5
    Last Post: 21st December 2009, 16:03
  2. QProcess, communicate with 'su' command
    By Mystical Groovy in forum Qt Programming
    Replies: 6
    Last Post: 20th October 2009, 12:40
  3. Replies: 4
    Last Post: 14th July 2009, 03:27
  4. System Command QProcess
    By jd in forum Qt Programming
    Replies: 7
    Last Post: 10th February 2009, 17:36
  5. how to use cat command in QProcess
    By renjithmamman in forum Qt Programming
    Replies: 4
    Last Post: 27th January 2006, 18:46

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.