Qt 5.10 Book Examples
SystemTray.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // SystemTray.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include "SystemTray.h"
13 
14 // ----------------------------------------------------------------------
15 SystemTray::SystemTray(QWidget* pwgt /*=0*/)
16  : QLabel("<H1>Application Window</H1>", pwgt)
17  , m_bIconSwitcher(false)
18 {
19  setWindowTitle("System Tray");
20 
21  QAction* pactShowHide =
22  new QAction("&Show/Hide Application Window", this);
23 
24  connect(pactShowHide, SIGNAL(triggered()),
25  this, SLOT(slotShowHide())
26  );
27 
28  QAction* pactShowMessage = new QAction("S&how Message", this);
29  connect(pactShowMessage, SIGNAL(triggered()),
30  this, SLOT(slotShowMessage())
31  );
32 
33  QAction* pactChangeIcon = new QAction("&Change Icon", this);
34  connect(pactChangeIcon, SIGNAL(triggered()),
35  this, SLOT(slotChangeIcon())
36  );
37 
38  QAction* pactQuit = new QAction("&Quit", this);
39  connect(pactQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
40 
41  m_ptrayIconMenu = new QMenu(this);
42  m_ptrayIconMenu->addAction(pactShowHide);
43  m_ptrayIconMenu->addAction(pactShowMessage);
44  m_ptrayIconMenu->addAction(pactChangeIcon);
45  m_ptrayIconMenu->addAction(pactQuit);
46 
47  m_ptrayIcon = new QSystemTrayIcon(this);
48  m_ptrayIcon->setContextMenu(m_ptrayIconMenu);
49  m_ptrayIcon->setToolTip("System Tray");
50 
52 
53  m_ptrayIcon->show();
54 }
55 
56 // ----------------------------------------------------------------------
57 /*virtual*/void SystemTray::closeEvent(QCloseEvent*)
58 {
59  if (m_ptrayIcon->isVisible()) {
60  hide();
61  }
62 }
63 
64 // ----------------------------------------------------------------------
66 {
67  setVisible(!isVisible());
68 }
69 
70 // ----------------------------------------------------------------------
72 {
73  m_ptrayIcon->showMessage("For your information",
74  "You have selected the "
75  "\"Show Message!\" option",
76  QSystemTrayIcon::Information,
77  3000
78  );
79 }
80 
81 // ----------------------------------------------------------------------
83 {
84  m_bIconSwitcher = !m_bIconSwitcher;
85  QString strPixmapName = m_bIconSwitcher ? ":/images/img1.bmp"
86  : ":/images/img2.bmp";
87  m_ptrayIcon->setIcon(QPixmap(strPixmapName));
88 
89 }
void slotChangeIcon()
Definition: SystemTray.cpp:82
void slotShowHide()
Definition: SystemTray.cpp:65
SystemTray(QWidget *pwgt=0)
Definition: SystemTray.cpp:15
void slotShowMessage()
Definition: SystemTray.cpp:71
virtual void closeEvent(QCloseEvent *)
Definition: SystemTray.cpp:57