QIcon icon = QIcon (":/images/icon. ico"); load the image
Setmediawicon (icon); set the image to the window
TrayIcon = new QSystemTrayIcon (this); Create a system tray icon
TrayIcon-> setIcon (icon); // set the icon
TrayIcon-> setToolTip ("a trayicon example"); // you can specify a prompt.
TrayIcon-> show ();
SetMinimumSize (200,100); the maximum and minimum sizes of windows are the same. Therefore, the window cannot be enlarged or zoomed out.
SetMaximumSize (200,100 );
QApplication: setQuitOnLastWindowClosed (false); // Quit On Last Window Closed exit in Last Window close click Close when the program is not Closed
In the system tray icon, when activated (activation) should be properly processed, it will Trigger the QSystemTrayIcon: ActivationReason signal, the signal may be clicked (Trigger )) double-click, switch should be used to receive processing, for example:
Void class (QSystemTrayIcon: ActivationReason reason ){
Switch (reason ){
Case QSystemTrayIcon: Trigger:
Add your own Processing
Break;
Case QSystemTrayIcon: DoubleClick:
Add your own Processing
Break;
Case QSystemTrayIcon: MiddleClick:
Add your own Processing
Break;
Default:
}
}
You can set the system tray icon to the following:
QIcon (":/images/icon. ico") // load the ico image or
QIcon (":/images/heart. svg") // load svg images
QSystemTrayIcon-> setIcon (QIcon icon); // define a system tray icon and set the icon
Setmediawicon (icon); // set the window icon
QSystemTrayIcon-> setToolTip (string); // defines a system tray icon and sets a prompt for the icon
You also need to set the menu on the icon. As follows:
Void Window: createActions ()
{
MinimizeAction = new QAction (tr ("minimal"), this );
Connect (minimizeAction, SIGNAL (triggered (), this, SLOT (hide (); // hide the window if the minimization is triggered
MaximizeAction = new QAction (tr ("maximize"), this );
Connect (maximizeAction, SIGNAL (triggered (), this, SLOT (showMaximized (); // If triggered to maximize the window
Restoreaction = new qaction (TR ("Restore"), this );
Connect (restoreaction, signal (triggered (), this, slot (shownormal (); // If a restore is triggered, it is displayed normally.
Quitaction = new qaction (TR ("quit"), this );
Connect (quitaction, signal (triggered (), qapp, slot (quit (); // exit the program if the exit is triggered
}
Void window: createtrayicon () // create a tray icon
{
Trayiconmenu = new qmenu (this); // menu
Trayiconmenu-> addaction (minimizeaction); // Add the smallest value to the menu item
Trayiconmenu-> addaction (maximizeaction); // Add the maximum value to the menu item
Trayiconmenu-> addaction (restoreaction); // Add the restore operation to the menu item
// Trayiconmenu-> addseparator (); // Add a separator
Trayiconmenu-> addaction (quitaction); // Add exit to a single menu
Trayicon = new qsystemtrayicon (this); // create a tray icon
Trayicon-> setcontextmenu (trayiconmenu); // you can specify trayiconmenu as the context menu of a tray.
}
If you do not want to display your icon and the program runs properly, you need to set whether it is visible (setEnabled ):
As follows:
Qidget: setEnabled (bool)