Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qml systemtrayicon without qwidget/qapplication?

Tags:

qt

qml

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

like image 622
Gibbz Avatar asked Jan 27 '26 17:01

Gibbz


1 Answers

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)

like image 74
eyllanesc Avatar answered Jan 30 '26 20:01

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!