Shawn Rutledge 82cba0ca5c systray example: if there is no system tray, allow waiting for one
Demonstrate that QSystemTrayIcon can wait for a tray to become
available. For example QDBusTrayIcon::init() connects to
QDBusServiceWatcher::serviceRegistered to detect the StatusNotifier
service becoming available. So instead of unconditionally quitting
if there is no tray, allow the user to choose to "Ignore" its absence,
or "Close" the application. Realistically, applications in which a tray
icon is an optional feature should not quit just because there's no
tray.

Task-number: QTBUG-94871
Change-Id: Ia8efd95fcfb9ff7c915ee8e259e9a0903fa7bcb3
Pick-to: 6.8 6.7 6.5
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Reviewed-by: Ilya Fedin <fedin-ilja2010@ya.ru>
Reviewed-by: Dmitry Shachnev <mitya57@gmail.com>
2024-06-05 01:40:10 +00:00

50 lines
1.1 KiB
C++

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QApplication>
#ifndef QT_NO_SYSTEMTRAYICON
#include <QMessageBox>
#include "window.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
auto choice = QMessageBox::critical(nullptr, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray on this system."),
QMessageBox::Close | QMessageBox::Ignore);
if (choice == QMessageBox::Close)
return 1;
// Otherwise "lurk": if a system tray is started later, the icon will appear.
}
QApplication::setQuitOnLastWindowClosed(false);
Window window;
window.show();
return app.exec();
}
#else
#include <QLabel>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString text("QSystemTrayIcon is not supported on this platform");
QLabel *label = new QLabel(text);
label->setWordWrap(true);
label->show();
qDebug() << text;
app.exec();
}
#endif