System Tray Icon Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"window.h"
#ifndef QT_NO_SYSTEMTRAYICON
#include <QAction>
#include <QCheckBox>
#include <QComboBox>
#include <QCoreApplication>
#include <QCloseEvent>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QMenu>
#include <QPushButton>
#include <QSpinBox>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QMessageBox>
Window::
Window()
{
createIconGroupBox();
createMessageGroupBox();
iconLabel-&
gt;setMinimumWidth(durationLabel-&
gt;sizeHint().width());
createActions();
createTrayIcon();
connect(showMessageButton, &
amp;QAbstractButton::
clicked, this
, &
amp;Window::
showMessage);
connect(showIconCheckBox, &
amp;QAbstractButton::
toggled, trayIcon, &
amp;QSystemTrayIcon::
setVisible);
connect(iconComboBox, QOverload&
lt;int
&
gt;::
of(&
amp;QComboBox::
currentIndexChanged),
this
, &
amp;Window::
setIcon);
connect(trayIcon, &
amp;QSystemTrayIcon::
messageClicked, this
, &
amp;Window::
messageClicked);
connect(trayIcon, &
amp;QSystemTrayIcon::
activated, this
, &
amp;Window::
iconActivated);
QVBoxLayout *
mainLayout =
new
QVBoxLayout;
mainLayout-&
gt;addWidget(iconGroupBox);
mainLayout-&
gt;addWidget(messageGroupBox);
setLayout(mainLayout);
iconComboBox-&
gt;setCurrentIndex(1
);
trayIcon-&
gt;show();
setWindowTitle(tr("Systray"
));
resize(400
, 300
);
}
void
Window::
setVisible(bool
visible)
{
minimizeAction-&
gt;setEnabled(visible);
maximizeAction-&
gt;setEnabled(!
isMaximized());
restoreAction-&
gt;setEnabled(isMaximized() ||
!
visible);
QDialog::
setVisible(visible);
}
void
Window::
closeEvent(QCloseEvent *
event)
{
#ifdef Q_OS_OSX
if
(!
event-&
gt;spontaneous() ||
!
isVisible()) {
return
;
}
#endif
if
(trayIcon-&
gt;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-&
gt;ignore();
}
}
void
Window::
setIcon(int
index)
{
QIcon icon =
iconComboBox-&
gt;itemIcon(index);
trayIcon-&
gt;setIcon(icon);
setWindowIcon(icon);
trayIcon-&
gt;setToolTip(iconComboBox-&
gt;itemText(index));
}
void
Window::
iconActivated(QSystemTrayIcon::
ActivationReason reason)
{
switch
(reason) {
case
QSystemTrayIcon::
Trigger:
case
QSystemTrayIcon::
DoubleClick:
iconComboBox-&
gt;setCurrentIndex((iconComboBox-&
gt;currentIndex() +
1
) %
iconComboBox-&
gt;count());
break
;
case
QSystemTrayIcon::
MiddleClick:
showMessage();
break
;
default
:
;
}
}
void
Window::
showMessage()
{
showIconCheckBox-&
gt;setChecked(true
);
int
selectedIcon =
typeComboBox-&
gt;itemData(typeComboBox-&
gt;currentIndex()).toInt();
QSystemTrayIcon::
MessageIcon msgIcon =
QSystemTrayIcon::
MessageIcon(selectedIcon);
if
(selectedIcon ==
-
1
) {
// custom icon
QIcon icon(iconComboBox-&
gt;itemIcon(iconComboBox-&
gt;currentIndex()));
trayIcon-&
gt;showMessage(titleEdit-&
gt;text(), bodyEdit-&
gt;toPlainText(), icon,
durationSpinBox-&
gt;value() *
1000
);
}
else
{
trayIcon-&
gt;showMessage(titleEdit-&
gt;text(), bodyEdit-&
gt;toPlainText(), msgIcon,
durationSpinBox-&
gt;value() *
1000
);
}
}
void
Window::
messageClicked()
{
QMessageBox::
information(0
, tr("Systray"
),
tr("Sorry, I already gave what help I could.
\n
"
"Maybe you should try asking a human?"
));
}
void
Window::
createIconGroupBox()
{
iconGroupBox =
new
QGroupBox(tr("Tray Icon"
));
iconLabel =
new
QLabel("Icon:"
);
iconComboBox =
new
QComboBox;
iconComboBox-&
gt;addItem(QIcon(":/images/bad.png"
), tr("Bad"
));
iconComboBox-&
gt;addItem(QIcon(":/images/heart.png"
), tr("Heart"
));
iconComboBox-&
gt;addItem(QIcon(":/images/trash.png"
), tr("Trash"
));
showIconCheckBox =
new
QCheckBox(tr("Show icon"
));
showIconCheckBox-&
gt;setChecked(true
);
QHBoxLayout *
iconLayout =
new
QHBoxLayout;
iconLayout-&
gt;addWidget(iconLabel);
iconLayout-&
gt;addWidget(iconComboBox);
iconLayout-&
gt;addStretch();
iconLayout-&
gt;addWidget(showIconCheckBox);
iconGroupBox-&
gt;setLayout(iconLayout);
}
void
Window::
createMessageGroupBox()
{
messageGroupBox =
new
QGroupBox(tr("Balloon Message"
));
typeLabel =
new
QLabel(tr("Type:"
));
typeComboBox =
new
QComboBox;
typeComboBox-&
gt;addItem(tr("None"
), QSystemTrayIcon::
NoIcon);
typeComboBox-&
gt;addItem(style()-&
gt;standardIcon(
QStyle::
SP_MessageBoxInformation), tr("Information"
),
QSystemTrayIcon::
Information);
typeComboBox-&
gt;addItem(style()-&
gt;standardIcon(
QStyle::
SP_MessageBoxWarning), tr("Warning"
),
QSystemTrayIcon::
Warning);
typeComboBox-&
gt;addItem(style()-&
gt;standardIcon(
QStyle::
SP_MessageBoxCritical), tr("Critical"
),
QSystemTrayIcon::
Critical);
typeComboBox-&
gt;addItem(QIcon(), tr("Custom icon"
),
-
1
);
typeComboBox-&
gt;setCurrentIndex(1
);
durationLabel =
new
QLabel(tr("Duration:"
));
durationSpinBox =
new
QSpinBox;
durationSpinBox-&
gt;setRange(5
, 60
);
durationSpinBox-&
gt;setSuffix(" s"
);
durationSpinBox-&
gt;setValue(15
);
durationWarningLabel =
new
QLabel(tr("(some systems might ignore this "
"hint)"
));
durationWarningLabel-&
gt;setIndent(10
);
titleLabel =
new
QLabel(tr("Title:"
));
titleEdit =
new
QLineEdit(tr("Cannot connect to network"
));
bodyLabel =
new
QLabel(tr("Body:"
));
bodyEdit =
new
QTextEdit;
bodyEdit-&
gt;setPlainText(tr("Don't believe me. Honestly, I don't have a "
"clue.
\n
Click this balloon for details."
));
showMessageButton =
new
QPushButton(tr("Show Message"
));
showMessageButton-&
gt;setDefault(true
);
QGridLayout *
messageLayout =
new
QGridLayout;
messageLayout-&
gt;addWidget(typeLabel, 0
, 0
);
messageLayout-&
gt;addWidget(typeComboBox, 0
, 1
, 1
, 2
);
messageLayout-&
gt;addWidget(durationLabel, 1
, 0
);
messageLayout-&
gt;addWidget(durationSpinBox, 1
, 1
);
messageLayout-&
gt;addWidget(durationWarningLabel, 1
, 2
, 1
, 3
);
messageLayout-&
gt;addWidget(titleLabel, 2
, 0
);
messageLayout-&
gt;addWidget(titleEdit, 2
, 1
, 1
, 4
);
messageLayout-&
gt;addWidget(bodyLabel, 3
, 0
);
messageLayout-&
gt;addWidget(bodyEdit, 3
, 1
, 2
, 4
);
messageLayout-&
gt;addWidget(showMessageButton, 5
, 4
);
messageLayout-&
gt;setColumnStretch(3
, 1
);
messageLayout-&
gt;setRowStretch(4
, 1
);
messageGroupBox-&
gt;setLayout(messageLayout);
}
void
Window::
createActions()
{
minimizeAction =
new
QAction(tr("Mi&nimize"
), this
);
connect(minimizeAction, &
amp;QAction::
triggered, this
, &
amp;QWidget::
hide);
maximizeAction =
new
QAction(tr("Ma&ximize"
), this
);
connect(maximizeAction, &
amp;QAction::
triggered, this
, &
amp;QWidget::
showMaximized);
restoreAction =
new
QAction(tr("&Restore"
), this
);
connect(restoreAction, &
amp;QAction::
triggered, this
, &
amp;QWidget::
showNormal);
quitAction =
new
QAction(tr("&Quit"
), this
);
connect(quitAction, &
amp;QAction::
triggered, qApp, &
amp;QCoreApplication::
quit);
}
void
Window::
createTrayIcon()
{
trayIconMenu =
new
QMenu(this
);
trayIconMenu-&
gt;addAction(minimizeAction);
trayIconMenu-&
gt;addAction(maximizeAction);
trayIconMenu-&
gt;addAction(restoreAction);
trayIconMenu-&
gt;addSeparator();
trayIconMenu-&
gt;addAction(quitAction);
trayIcon =
new
QSystemTrayIcon(this
);
trayIcon-&
gt;setContextMenu(trayIconMenu);
}
#endif