The article directories

  • 0 effect
  • 1 implementation
    • In the 1.1.h file, declare variables and methods
    • 1.2 Initializing tray:
    • 1.3 Creating a Tray
    • 1.4 Click Events

0 effect

What is a tray? That is, hide it in the taskbar after minimizing it, as shown in the picture below:

1 implementation

In the 1.1.h file, declare variables and methods

    // Window taskbar properties
      QSystemTrayIcon *trayIcon;
      QMenu *trayMenu ;

      void iconActivated(QSystemTrayIcon::ActivationReason);

       QAction *trayShowMainAction;// Tray display window signal generator
       QAction *trayExitAppAction;// Tray display window signal generator
       void on_showMainAction(a);/ / tray slots
       void on_exitAppAction(a);/ / tray slots
Copy the code

1.2 Initializing tray:

void initTray(a)
{
    // Create the tray
    this->initTrayIcon(a);connect(trayIcon, &QSystemTrayIcon::activated,
                this, &UIMainWindows::iconActivated);
    // The tray event
    // Display the interface
    connect(trayShowMainAction, &QAction::triggered,
            this, &UIMainWindows::showNormal);
     // Exit the program
     connect(trayExitAppAction, &QAction::triggered,
             this, &UIMainWindows::on_exitAppAction);
    //this->setWindowState((this->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
    // Set the window style
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
}
Copy the code

1.3 Creating a Tray

void initTrayIcon(a)
{
    // Check whether the system supports the display of tray ICONS
    if(! QSystemTrayIcon::isSystemTrayAvailable())
     {
            //qDebug() <<"test";
            return;
      }
    // Instantiate the tray icon control
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(":/image/main_menu.png"));  // Set the tray icon display
    trayIcon->setToolTip("Calculator"); // Displays a prompt message

    // Create tray menu
   trayShowMainAction = new QAction(tr("Display main screen"),this);
   trayExitAppAction = new QAction(tr("Quit"),this);
    trayMenu = new QMenu(this);
    trayMenu ->addAction(trayShowMainAction);// Add events
    trayMenu ->addSeparator(a);// Add a divider
    trayMenu ->addAction(trayExitAppAction);
    trayIcon->setContextMenu(trayMenu );// Tray add menu

}
Copy the code

1.4 Click Events

void iconActivated(QSystemTrayIcon::ActivationReason reason)
{
        switch (reason)
        {
        case QSystemTrayIcon::Trigger:
            //trayIcon->showMessage("title"," you click "); // The last two default parameters
            trayIcon->showMessage(tr("Game platform"),
                                      tr("Welcome to this program!"),
                                      QSystemTrayIcon::Information,
                                      700);
            break;
        case QSystemTrayIcon::DoubleClick:
            //trayIcon->showMessage("title"," you double click ");
            //setVisible(true);
            this->show(a);break;
/ / case QSystemTrayIcon: : MiddleClick: / / mouse wheel in the middle
// trayIcon->showMessage("title"," you are in the middle ");
// break;
        default:
            break; }}Copy the code
void on_exitAppAction(a)
{
    exit(0);
}
Copy the code