Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to implement SystemTrayIcon functionality in Qt Quick application

Tags:

qt

qt5

qml

qtquick2

I am writing a QtQuick desktop application. I use both c++ (for functionality) and QML (for UI) in it. I use QQuickView to show the interface written in QML.

I want this application to reside in System Tray when minimised.

I mean a functionality similar to this example. http://qt-project.org/doc/qt-4.8/desktop-systray.html . I am trying to implement this feature 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 hint/pointers to do this.

Thanks.

like image 849
Mugunth Avatar asked Jun 26 '14 06:06

Mugunth


1 Answers

I was facing the same challenge today and ended up using the following solution within main(). Works great for me when using Qt 5.3. You should of course implement a better way to check whether 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();
}
like image 115
m00re Avatar answered Sep 28 '22 08:09

m00re