3

The last post here seems to imply that systemtrayicon no longer requires qwidget or qapplication... System tray icon without widgets

Ive given this a try and its not working for me. Am I correct in guessing this is an incorrect statement?

Put the sample code bellow and import.

in main.qml:

SystemTrayIcon {
        visible: true
        //icon.source: "qrc:/images/tray-icon.png"

        onActivated: {
            window.show()
            window.raise()
            window.requestActivate()
        }
}

Error: QWidget: Cannot create a QWidget without QApplication

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Gibbz
  • 69
  • 4

1 Answers1

1

TL; DR; No, you have misunderstood. SystemTrayIcon needs you to use QApplication.


Explanation:

SystemTrayIcon is a QSystemTrayIcon wrapper that can be used in QML and therefore it is necessary to use QApplication, and that is indicated in the docs:

Availability

A native system tray icon is currently available on the following platforms:

  • All window managers and independent tray implementations for X11 that implement the freedesktop.org XEmbed system tray specification.
  • All desktop environments that implement the freedesktop.org D-Bus StatusNotifierItem specification, including recent versions of KDE and Unity.
  • All supported versions of macOS. Note that the Growl notification system must be installed for showMessage() to display messages on OS X prior to 10.8 (Mountain Lion).

The Qt Labs Platform module uses Qt Widgets as a fallback on platforms that do not have a native implementation available. Therefore, applications that use types from the Qt Labs Platform module should link to QtWidgets and use QApplication instead of QGuiApplication.

To link against the QtWidgets library, add the following to your qmake project file:

QT += widgets

Create an instance of QApplication in main():

#include <QApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}

Note: Types in Qt.labs modules are not guaranteed to remain compatible in future versions.

(emphasis mine)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241