0

I want to create a console application in Qt which handles Close, Minimize and Maximize buttons of the console window. My goal is just to show some message before the application quits - i.e. Close button is clicked.

Further, I want the application to be minimized to the system tray instead of task bar. However, it seems there are no signals or events which I can process when user clicks on one of the system buttons.

Is it even possible?

vitakot
  • 3,786
  • 4
  • 27
  • 59

3 Answers3

1

I do not think you can handle such "signals" (minimize, maximize, and close the terminal window running a QCoreApplication) through the APIs provided by Qt.

But QCoreApplication sends a signal called aboutToQuit(). Probably you can use it to do what you want (write in the terminal, for example), just do not know if the user will be able to read in time.

About minimize the application to the tray: Again, probably not possible to do it in a terminal application using the Qt APIs. But it is perfectly possible in a QApplication (which has a window). See this answer to a similar SO question.

Community
  • 1
  • 1
borges
  • 3,627
  • 5
  • 29
  • 44
  • I know about the 'aboutToQuit()' signal, but according to the documentation no user interaction is possible in this state. I also know how to create system tray icon and menu, it works, I just can't catch the button clicks and thus call the appropriate handlers. You are right, it is not possible in Qt, but there is a WinApi handler 'SetConsoleCtrlHandler' which I can install and so handle the system clicks, but I can't get it working properly. I give up; my app will have a simple GUI... – vitakot Jan 12 '12 at 17:37
0

Are you using a Unix / Linux or a Windows? If you are using Unix or Linux you might want to look into the Posix / Unix Signals. They do not offer any solution to the maximize / minimize buttons, but they give you the possibility to at least catch the System Signal when you hit the close button. From my experience, the aboutToQuit() Signal is not as fast as a custom override of the Signal Handler.

As soon as you catch the Signal you can process it yourself.

Here's a good tutorial for the Custom Signal Handlers:

http://doc.qt.io/qt-5/unix-signals.html

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
mmoment
  • 1,269
  • 14
  • 30
0

You can use POSIX signals both in Linux and Windows #include

void quit_handle( int ) {
    qApp->quit();
}
int main( int argc, char *argv[] ) {
    QCoreApplication a(argc, argv);
    ...
    signal( SIGINT, quit_handle );
    return a.exec();
}

I tested it only in Windows+MinGW, but I think it would work in Linux too

borisbn
  • 4,988
  • 25
  • 42