Is it possible to prevent closure of standard out terminal? (Consume "X" close click?) (Windows)

  • We have a large Qt 4.8.5 Windows desktop C++ / QWidget application which prints some information to standard out. That shows a terminal window. Unfortunately, clicking the Close-Window ("X") title-bar button on that terminal window, the entire application exists.

    Of course, in Qt, clicking that Close-Window ("X") button in a QWidget's window can be ignored by intercepting and calling ignore() on the QCloseEvent. I don't imagine that is relevant to the terminal window's close-window button.

    Is there some Windows event handler (or something) we can define to consume that event? (or something) to prevent that window from closing, or at least prevent the application from exiting?

    (We're using Visual Studio 2010, mostly Windows 7).
    Thank you in advance.

    • Phil

  • Moderators

    No, and that's a good thing!

    The "terminal" is a separate GUI program that you don't get windowing events from. As a matter of fact when you run console app it doesn't even know it is run in a window, because it really doesn't have to be. Imagine some remote or fullscreen kiosk mode access or just the windows recovery console (which doesn't even have window manager).

    The interaction with your app is limited to consuming your stdout and providing stdin (or cout/cin). It also terminates your app when X is pressed. But it's not like pressing X in a windowing app that simply sends WM_CLOSE message to your app window handler. As far as your app is concerned there is no window and no window event handler.

    If you want control over windowing events create a windowing (widget) application. It's not hard to make a simple widget that will redirect and consume/provide stdin/stdout.

  • Chris, thank you for your response. I realize that the terminal window is outside of the Qt event world. It's a separate GUI program, as you say. But not separate enough! Closing it kills our program! We need to prevent that.

    In your last paragraph, you suggested that standard out from our main Qt application could be redirected to another Qt application we could provide. ("easy"). Am I understanding that correctly? How would we do that? (Can that be set up programmatically from within our main application?)

    OR does anyone else know how this could be accomplished at a lower level with some Windows API?

  • Moderators

    Well think about it like a browser - web page relation. Web page shouldn't be able to prevent browser from closing, but the other way around is ok.

    As for redirection, you can approach this a couple of ways.

    One way is to just turn your app into a GUI app and instead of writing to stdout log your output to some text widget like QTextEdit. If you can use qDebug and friends for output then the easiest way is to redirect it via "qInstallMessageHandler":

    Another way, if you want to keep your main app as it is, is to create a simple GUI app with just the QTextEdit and start your main app from it via "QProcess": With that you can connect to "readyReadStandardOutput": and "readyReadStandardError": signals to get the output and put it into text box. You can of course style it to look like a Windows console if you like.

Log in to reply