Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSystemTrayIcon in Qt Quick application

QSystemTrayIcon in Qt Quick application

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 8.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aleskey78
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Aleskey78
        wrote on last edited by
        #3

        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
        }
        

        }
        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Aleskey78
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Aleskey78
              wrote on last edited by
              #6

              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();
              }
              

              }@

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved