QT Implementation system Tray

Source: Internet
Author: User

Usually most of the software you see is with the system tray, so the software can run in the background, very good. Today to see the next Qt Demon, the middle has reference to the next, the realization of this small function, will add this feature to their own before a project, the effect is fine. The interface looks like this:


Right-clicking on the System tray displays the menu functions currently set:

How, the effect is OK. To achieve this small function hall satisfied. Right-click on the popup menu to set the desired settings yourself. You can also set the actions that are triggered when the mouse clicks, double-clicks, and middle keys are pressed separately. The main code is as follows:

1, first need to add a header file, Qsystem/trayicon class, Qmenu class and qaction three classes

<span style= "FONT-SIZE:18PX;" ><strong>    Qsystemtrayicon *mytrayicon;                                  System tray pointer    qmenu *mymenu;                                                System tray Menu    qaction *minisizeaction;                                      Minimization of    qaction *maxsizeaction;                                       maximizing    qaction *restorewinaction;                                    Restore    qaction *quitaction;                                          Exit    qaction *checkupdate;                                         Check for Updates </strong></span>

function function Realization:

<span style= "FONT-SIZE:18PX;" ><strong>/* * Function: Create system tray Menu * Input parameters: None * Output parameters: None * Description: This function is used to create a menu for the system tray, providing the user to click on the corresponding menu to operate */void MainWindow::    Createtraymenu () {minisizeaction = new qaction (tr ("Minimized (&n)"), this);    Minisizeaction->settooltip (tr ("minimized"));    Minisizeaction->seticon (Qicon (":/images/systemtrayico/minac.png"));    Maxsizeaction = new Qaction (tr ("Maximized (&x)"), this);    Maxsizeaction->settooltip (TR ("maximized"));    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 ("Exit (&AMP;Q)"), this);    Quitaction->settooltip (tr ("exit"));    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 "));    The various menus are connected by slots to 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 (Logoutwidget ()));    Connect (checkupdate,signal (triggered ()), This,slot (Checksysupdate ()));    Add the Created menu object to the system tray menu MyMenu = new Qmenu ((qwidget*) qapplication::d esktop ());    Mymenu->addaction (minisizeaction);    Mymenu->addaction (maxsizeaction);    Mymenu->addaction (restorewinaction);    Mymenu->addseparator ();    Mymenu->addaction (checkupdate);                 Mymenu->addseparator (); Add a delimiter mymenu->addaction (quitaction);}    /* Function: Create the System icon when the software is minimized and display some information * Description: The system supports the system tray icon effect to make a judgment, support to perform the corresponding action */void Mainwindow::createtrayicon () {    Createtraymenu (); Determine if 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 cashier System V2.0"));    Mytrayicon->showmessage ("VW Software", "software entered the minimized mode, if you want to restore, click to Restore", qsystemtrayicon::information,10000);    Mytrayicon->setcontextmenu (MyMenu);    Mytrayicon->show (); Connect (mytrayicon,signal (activated (Qsystemtrayicon::activationreason)), This,slot (iconacitvated ( Qsystemtrayicon::activationreason)));} </strong></span>

Mouse click event Handling:

<span style= "FONT-SIZE:18PX;" ><strong>/* * Function: Perform mouse click on the system tray menu after the corresponding action * Description: When the mouse click on the menu to trigger the action *     (left, right) Click, double, middle button. These actions can be handled separately for the corresponding event */void mainwindow::iconacitvated (Qsystemtrayicon::activationreason reason) {    switch (reason)    {case    Qsystemtrayicon::trigger:        this->shownormal ();        break;    Case Qsystemtrayicon::D oubleclick:        this->showmaximized ();        break;    Case Qsystemtrayicon::middleclick:        //Break        ;    Default: Break        ;    }} </strong></span>

That's the basic code.

Need the code can leave the mailbox, send you ...

Technology lies in communication ...



QT Implementation system Tray

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.