Skip to content
Advertisement

Linux Qt QSystemTrayIcon in console application

I have a working qt application. It is not a MainWindow application and has no GUI elements at all so far. It runs on startup of the linux box (as a background service). However, it has to wait for ~40-60 seconds until it can start while the IP/DHCP sorts its self out.

So I wanted to give the user some feedback as to what is going on. I thought it might be nice to add a system tray icon to look a little bit more professional then a terminal (though I could just do that).

However, after reading the documentation on QSystemTrayIcon I am still not sure if I have to use it as part of a MainWindow style application (i.e. do I need to add main window and make the sys tray a part of that), or can I just run it within my “console” style app?

Advertisement

Answer

i created a console application with system tray myself for win7 so i have no info about linux clients. it does not display any console, it only uses logging for output and the systemtray to display ‘user readable content’.

your application need to use QApplication instead of QCoreApplication in order to draw widgets and create the QSystemTrayIcon to display the system tray icon. now just fill the menu as you please.

the code in main.cpp might look like this:

int main(int ac, char** av){
  QApplication a(ac, av);
  QSystemTrayIcon i;
  QMenu* m = i.contextMenu();
  m->addAction(tr("hello world!"));
  i.show();
  return a.exec();
}

cheers

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement