Qt implements system pallets and qt implements pallets

Source: Internet
Author: User

Qt implements system pallets and qt implements pallets

Most of the software we see usually comes with a system tray, so that the software can run in the background, which is quite good. Today, I checked the demon that comes with Qt. I have a reference in the middle. After implementing this small function, I added this function to my previous project. The effect is quite good. The interface is as follows:


Right-click the system tray to display the menu functions currently set:

How about it? The effect is fine. We are satisfied with the implementation of this small function hall. You can right-click the pop-up menu to set what you need. In addition, you can set the actions triggered when you click, double-click, and press the intermediate key respectively. The main code is as follows:

1. First, add the header file, including the Qsystem/trayIcon class, QMenu class, And QAction class.

<Span style = "font-size: 18px;"> <strong> QSystemTrayIcon * myTrayIcon; // System Tray pointer QMenu * myMenu; // System Tray menu QAction * miniSizeAction; // minimize QAction * maxSizeAction; // maximize QAction * restoreWinAction; // restore QAction * quitAction; // exit QAction * checkUpdate; // check for updates </strong> </span>

Function implementation:

<Span style = "font-size: 18px;"> <strong>/** function: Create a system tray menu * input parameter: none * output parameter: none * description: this function is used to create a system tray menu. Users can click the corresponding menu to perform operations */void MainWindow: CreateTrayMenu () {miniSizeAction = new QAction (tr ("minimal (& N) "), this); miniSizeAction-> setToolTip (tr (" minimal "); miniSizeAction-> setIcon (QIcon (":/images/systemtrayico/minac.png ")); maxSizeAction = new QAction (tr ("maximize (& X)"), this); maxSizeAction-> setToolTip (tr ("maximize"); maxSizeAction-> SetIcon (QIcon (":/images/systemtrayico/maxac.png"); restoreWinAction = new QAction (tr ("Restore (& R)"), this ); restoreWinAction-> setToolTip (tr ("Restore"); restoreWinAction-> setIcon (QIcon (":/images/systemtrayico/restoreac.png ")); quitAction = new QAction (tr ("Quit (& Q)"), this); quitAction-> setToolTip (tr ("quit ")); quitAction-> setIcon (QIcon (":/images/systemtrayico/exitac.png"); checkUpdate = new QAction (tr ("check for updates (& C) "), This); checkUpdate-> setToolTip (tr (" check for updates "); checkUpdate-> setIcon (QIcon (":/images/systemtrayico/updateac.png ")); // connect the menus to the SLOT and trigger the corresponding action connect (miniSizeAction, SIGNAL (triggered (), this, SLOT (hide (); connect (maxSizeAction, SIGNAL (triggered (), this, SLOT (showMaximized (); connect (restoreWinAction, SIGNAL (triggered (), this, SLOT (showNormal ())); connect (quitAction, SIGNAL (triggered (), this, SLOT (logoutWidg Et (); connect (checkUpdate, SIGNAL (triggered (), this, SLOT (checkSysUpdate ())); // Add the created menu object to the menu of the system tray. myMenu = new QMenu (QWidget *) QApplication: desktop (); myMenu-> addAction (miniSizeAction ); myMenu-> addAction (maxSizeAction); myMenu-> addAction (restoreWinAction); myMenu-> addSeparator (); myMenu-> addAction (checkUpdate); myMenu-> addSeparator (); // Add a separator myMenu-> addAction (quitAction);}/** function: System icons and display of some information * Description: determines whether the system supports system graphic disk icon special effects. You can execute corresponding actions */void MainWindow: CreateTrayIcon () {CreateTrayMenu (); // determines whether the system supports the system tray icon if (! QSystemTrayIcon: isSystemTrayAvailable () {return;} myTrayIcon = new QSystemTrayIcon (this); myTrayIcon-> setIcon (QIcon (":/images/main.png ")); myTrayIcon-> setToolTip (tr ("Volkswagen supermarket cash register system V2.0"); myTrayIcon-> showMessage ("Volkswagen software", "the software enters the minimization mode, click "Restore", QSystemTrayIcon: Information, 10000); myTrayIcon-> setContextMenu (myMenu); myTrayIcon-> show (); connect (myTrayIcon, SIGNAL (activated (QSystemTrayIcon :: activationReason), this, SLOT (iconAcitvated (QSystemTrayIcon: ActivationReason); }</strong> </span>

Click Event processing:

<Span style = "font-size: 18px;"> <strong>/** function: Click the corresponding action on the system tray menu. * description: when you click the action * (left and right) triggered by clicking the menu, click, double-click, and intermediate button. You can process the corresponding events */void MainWindow: iconAcitvated (QSystemTrayIcon: ActivationReason reason) {switch (reason) {case QSystemTrayIcon: Trigger: this-> showNormal (); break; case QSystemTrayIcon: DoubleClick: this-> showMaximized (); break; case QSystemTrayIcon: MiddleClick: // break; default: break ;}} </strong> </span>

This is the basic code.

If you need code, you can leave a mailbox and send it to you ......

Technology lies in communication ......



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.