[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


  • Moderators

    @cristeab I think you will need to subclass QQmlApplicationEngine and load the QML there. In this way you will able to add QSystemTrayIcon there. Get the top window using rootObjects and cast it to QQuickWindow. Now the problem I guess would be to trap close event in order hide the window when close is clicked. QQuickWindow has closing signal but can't use it since QQuickCloseEvent is not public yet. So one way would be to use eventFilter for QQuickWindow 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


  • Moderators

    @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.


  • Moderators

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.