Results 1 to 9 of 9

Thread: Can't send a Ctrl-C to a QProcess (Win32)

  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Can't send a Ctrl-C to a QProcess (Win32)

    I create a Process class from QProcess to run MEncoder.

    Everything's fine and I can abort the process with kill() method.

    However in Win32 CLI, running MENcoder and typing Ctrl-C make MEncoder to stop smoothly.

    Therefore I try to stop smoothly writing Ctrl-C (0x03) with:

    Qt Code:
    1. class Process : QProcess
    2. {
    3. ...
    4. char ctrlC = 0x03;
    5. write( &ctrlC );
    6. ...
    To copy to clipboard, switch view to plain text mode 

    And it just does nothing
    Last edited by wysota; 12th December 2010 at 17:23. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't send a Ctrl-C to a QProcess (Win32)

    It's not how it works. Ctrl+C when issued to the terminal of the process (not to its standard input) causes the operating system to send a SIGTERM signal to the process. So what you have to do is to use QProcess API to send a SIGTERM signal to your child process - use terminate() instead of kill(). The latter sends SIGKILL instead of SIGTERM.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't send a Ctrl-C to a QProcess (Win32)

    Thanks for replying, nevertheless, it isn't working exactly like you suggest:

    with terminate() the process just continues... I guess the program expects a SIGINT (3) as Ctrl-C and Windows has no SIGINT support (taskklist /t ot /f).

    Any other idea?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't send a Ctrl-C to a QProcess (Win32)

    Sorry, yes, Ctrl+C is SIGINT. Nevertheless using terminate() is the way to go. Unfortunately console applications don't support being killed by terminate(). What you can do is try to send the SIGINT signal yourself to the other process using ::kill() (your system should support it).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2013
    Posts
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Can't send a Ctrl-C to a QProcess (Win32)

    Sorry for the late entrance, but it would seem this is still a valid discussion. I have just solved the problem under Windows 7 with Qt5 by closing the stream to stdin of the child process. I.e. process->close() instead of process->terminate(). The child process was then gracefully able to shut down.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't send a Ctrl-C to a QProcess (Win32)

    Maybe because your child program ends when it gets end of stream on its stdin. It's not the case for any program.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2013
    Posts
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Can't send a Ctrl-C to a QProcess (Win32)

    My scenario works from command line, but not via QProcess, not even using close() as I suggested earlier. This is very curious, as there seem to be a way of signaling the console application (i.e node.exe in my case) to close, but it is not used by QProcess::terminate(). I have solved my particular problem by writing a shutdown command to stdin and reading it in the child process, but it is not a generic solution.

    A similar discussion is found on qt-project: http://qt-project.org/forums/viewthread/28029

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't send a Ctrl-C to a QProcess (Win32)

    A well-behaved process should terminate itself when receiving SIGTERM which is what QProcess::terminate() sends.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't send a Ctrl-C to a QProcess (Win32)

    This reply comes a bit late.

    It is unfortunate that Windows' API does not deal with GUI and console processes in a uniform way. The documentation of QProcess::terminate() clearly states that it only supports the former kind. In you case, I suggest -- for lack of a better solution -- that you rely on platform-specific API for console processes; the GenerateConsoleCtrlEvent function looks promising.

Similar Threads

  1. How to send a "Ctrl+V" event to a QWebView
    By daudiam in forum Qt Programming
    Replies: 2
    Last Post: 1st December 2010, 10:51
  2. POSIX/Win32 Port involving QProcess, QSharedMemory
    By sfabel in forum Qt Programming
    Replies: 0
    Last Post: 16th July 2010, 00:00
  3. filterout the Alt+Ctrl+Del
    By sudhansu in forum Qt Programming
    Replies: 6
    Last Post: 25th March 2010, 08:55
  4. CTrl + A
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 23rd October 2008, 10:19
  5. arg!!!! QProcess on win32
    By gfunk in forum Qt Programming
    Replies: 7
    Last Post: 27th January 2008, 19:40

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.