ISSUE-8 System tray is fully rewritten in C++ due to a bug in QT: https://bugreports.qt.io/browse/QTBUG-69226 #15

Merged
blago merged 1 commits from ISSUE-8 into master 4 years ago
  1. 6
      TangraPlay.pro
  2. 10
      main.cpp
  3. 25
      main.qml
  4. 47
      tangratray.cpp
  5. 37
      tangratray.h

6
TangraPlay.pro

@ -17,7 +17,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \ SOURCES += \
fetchshowid.cpp \ fetchshowid.cpp \
main.cpp main.cpp \
tangratray.cpp
RESOURCES += qml.qrc RESOURCES += qml.qrc
@ -33,4 +34,5 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
HEADERS += \ HEADERS += \
fetchshowid.h fetchshowid.h \
tangratray.h

10
main.cpp

@ -1,20 +1,26 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QApplication> #include <QApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlContext>
#include "tangratray.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv); QApplication app(argc, argv);
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
TangraTray * tangratray = new TangraTray();
QQmlContext *context = engine.rootContext();
context->setContextProperty("tangraTray", tangratray);
const QUrl url(QStringLiteral("qrc:/main.qml")); const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) { &app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl) if (!obj && url == objUrl)
QCoreApplication::exit(-1); QCoreApplication::exit(-1);
}, Qt::QueuedConnection); }, Qt::QueuedConnection);
engine.load(url); engine.load(url);
return app.exec(); return app.exec();

25
main.qml

@ -15,31 +15,18 @@ Window {
FontLoader { id: mainfont; source: "Resources/KellySlab-Regular.ttf" } FontLoader { id: mainfont; source: "Resources/KellySlab-Regular.ttf" }
SystemTrayIcon { Connections {
visible: true target: tangraTray
icon.source: "Resources/tangra.ico"
menu: Menu {
MenuItem {
text: qsTr("Показване")
onTriggered: {
mainWindow.show()
mainWindow.raise()
mainWindow.requestActivate()
}
}
MenuItem {
text: qsTr("Изключване")
onTriggered: Qt.quit()
}
}
onActivated: { function onSignalIconActivated() {
mainWindow.show() mainWindow.show()
mainWindow.raise() mainWindow.raise()
mainWindow.requestActivate() mainWindow.requestActivate()
} }
function onSignalQuit(){
Qt.quit()
}
} }
AboutProgram { AboutProgram {

47
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();
}

37
tangratray.h

@ -0,0 +1,37 @@
#ifndef TANGRATRAY_H
#define TANGRATRAY_H
#include <QObject>
#include <QAction>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QApplication>
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
Loading…
Cancel
Save