1. Add two member variables
QSystemTrayIcon * trayicon;
QMenu * trayiconMenu;
2. Add a slot function to respond to the mouse clicks on the tray icon and tray menu.
Void onSystemTrayIconClicked (QSystemTrayIcon: ActivationReason reason );
3. initialize the two variables added in the constructor.
Trayicon = new QSystemTrayIcon (this );
// Create a QIcon object. The parameter is an icon resource and the value is the address of the mark in the resource file of the project.
QIcon icon ("F:/Windows QT/TrayIcon/bottom.png ");
TrayiconMenu = new QMenu (this );
// Add menu items for the tray menu
TrayiconMenu-> addAction (ui-> action_quit );
// Add a separator to the tray menu
TrayiconMenu-> addSeparator ();
// Use the created QIcon object as the system tray icon
Trayicon-> setIcon (icon );
// Display the tray icon
Trayicon-> show ();
// Set the system tray prompt
Trayicon-> setToolTip (tr ("tray test "));
// Use the create menu as the system tray menu
Trayicon-> setContextMenu (trayiconMenu );
// Display a bubble message on the system tray
Trayicon-> showMessage (tr ("hahaya"), tr ("tray test"), QSystemTrayIcon: Information, 5000 );
// When you bind the system tray to a slot with a click signal, that is, when the icon is activated
Connect (trayicon, SIGNAL (activated (QSystemTrayIcon: ActivationReason), this, SLOT (onSystemTrayIconClicked (QSystemTrayIcon: ActivationReason )));
4. Implement the slot function onSystemTrayIconClicked.
Void MainWindow: onSystemTrayIconClicked (QSystemTrayIcon: ActivationReason reason)
{
Switch (reason)
{
// Click
Case QSystemTrayIcon: Trigger:
// Double-click
Case QSystemTrayIcon: DoubleClick:
// Restore window display
This-> setWindowState (Qt: WindowActive );
This-> show ();
Break;
Default:
Break;
}
}
5. When you click the close button in the window and set the program tray, rewrite the closeEvent function of the class.
Void MainWindow: closeEvent (QCloseEvent * event)
{
If (trayicon-> isVisible ())
{
Hide ();
Event-> ignore ();
}
}
6. Click the slot function in the "exit" menu and click "exit ".
Void MainWindow: on_action_quit_triggered ()
{
This-> hide ();
This-> close ();
}
// Mainwindow. h
# Ifndef MAINWINDOW_H
# Define MAINWINDOW_H
# Include <QMainWindow>
# Include <QSystemTrayIcon>
# Include <QMenu>
# Include <QCloseEvent>
Namespace Ui {
Class MainWindow;
}
Class MainWindow: public QMainWindow
{
Q_OBJECT
Public:
Explicit MainWindow (QWidget * parent = 0 );
~ MainWindow ();
Private:
Ui: MainWindow * ui;
Private slots:
Void on_action_quit_triggered ();
Void onSystemTrayIconClicked (QSystemTrayIcon: ActivationReason reason );
Protected:
Void closeEvent (QCloseEvent * event );
Private:
QSystemTrayIcon * trayicon;
QMenu * trayiconMenu;
};
# Endif // MAINWINDOW_H
// Mainwindow. h
# Ifndef MAINWINDOW_H
# Define MAINWINDOW_H
# Include <QMainWindow>
# Include <QSystemTrayIcon>
# Include <QMenu>
# Include <QCloseEvent>
Namespace Ui {
Class MainWindow;
}
Class MainWindow: public QMainWindow
{
Q_OBJECT
Public:
Explicit MainWindow (QWidget * parent = 0 );
~ MainWindow ();
Private:
Ui: MainWindow * ui;
Private slots:
Void on_action_quit_triggered ();
Void onSystemTrayIconClicked (QSystemTrayIcon: ActivationReason reason );
Protected:
Void closeEvent (QCloseEvent * event );
Private:
QSystemTrayIcon * trayicon;
QMenu * trayiconMenu;
};
# Endif // MAINWINDOW_H
// Mainwindow. cpp
# Include <QtGui>
# Include "mainwindow. h"
# Include "ui_main1_1_h"
MainWindow: MainWindow (QWidget * parent ):
QMainWindow (parent ),
Ui (new Ui: MainWindow)
{
Ui-> setupUi (this );
Trayicon = new QSystemTrayIcon (this );
// Create a QIcon object. The parameter is an icon resource and the value is the address of the mark in the resource file of the project.
QIcon icon ("F:/Windows QT/TrayIcon/bottom.png ");
TrayiconMenu = new QMenu (this );
// Add menu items for the tray menu
TrayiconMenu-> addAction (ui-> action_quit );
// Add a separator to the tray menu
TrayiconMenu-> addSeparator ();
// Use the created QIcon object as the system tray icon
Trayicon-> setIcon (icon );
// Display the tray icon
Trayicon-> show ();
// Set the system tray prompt
Trayicon-> setToolTip (tr ("tray test "));
// Use the create menu as the system tray menu
Trayicon-> setContextMenu (trayiconMenu );
// Display a bubble message on the system tray
Trayicon-> showMessage (tr ("hahaya"), tr ("tray test"), QSystemTrayIcon: Information, 5000 );
// When you bind the system tray to a slot with a click signal, that is, when the icon is activated
Connect (trayicon, SIGNAL (activated (QSystemTrayIcon: ActivationReason), this, SLOT (onSystemTrayIconClicked (QSystemTrayIcon: ActivationReason )));
}
MainWindow ::~ MainWindow ()
{
Delete ui;
}
Void MainWindow: onSystemTrayIconClicked (QSystemTrayIcon: ActivationReason reason)
{
Switch (reason)
{
// Click
Case QSystemTrayIcon: Trigger:
// Double-click
Case QSystemTrayIcon: DoubleClick:
// Restore window display
This-> setWindowState (Qt: WindowActive );
This-> show ();
Break;
Default:
Break;
}
}
Void MainWindow: closeEvent (QCloseEvent * event)
{
If (trayicon-> isVisible ())
{
Hide ();
Event-> ignore ();
}
}
Void MainWindow: on_action_quit_triggered ()
{
This-> hide ();
This-> close ();
}
// Mainwindow. cpp
# Include <QtGui>
# Include "mainwindow. h"
# Include "ui_main1_1_h"
MainWindow: MainWindow (QWidget * parent ):
QMainWindow (parent ),
Ui (new Ui: MainWindow)
{
Ui-> setupUi (this );
Trayicon = new QSystemTrayIcon (this );
// Create a QIcon object. The parameter is an icon resource and the value is the address of the mark in the resource file of the project.
QIcon icon ("F:/Windows QT/TrayIcon/bottom.png ");
TrayiconMenu = new QMenu (this );
// Add menu items for the tray menu
TrayiconMenu-> addAction (ui-> action_quit );
// Add a separator to the tray menu
TrayiconMenu-> addSeparator ();
// Use the created QIcon object as the system tray icon
Trayicon-> setIcon (icon );
// Display the tray icon
Trayicon-> show ();
// Set the system tray prompt
Trayicon-> setToolTip (tr ("tray test "));
// Use the create menu as the system tray menu
Trayicon-> setContextMenu (trayiconMenu );
// Display a bubble message on the system tray
Trayicon-> showMessage (tr ("hahaya"), tr ("tray test"), QSystemTrayIcon: Information, 5000 );
// When you bind the system tray to a slot with a click signal, that is, when the icon is activated
Connect (trayicon, SIGNAL (activated (QSystemTrayIcon: ActivationReason), this, SLOT (onSystemTrayIconClicked (QSystemTrayIcon: ActivationReason )));
}
MainWindow ::~ MainWindow ()
{
Delete ui;
}
Void MainWindow: onSystemTrayIconClicked (QSystemTrayIcon: ActivationReason reason)
{
Switch (reason)
{
// Click
Case QSystemTrayIcon: Trigger:
// Double-click
Case QSystemTrayIcon: DoubleClick:
// Restore window display
This-> setWindowState (Qt: WindowActive );
This-> show ();
Break;
Default:
Break;
}
}
Void MainWindow: closeEvent (QCloseEvent * event)
{
If (trayicon-> isVisible ())
{
Hide ();
Event-> ignore ();
}
}
Void MainWindow: on_action_quit_triggered ()
{
This-> hide ();
This-> close ();
}
// Main. cpp
# Include <QtGui/QApplication>
# Include <QTextCodec>
# Include "mainwindow. h"
Int main (int argc, char * argv [])
{
QTextCodec: setCodecForTr (QTextCodec: codecForLocale ());
QApplication a (argc, argv );
MainWindow w;
W. show ();
Return a.exe c ();
}
// Main. cpp
# Include <QtGui/QApplication>
# Include <QTextCodec>
# Include "mainwindow. h"
Int main (int argc, char * argv [])
{
QTextCodec: setCodecForTr (QTextCodec: codecForLocale ());
QApplication a (argc, argv );
MainWindow w;
W. show ();
Return a.exe c ();
}
From: Code life