Qt implementation system Tray, tray menu, tray message

Source: Internet
Author: User

Qt implementation system Tray, tray menu, tray messagesystem Tray is running a program in the lower right corner of the computer small icon, I believe we are very familiar. QT provides a tray for the Qsystemtrayicon class to implement the program. QT provides a sample tray, under the examples folder, you can do their own research. Here is an example of my own implementation, the main implementation of the function:1. Tray Display, tray icon added; 2. Pallet activation event distribution;
3. Tray menu;
4. Tray message, click on the message to display the main window.
to display the tray icon, you first need to create aThe Qsystemtrayicon object. You can set some information for this object, such as the ToolTip, the label. You need to call show to display the tray.
M_systemtray = new Qsystemtrayicon (this), M_systemtray->seticon (Qicon (":/systemtray/resources/ico.png"); m_ Systemtray->settooltip ("Systemtray program"); M_systemtray->show ();
To add a picture resource to a program, you need to edit the. qrc file to add the path where the picture is located:
<RCC>    <qresource prefix= "Systemtray" >      <file>Resources/ico.png</file>    </ Qresource></rcc>
Effect Display:
Tray activation event, including click, double click, right button, etc. Qsystemtrayicon provides a signal for us to use, the parameter of the signal is the type of activation, we can connect our own slot function to branch the type, to do the corresponding operation:
Constantvaluedescriptionqsystemtrayicon::unknown0unknown reasonqsystemtrayicon::context1the context menu for the System Tray entry was Requestedqsystemtrayicon::D oubleclick2the system Tray entry was double Clickedqsystemtrayicon:: Trigger3the System Tray entry was clickedqsystemtrayicon::middleclick4the system tray entry is clicked with the middle Mo Use button
To connect our own slot functions:
Connect (M_systemtray, &qsystemtrayicon::activated, this, &systemtray::activetray);//Click on the tray to perform the corresponding action
In the slot function, different functions are called, and different operations are performed:
void Systemtray::activetray (Qsystemtrayicon::activationreason reason) {switch (reason) {case Qsystemtrayicon:: Context:showmenu (); Break;case Qsystemtrayicon::D Oubleclick:showwindow (); Break;case Qsystemtrayicon::trigger: ShowMessage (); break;}}
To implement the menu, we need to create a qmenu and create the qaction that we need, add it to Qmenu, and set the Qmenu to Qsystemtrayicon. We can connect different slot functions to each qaction and perform different operations.
M_menu = new Qmenu (this), M_action1 = new Qaction (m_menu); m_action2 = new Qaction (m_menu); M_action1->settext ("Show Window "), M_action2->settext (" Show Message "), M_menu->addaction (M_action1); m_menu->addaction (M_action2); Connect (M_action1, &qaction::triggered, this, &systemtray::showwindow); Connect (M_action2, &qaction:: Triggered, this, &systemtray::showmessage); M_systemtray->setcontextmenu (M_menu);
Menu Effect:
message display is also very simple,Qsystemtrayicon provides the ShowMessage function to display the message window, this function has four parameters, the following:
void Systemtray::showmessage () {m_systemtray->showmessage ("information",//message window title "There is a new message!",// Message Content qsystemtrayicon::messageicon::information,//message window icon 5000);//message window display duration}
Message effects:
Qsystemtrayicon provides a messageclicked signal, Click on the message, can trigger our own connected slot function, perform the action we want to implement:
Connect (M_systemtray, &qsystemtrayicon::messageclicked, this, &systemtray::showwindow);//Click the message box to display the main window
The main window displays:
at the beginning of the program the main interface is hidden, double-click the tray to display the main window.
Full code:
#ifndef systemtray_h#define systemtray_h#include <QtWidgets/QWidget> #include <QSystemTrayIcon> #include <qmenu>class systemtray:public qwidget{q_objectpublic:systemtray (qwidget *parent = 0); ~SystemTray ();p rotected : void Activetray (Qsystemtrayicon::activationreason reason); void ShowWindow ();//Show Form void ShowMessage ();//message box void ShowMenu ();//Display menu Private:qsystemtrayicon *m_systemtray; Qmenu *m_menu; Qaction *m_action1; Qaction *m_action2;}; #endif//Systemtray_h
#include "Systemtray.h" Systemtray::systemtray (Qwidget *parent): Qwidget (parent) {M_systemtray = new Qsystemtrayicon ( This), M_menu = new Qmenu (this), M_action1 = new Qaction (m_menu); m_action2 = new Qaction (m_menu); M_action1->settext (" Show window "); M_action2->settext (" Show Message "); M_menu->addaction (M_action1); M_menu->addaction (m_ Action2); Connect (M_action1, &qaction::triggered, this, &systemtray::showwindow); Connect (M_action2, & Qaction::triggered, this, &systemtray::showmessage); M_systemtray->setcontextmenu (m_menu); m_systemTray- >seticon (Qicon (":/systemtray/resources/ico.png")) M_systemtray->settooltip ("Systemtray program"); m_ Systemtray->show (); Connect (M_systemtray, &qsystemtrayicon::activated, this, &systemtray::activetray); /Click on the tray to perform the corresponding action connect (M_systemtray, &qsystemtrayicon::messageclicked, this, &systemtray::showwindow);// Click the message box to display the main window}systemtray::~systemtray () {}void Systemtray::activetray (Qsystemtrayicon::activationreason reason) {SWItch (reason) {case qsystemtrayicon::context:showmenu (), break;case Qsystemtrayicon::D Oubleclick:showwindow (); break ; case Qsystemtrayicon::trigger:showmessage (); break;}} void Systemtray::showmenu () {m_menu->show ();} void Systemtray::showwindow () {this->show ();} void Systemtray::showmessage () {m_systemtray->showmessage ("information",//message window title "There is a new message!",// Message Content qsystemtrayicon::messageicon::information,//message window icon 5000);//message window display duration}
#include "Systemtray.h" #include <qtwidgets/qapplication>int main (int argc, char *argv[]) {qapplication A (argc, ARGV); Systemtray w;//w.show (); W.hide ();//initialization, main window hidden, easy to observe the effect return a.exec ();}

<RCC>    <qresource prefix= "Systemtray" >      <file>Resources/ico.png</file>    </ Qresource></rcc>
AC qq:1245178753This address: http://blog.csdn.net/u011417605/article/details/51322997source Download: http://download.csdn.net/detail/u011417605/9510891

Qt implementation system Tray, tray menu, tray message

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.