[Solved] Using QSystemTrayIcon with an application based on QQmlApplicationEngine
-
Hi
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 ?
thanks
Bogdan -
@cristeab I think you will need to subclass
QQmlApplicationEngine
and load the QML there. In this way you will able to addQSystemTrayIcon
there. Get the top window usingrootObjects
and cast it toQQuickWindow
. Now the problem I guess would be to trap close event in order hide the window when close is clicked.QQuickWindow
hasclosing
signal but can't use it sinceQQuickCloseEvent
is not public yet. So one way would be to useeventFilter
forQQuickWindow
object and look out for close event and ignore it and hide the window.
Can you try this ? -
Hi
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.
regards
Bogdan -
@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.