Is it possible to implement SystemTrayIcon functionality in a Qt Quick application

I am writing a QtQuick desktop application. I use both C ++ (for functionality) and QML (for UI). I am using QQuickView to display an interface written in QML.

I want this application to be in the system tray while minimizing.

I mean functionality similar to this example. http://qt-project.org/doc/qt-4.8/desktop-systray.html I am trying to implement this function, but could not find a way to do this in my Qt Quick application.

Here is my main.cpp code:

#include <QGuiApplication> #include <QQmlEngine> #include <QQmlContext> #include <QQmlFileSelector> #include <QQuickView> #include "myapp.h" int main(int argc, char* argv[]) { QGuiApplication app(argc,argv); app.setApplicationName(QFileInfo(app.applicationFilePath()).baseName()); QDir::setCurrent(qApp->applicationDirPath()); MyApp myappObject; QQuickView view; view.connect(view.engine(), SIGNAL(quit()), &app, SLOT(quit())); view.rootContext()->setContextProperty("myappObject", &myappObject); new QQmlFileSelector(view.engine(), &view); view.setSource(QUrl("qrc:///myapp.qml")); view.setResizeMode(QQuickView::SizeRootObjectToView); view.show(); return app.exec(); } 

Please help by providing any hints / pointers.

Thanks.

+6
source share
2 answers

Today I ran into the same problem and within main () I used the following solution. Great for using Qt 5.3. You must, of course, implement the best way to check if the first root object is your application window object or not.

 #include <QApplication> #include <QQmlApplicationEngine> #include <QMessageBox> #include <QAction> #include <QMenu> #include <QSystemTrayIcon> int main(int argc, char *argv[]) { QApplication app(argc, argv); if (!QSystemTrayIcon::isSystemTrayAvailable()) { QMessageBox::critical(0, QObject::tr("Systray"), QObject::tr("I couldn't detect any system tray " "on this system.")); return 1; } QApplication::setQuitOnLastWindowClosed(false); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); QObject *root = 0; if (engine.rootObjects().size() > 0) { root = engine.rootObjects().at(0); QAction *minimizeAction = new QAction(QObject::tr("Mi&nimize"), root); root->connect(minimizeAction, SIGNAL(triggered()), root, SLOT(hide())); QAction *maximizeAction = new QAction(QObject::tr("Ma&ximize"), root); root->connect(maximizeAction, SIGNAL(triggered()), root, SLOT(showMaximized())); QAction *restoreAction = new QAction(QObject::tr("&Restore"), root); root->connect(restoreAction, SIGNAL(triggered()), root, SLOT(showNormal())); QAction *quitAction = new QAction(QObject::tr("&Quit"), root); root->connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); QMenu *trayIconMenu = new QMenu(); trayIconMenu->addAction(minimizeAction); trayIconMenu->addAction(maximizeAction); trayIconMenu->addAction(restoreAction); trayIconMenu->addSeparator(); trayIconMenu->addAction(quitAction); QSystemTrayIcon *trayIcon = new QSystemTrayIcon(root); trayIcon->setContextMenu(trayIconMenu); trayIcon->setIcon(QIcon(":/resources/DatagnanLogoColor.png")); trayIcon->show(); } return app.exec(); } 
+12
source

Copy the Windos class (window.cpp / window.h) from the systray example into your project, if necessary, put it in Qt5 and open both from the main file:

 int main(int argc, char* argv[]) { // ... QQuickView view; // ... view.show(); Window window; window.show(); return app.exec(); } 
-2
source

Source: https://habr.com/ru/post/971367/


All Articles