From ddd7dc138b863a6f6536c03ac77f02a433f81511 Mon Sep 17 00:00:00 2001 From: Blagovest Petrov Date: Tue, 2 Mar 2021 02:10:43 +0200 Subject: [PATCH] ISSUE-8 System tray is fully rewritten in C++ due to a bug in QT: https://bugreports.qt.io/browse/QTBUG-69226 Signed-off-by: Blagovest Petrov --- TangraPlay.pro | 6 ++++-- main.cpp | 10 ++++++++-- main.qml | 25 ++++++------------------- tangratray.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ tangratray.h | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 23 deletions(-) create mode 100644 tangratray.cpp create mode 100644 tangratray.h diff --git a/TangraPlay.pro b/TangraPlay.pro index 0966277..9ffdd16 100644 --- a/TangraPlay.pro +++ b/TangraPlay.pro @@ -17,7 +17,8 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ fetchshowid.cpp \ - main.cpp + main.cpp \ + tangratray.cpp RESOURCES += qml.qrc @@ -33,4 +34,5 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += \ - fetchshowid.h + fetchshowid.h \ + tangratray.h diff --git a/main.cpp b/main.cpp index 068ac16..d7b90e4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,20 +1,26 @@ #include #include #include +#include +#include "tangratray.h" int main(int argc, char *argv[]) { - QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); - QQmlApplicationEngine engine; + + TangraTray * tangratray = new TangraTray(); + QQmlContext *context = engine.rootContext(); + context->setContextProperty("tangraTray", tangratray); + const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); + engine.load(url); return app.exec(); diff --git a/main.qml b/main.qml index 5b2f0b3..b1bc848 100644 --- a/main.qml +++ b/main.qml @@ -15,31 +15,18 @@ Window { FontLoader { id: mainfont; source: "Resources/KellySlab-Regular.ttf" } - SystemTrayIcon { - visible: true - icon.source: "Resources/tangra.ico" - - menu: Menu { - MenuItem { - text: qsTr("Показване") - onTriggered: { - mainWindow.show() - mainWindow.raise() - mainWindow.requestActivate() - } - } - MenuItem { - text: qsTr("Изключване") - onTriggered: Qt.quit() - } - } + Connections { + target: tangraTray - onActivated: { + function onSignalIconActivated() { mainWindow.show() mainWindow.raise() mainWindow.requestActivate() } + function onSignalQuit(){ + Qt.quit() + } } AboutProgram { diff --git a/tangratray.cpp b/tangratray.cpp new file mode 100644 index 0000000..52de6c3 --- /dev/null +++ b/tangratray.cpp @@ -0,0 +1,47 @@ +#include "tangratray.h" + +TangraTray::TangraTray(QObject *parent) : QObject(parent) +{ + + QMenu *trayIconMenu = new QMenu(); + + QAction * viewWindowAction = new QAction(tr("Показване"), this); + QAction * quitAction = new QAction(tr("Изход"), this); + + connect(viewWindowAction, &QAction::triggered, this, &TangraTray::signalIconActivated); + connect(quitAction, &QAction::triggered, this, &TangraTray::signalQuit); + + trayIconMenu->addAction(viewWindowAction); + trayIconMenu->addAction(quitAction); + + trayIcon = new QSystemTrayIcon(); + trayIcon->setContextMenu(trayIconMenu); + setTrayIcon(":Resources/tangra.ico"); + + trayIcon->show(); + + connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), + this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); +} + +void TangraTray::iconActivated(QSystemTrayIcon::ActivationReason reason) +{ + switch (reason){ + case QSystemTrayIcon::Trigger: + emit signalIconActivated(); + break; + default: + break; + } +} + +void TangraTray::setTrayIcon(QString image) +{ + QIcon icon = QIcon(image), tr("Icon"); + trayIcon->setIcon(icon); +} + +void TangraTray::hideIconTray() +{ + trayIcon->hide(); +} diff --git a/tangratray.h b/tangratray.h new file mode 100644 index 0000000..e2d3e4f --- /dev/null +++ b/tangratray.h @@ -0,0 +1,37 @@ +#ifndef TANGRATRAY_H +#define TANGRATRAY_H + +#include +#include +#include +#include +#include + +class TangraTray : public QObject +{ + Q_OBJECT +public: + explicit TangraTray(QObject *parent = 0); + +signals: + void signalIconActivated(); + void signalShow(); + void signalQuit(); + +private slots: + /* The slot that will accept the signal from the event click on the application icon in the system tray + */ + void iconActivated(QSystemTrayIcon::ActivationReason reason); + +public slots: + void hideIconTray(); + +private: + /* Declare the object of future applications for the tray icon*/ + QSystemTrayIcon * trayIcon; + void setTrayIcon(QString image); + void createTrayIcon(); +}; + +#endif // SYSTEMTRAY_H + -- 2.34.1