6

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.

2
  • Are you trying to do this only with QML, or C++/Qt can be used ?
    – BlueMagma
    Commented Jun 26, 2014 at 6:50
  • Hi BlueMagma, C++/Qt can be used. My UI is created using QML and I launch this from Qt main.cpp file. I have just updated my main.cpp file code in my question.
    – Mugunth
    Commented Jun 26, 2014 at 6:59

2 Answers 2

14

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();
}
1
  • Cons: it causes Qt5Widgets module dependency. Seems that it is not possible to use system tray from plain qml. Commented Nov 19, 2014 at 12:16
-2

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

int main(int argc, char* argv[])
{
    // ...
    QQuickView view;
    // ... 
    view.show();

    Window window;
    window.show();

    return app.exec();
}
3
  • Hi Sion Wart, where can i take window.cpp & window.h files ?
    – Mugunth
    Commented Jun 30, 2014 at 0:23
  • I understand you meant the windows.cpp /windows.h files from systray example. I tried them and made it work. But both Windows (one originating from Window.cpp & the qml based window created by QML (via QQuickView) appear. It doesnt solve my requirement. How to integarate them. If possible pls give more details.
    – Mugunth
    Commented Jun 30, 2014 at 1:24
  • You'll now need to integrate your functionality in C++. Modify the Window class to your needs, and start to send signals between your main program and Window, for example to show/hide/modify the icon. Commented Jun 30, 2014 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.