|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
|
int main(int argc, char *argv[])
{
// Initializes the resources specified by the .qrc file with the specified base name. Normally, when resources are built as part of the application, the resources are loaded automatically at startup.
// The Q_INIT_RESOURCE() macro is necessary on some platforms for resources stored in a static library.
Q_INIT_RESOURCE(systray);
QApplication app(argc, argv);
// Returns true if the system tray is available; otherwise returns false.
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
QMessageBox::critical(0, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray on this system."));
return 1;
}
QApplication::setQuitOnLastWindowClosed(false);
Window window;
window.show();
return app.exec();
}
Window::Window()
{
// Tray Icon Group Box(采用HBox布局)
createIconGroupBox();
// Ballon Message Group Box(采用Grid布局)
createMessageGroupBox();
iconLabel->setMinimumWidth(durationLabel->sizeHint().width());
// Tray right button menu
createActions();
// Create Tray Icon
createTrayIcon();
// 信号与槽的连接
connect(showMessageButton, &QAbstractButton::clicked,
this, &Window::showMessage);
connect(showIconCheckBox, &QAbstractButton::toggled,
trayIcon, &QSystemTrayIcon::setVisible);
connect(iconComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &Window::setIcon);
// Tray 信号槽
connect(trayIcon, &QSystemTrayIcon::messageClicked,
this, &Window::messageClicked);
connect(trayIcon, &QSystemTrayIcon::activated,
this, &Window::iconActivated);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(iconGroupBox);
mainLayout->addWidget(messageGroupBox);
setLayout(mainLayout);
iconComboBox->setCurrentIndex(1);
trayIcon->show();
setWindowTitle(tr("Systray"));
resize(400, 300);
}
void Window::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}
// 重写closeEvent实现关闭时隐藏到托盘
void Window::closeEvent(QCloseEvent *event)
{
#ifdef Q_OS_OSX
if (!event->spontaneous() || !isVisible())
{
return;
}
#endif
if (trayIcon->isVisible())
{
QMessageBox::information(this, tr("Systray"),
tr("The program will keep running in the "
"system tray. To terminate the program, "
"choose <b>Quit</b> in the context menu "
"of the system tray entry."));
hide();
event->ignore();
}
}
// 重写setVisible实现右键菜单的动态变化
void Window::setVisible(bool visible)
{
minimizeAction->setEnabled(visible);
maximizeAction->setEnabled(!isMaximized());
restoreAction->setEnabled(isMaximized() || !visible);
QDialog::setVisible(visible);
}
// 单击或双击托盘图标动态改变图标,点击滚轮显示气泡信息
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) %
iconComboBox->count());
break;
case QSystemTrayIcon::MiddleClick:
showMessage();
break;
default:
break;
}
}
void Window::showMessage()
{
showIconCheckBox->setChecked(true);
QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(typeComboBox->itemData(typeComboBox->currentIndex()).toInt());
if (msgIcon == QSystemTrayIcon::NoIcon)
{
QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex()));
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon, durationSpinBox->value() * 1000);
}
else
{
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon, durationSpinBox->value() * 1000);
}
}
|