QSystemTrayIcon in Qt Quick application



  • Hi,

    I have a QML application with a system tray icon... When the Windows 7 shuts down the main window goes into the tray and prevents Windows to continue. When the application is already in a tray then Windows shuts down properly.

    Please suggest something to avoid this behaviour.

    Thanks.



  • I guess it has to do with the way you make the application minimize into the tray in the first place. Could you show that code?



  • It's like in this example: doc.trolltech.com/4.2/desktop-systray.html

    @class QmlAppView : public QmlApplicationViewer
    {
    Q_OBJECT
    public:
    explicit QmlAppView(QWidget *parent = 0);

    protected:
    virtual bool event(QEvent * event);

    private:
    void createActions();
    void createTrayIcon();
    void setIcon();
    void closeEvent(QCloseEvent *);
    QSystemTrayIcon *trayIcon;
    QMenu *trayIconMenu;
    QAction *minimize;
    QAction *open;
    QAction *close;

    signals:

    private slots:
    void trayIconClicked(QSystemTrayIcon::ActivationReason);
    };@

    @QmlAppView::QmlAppView(QWidget *parent) :
    QmlApplicationViewer(parent)
    {
    createActions();
    createTrayIcon();
    setIcon();

    trayIcon->show();
    }

    bool QmlAppView::event(QEvent *event)
    {
    if (event->type() == QEvent::RequestSoftwareInputPanel ||
    event->type() == QEvent::CloseSoftwareInputPanel)
    {
    //qDebug() << event;
    }
    return QmlApplicationViewer::event(event);
    }

    void QmlAppView::createActions()
    {
    minimize = new QAction(tr("Mi&nimize"), this);
    connect(minimize, SIGNAL(triggered()), this, SLOT(hide()));

    open = new QAction(tr("&Open"), this);
    connect(open, SIGNAL(triggered()), this, SLOT(show()));
    
    close = new QAction(tr("&Quit"), this);
    connect(close, SIGNAL(triggered()), qApp, SLOT(quit()));
    

    }

    void QmlAppView::createTrayIcon()
    {
    trayIconMenu = new QMenu(this);

    trayIconMenu->addAction(open);
    trayIconMenu->addAction(minimize);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(close);
    
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
    
    connect(
            trayIcon,
            SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this,
            SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))
    );
    

    }

    void QmlAppView::setIcon()
    {
    trayIcon->setIcon(QIcon(":/qml/images/app.png"));
    }

    void QmlAppView::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
    {
    if(reason == QSystemTrayIcon::Trigger)
    this->show();
    }

    void QmlAppView::closeEvent(QCloseEvent *event)
    {
    if (trayIcon->isVisible()) {
    trayIcon->showMessage(tr("Systray"),
    tr("This application is still running. To quit please click this icon and select Quit"));
    hide();

        event->ignore(); // Don't let the event propagate to the base class
    }
    

    }
    @



  • Would it help if you check the spontaneous flag of the QCloseEvent? I think that it will be true if the system wants your application to close, and false if the user just clicked the close button, but you will have to check to make sure.



  • Yes, it is exactly like you said.

    This code should work:
    @void QmlAppView::closeEvent(QCloseEvent *event)
    {
    if (!event->spontaneous() && trayIcon->isVisible()) {
    trayIcon->showMessage(tr("Systray"),
    tr("This application is still running. To quit please click this icon and select Quit"));
    hide();

        event->ignore();
    }
    

    }@
    And it works, but not every time. I'm still missing something...



  • It is weird, but now it works for all cases. But why?
    @void QmlAppView::closeEvent(QCloseEvent *event)
    {
    if (trayIcon->isVisible()) {
    trayIcon->showMessage(tr("Systray"),
    tr("This application is still running. To quit please click this icon and select Quit"));
    hide();

        //event->ignore();
    }
    

    }@


Log in to reply
 

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