Browse Source

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 <blagovest@petrovs.info>
pull/15/head
Blagovest Petrov 4 years ago
parent
commit
ddd7dc138b
  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 += \
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

10
main.cpp

@ -1,20 +1,26 @@
#include <QGuiApplication>
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#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();

25
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 {

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