Results 1 to 20 of 21

Thread: QProcess disconnect trouble

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess disconnect trouble

    Quote Originally Posted by fatjuicymole View Post
    It depends on what your process does. If it has a systray icon, then it must have an event loop.
    Understandable. But the QProcess that is running is not necessarily in the Systray. The program that calls the QProcess runs in the systray.

    Eitherway, the signals on the QProcess do not get disconnected. I tested connecting/disconnecting signals from other random objects (like a spinbox) and that works fine.

    I'm starting to think that the signals of a QProcess that has an active process cannot be disconnected while the process is still running.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess disconnect trouble

    Have you tried blockSignals ?

  3. #3
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess disconnect trouble

    Yup. See post #6

    Didn't work either.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QProcess disconnect trouble

    Maybe it's a windows/vs problem. You might try your app on Linux or do somethiing simple like the following to see if it works:

    I created a new GUI project with Qt Creator. Used your code in the MainWindow constructor to start a process (a php script that waits for input on stdin) and make connections, put a push button in the form, and created two slots:
    Qt Code:
    1. void MainWindow::on_pushButton_clicked(){
    2. disconnect(process,SIGNAL(error(QProcess::ProcessError)),0,0);
    3. process->kill();
    4. }
    5.  
    6. void MainWindow::handleProcessError(QProcess::ProcessError){
    7. qDebug() << process->errorString();
    8. }
    To copy to clipboard, switch view to plain text mode 
    It silently kills the process as shown. If I comment out the disconnect statement I get "Process crashed".

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

    wally (6th April 2010)

  6. #5
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess disconnect trouble

    Quote Originally Posted by norobro View Post
    Maybe it's a windows/vs problem. You might try your app on Linux or do somethiing simple like the following to see if it works:

    I created a new GUI project with Qt Creator. Used your code in the MainWindow constructor to start a process (a php script that waits for input on stdin) and make connections, put a push button in the form, and created two slots:
    Qt Code:
    1. void MainWindow::on_pushButton_clicked(){
    2. disconnect(process,SIGNAL(error(QProcess::ProcessError)),0,0);
    3. process->kill();
    4. }
    5.  
    6. void MainWindow::handleProcessError(QProcess::ProcessError){
    7. qDebug() << process->errorString();
    8. }
    To copy to clipboard, switch view to plain text mode 
    It silently kills the process as shown. If I comment out the disconnect statement I get "Process crashed".
    Hmmm.. Maybe it is a Windows problem since that's essentially what I tried.

    What version of Qt are you using?

    I will try this in Linux and/or QtCreator tomorrow.

  7. #6
    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: QProcess disconnect trouble

    From post 7:
    Qt Code:
    1. disconnect(process, SIGNAL(error(QProcess::ProcessError)), 0, 0);
    2. disconnect(process, SIGNAL(finished(int exitCode, QProcess::ExitStatus)), 0,0);
    3. process->kill();
    To copy to clipboard, switch view to plain text mode 
    should not list the name of the int parameter at line 2. Perhaps this leaves the finished() signal still connected?

    Have you tried calling QProcess::terminate() (close gracefully) rather than QProcess::kill() (close with mallet)? I cannot imagine many Windows applications that ignore the WM_CLOSE message sent by terminate()

  8. The following user says thank you to ChrisW67 for this useful post:

    wally (6th April 2010)

  9. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QProcess disconnect trouble

    Quote Originally Posted by ChrisW67 View Post
    should not list the name of the int parameter at line 2. Perhaps this leaves the finished() signal still connected?
    I missed that. I guess that is what borisbn is alluding to in post #4.
    I put that slot in my app with the disconnect error and received a "no such slot" warning on the command line but the process still terminated without complaining.

  10. #8
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess disconnect trouble

    Quote Originally Posted by ChrisW67 View Post
    From post 7:
    Qt Code:
    1. disconnect(process, SIGNAL(error(QProcess::ProcessError)), 0, 0);
    2. disconnect(process, SIGNAL(finished(int exitCode, QProcess::ExitStatus)), 0,0);
    3. process->kill();
    To copy to clipboard, switch view to plain text mode 
    should not list the name of the int parameter at line 2. Perhaps this leaves the finished() signal still connected?

    Have you tried calling QProcess::terminate() (close gracefully) rather than QProcess::kill() (close with mallet)? I cannot imagine many Windows applications that ignore the WM_CLOSE message sent by terminate()
    Sorry that's a typo.. I don't have the name of the int parameter in my actual code.

    Regarding QProcess::terminate() vs. QProcess::kill(), the documentation says that "Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling kill()." Since, I'm using this program to run any possible program (including console applications) I will have to stick with the "mallet."

  11. #9
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess disconnect trouble

    Hello Everyone,

    Thank you for your help. The solution was my lack of awareness on what's happening when you call QProcess::kill(). I was trying to disconnect and then reconnect my signals by calling the connect immediately after calling kill() (like in post #6 where I call blockSignals before and after the kill call.

    I got the idea from fatjuicymole when he made a comment about my post #6. The process object was getting its signals reconnected before the external program had been terminated, since QProcess::kill() is not a synchronous function as it does not wait for the external function to be terminated.

    Now when I press the stop button, the slot stopProcess contains the following:
    Qt Code:
    1. disconnect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(handleProcessError(QProcess::ProcessError error)));
    2. disconnect(process, SIGNAL(finished(int, QProcess::ExitStatus )), this, SLOT(handleProcessFinish(int, QProcess::ExitStatus)));
    3. process->kill()
    To copy to clipboard, switch view to plain text mode 

    and then when I press the start button I reconnect the signals, e.g.:
    Qt Code:
    1. connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(handleProcessError(QProcess::ProcessError error)));
    2. connect(process, SIGNAL(finished(int, QProcess::ExitStatus )), this, SLOT(handleProcessFinish(int, QProcess::ExitStatus)));
    3. process->start(processName);
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help everyone. The problem, like usual, ended up being the user. You learn something new everyday, even though you realize that you should've known it anyways

  12. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess disconnect trouble

    Quote Originally Posted by wally View Post
    Regarding QProcess::terminate() vs. QProcess::kill(), the documentation says that "Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling kill()." Since, I'm using this program to run any possible program (including console applications) I will have to stick with the "mallet."
    A better idea might be to use QProcess::terminate(), and if the process doesn't terminate within some time frame, then use QProcess:kill(). At least then it'll give the process that amount of time to cleanup if necessary, and will also work in situations where the process doesn't handle WM_CLOSE.

  13. #11
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QProcess disconnect trouble

    @wally - I'm using Qt 4.6.0 on Linux.

    I recently installed Qt 4.6.2 on my WinXP partiton and thought I'd give my simple app a try. I started "calc.exe" as a process and it ended silently. So disconnect worked here. Also it ended whether I used the "mallet" (TM ChrisW67) or terminate() .

  14. The following user says thank you to norobro for this useful post:

    wally (6th April 2010)

  15. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess disconnect trouble

    Quote Originally Posted by wally View Post
    Yup. See post #6

    Didn't work either.
    In post #6, you re-enable directly after the kill. I was thinking of leaving the blocking active until the process is restarted, just in case some signals are in the event queue.

  16. The following user says thank you to squidge for this useful post:

    wally (6th April 2010)

  17. #13
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess disconnect trouble

    Quote Originally Posted by fatjuicymole View Post
    In post #6, you re-enable directly after the kill. I was thinking of leaving the blocking active until the process is restarted, just in case some signals are in the event queue.
    Good point. I'll give that a shot.

Similar Threads

  1. Disconnect slot when another is being connected
    By holst in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2009, 09:49
  2. auto-disconnect in ~QGraphicsItem?
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2008, 08:14
  3. QTcpSocket not recognizing disconnect
    By jimroos in forum Qt Programming
    Replies: 3
    Last Post: 6th September 2007, 15:31
  4. How to make QHttp detect a disconnect?
    By gfunk in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2007, 10:07
  5. qsqldatabase does not sense db disconnect
    By rburge in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2006, 18:59

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
  •  
Qt is a trademark of The Qt Company.