[Solved] Using QSystemTrayIcon with an application based on QQmlApplicationEngine
I have an application developed with Qt5.4. In main file I am using QQmlApplicationEngine to load directly the UI from QML file. My problem is that I need to minimise this application to tray icon, however I don't see how to use QSystemTrayIcon from my application, written mostly in QML. Could you advise ?
@cristeab I think you will need to subclass
QQmlApplicationEngineand load the QML there. In this way you will able to add
QSystemTrayIconthere. Get the top window using
rootObjectsand cast it to
QQuickWindow. Now the problem I guess would be to trap close event in order hide the window when close is clicked.
closingsignal but can't use it since
QQuickCloseEventis not public yet. So one way would be to use
QQuickWindowobject and look out for close event and ignore it and hide the window.
Can you try this ?
I have been able to create a QSystemTrayIcon without deriving from QQmlApplicationEngine. I get with rootObjects() a pointer to QQuickWindow, but when I try to show a message using QSystemTrayIcon the entire application crashes. It doesn't matter if QSystemTrayIcon parent is QQuickWindow or NULL, in both cases I get a crash.
@cristeab Can you post the relevant code ?
I have found the problem: I should instantiate a QApplication object instead of QGuiApplication in my main function. Thanks for your help.
@cristeab Ok. So does it not crash now ?
Actually I don't need to subclass from QQmlApplicationEngine in order to create a system icon, but I must use QApplication, otherwise the application will crash.