Add a System Tray to a Qt Application
A quick intro on how to add a system tray to a Qt application
Brief intro

Start building
Add the tray icon
Use QSystemTrayIcon. First create one in .h under private:
1 | |
Then implement in .cpp:
1 | |
Check the icon setup. If it fails, there will be no error but the tray will not show.
Next, add events for the tray icon:
1 | |
Add the tray menu
This is the menu shown when you right-click the tray icon. Use QMenu. Create in .h:
1 | |
Then in .cpp:
1 | |
Add menu items
A menu needs actions. Use QAction to add actions. Here is an example with “Show Main Window” and “Exit”:
Add in .h:
1 | |
Then before setting the menu on the icon, add:
1 | |
You also need to add actions for these buttons.
Add in private slots:
1 | |
Implement them:
1 | |
Finally, connect signals and slots:
1 | |
Add a System Tray to a Qt Application
https://greatzaochen.dev/en/posts/dce12727/