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. Minimizing Application to Tray

Minimizing Application to Tray

Scheduled Pinned Locked Moved General and Desktop
25 Posts 8 Posters 43.6k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    khalidmushtaq65
    wrote on last edited by
    #4

    Volker I have tried this too but can't get through.

    Will you please explain about QMainWindow based UI?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #5

      Your class MainWindow inherits from some base class. I suppose this is QMainWindow (others could be QDialog or QWidget for example).

      And for the code. Did you try already:

      @
      void MainWindow::changeEvent(QEvent *event)
      {
      QMainWindow::changeEvent(event);
          if(event->type() == QEvent::WindowStateChange)
              if(isMinimized())
                  this->hide();
      }
      @

      This let's Qt do the regular work and after the window is minimized you hide it.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • K Offline
        K Offline
        khalidmushtaq65
        wrote on last edited by
        #6

        I tried this but the same result again..
        Just getting the blank screen while minimizing the application..
        I can't understand what's the problem..

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #7

          Hi Khalidmushta,

          what exactly do you want to achieve?

          Minimize to system try, so to have a tray icon? Are you using "QSystemTryIcon":http://doc.qt.nokia.com/latest/qsystemtrayicon.html ?
          Can you show a bit more of your code which results in the problem? The best would be a very simple, small example showing the problem.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            khalidmushtaq65
            wrote on last edited by
            #8

            I want to minimize my application to tray when I click on Minimize button and same for the close button..

            I am using QSystemTryIcon, works fine with close button, but not working with minimize button.
            When I click on minimize button all the contents get hide and a screen with white background continues to show..

            This is for the minimize button that shows white background of screen but hides the content..

            @void MainWindow::changeEvent(QEvent *event)
            {
            if(event->type() == QEvent::WindowStateChange) {
            if(isMinimized())
            this->hide();
            event->ignore();
            }
            }@

            This is for the close button that works fine.

            @void MainWindow::closeEvent(QCloseEvent *event)
            {
            if (trayIcon->isVisible()) {
            this->hide();
            event->ignore();
            }
            }@

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

              [quote author="khalidmushtaq65" date="1300015096"]

              This is for the minimize button that shows white background of screen but hides the content..

              @void MainWindow::changeEvent(QEvent *event)
              {
              if(event->type() == QEvent::WindowStateChange) {
              if(isMinimized())
              this->hide();
              event->ignore();
              }
              }@

              [/quote]

              Not sure if this is the problem, but shouldn't that be:

              @void MainWindow::changeEvent(QEvent *event)
              {
              if(event->type() == QEvent::WindowStateChange) {
              if(isMinimized()) {
              this->hide();
              event->ignore();
              }
              }
              QMainWindow::changeEvent(event);
              }@

              1 Reply Last reply
              0
              • K Offline
                K Offline
                khalidmushtaq65
                wrote on last edited by
                #10

                I have tried that too but the problem still exists..

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kkrzewniak
                  wrote on last edited by
                  #11

                  @MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent)
                  {
                  ui.setupUi(this);

                  icon = new QSystemTrayIcon(this);
                  icon->show();
                  
                  menu = new QMenu(this);
                  
                  quitAction = new QAction("Quit",this);
                  connect(quitAction,SIGNAL(triggered()),this,SLOT(close()));
                  menu->addAction(quitAction);
                  icon->setContextMenu(menu);
                  
                  restore = new QAction("Restore",this);
                  restore->setEnabled(false);
                  connect(restore,SIGNAL(triggered()),this,SLOT(showNormal()));
                  menu->addAction(restore);
                  

                  }

                  void MainWindow::changeEvent(QEvent *e)
                  {
                  QMainWindow::changeEvent(e);
                  switch (e->type()) {
                  case QEvent::LanguageChange:
                  ui.retranslateUi(this);
                  break;
                  case QEvent::WindowStateChange:
                  {
                  if(isMinimized())
                  {
                  hide();
                  restore->setEnabled(true);
                  }
                  else
                  {
                  restore->setEnabled(false);
                  }
                  }
                  break;
                  default:
                  break;
                  }
                  }
                  @
                  Just put this code together to test Your issue it works ass expected on my Linux box. Why are You ignoring the change event? It's not like the close event witch You need to ignore to prevent your window from actually closing.

                  Me, Grimlock, not "nice dino". ME BASH BRAINS!

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    khalidmushtaq65
                    wrote on last edited by
                    #12

                    I am sorry to say that it's not working too. Same problem exists..

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

                      Well, if kkrzewniak tested the code he posted, and you just claim "it doesn't work", perhaps you should start by giving more information. For starters: what platform are we talking about anyway? Can you post a minimal, yet complete, complileable example that shows the problem?

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        khalidmushtaq65
                        wrote on last edited by
                        #14

                        I am using Microsoft Windows 7 Ultimate.

                        This is mainwindow.h
                        @#ifndef MAINWINDOW_H
                        #define MAINWINDOW_H

                        #include <QWidget>
                        #include <QSystemTrayIcon>

                        #include <QMainWindow>

                        class QMenu;

                        namespace Ui {
                        class MainWindow;
                        }

                        class MainWindow : public QMainWindow
                        {
                        Q_OBJECT

                        public:
                        explicit MainWindow(QWidget *parent = 0);
                        ~MainWindow();
                        bool createConnection();

                        private:
                        Ui::MainWindow *ui;

                        // Tray Icon Functions
                        void createTrayActions();
                        void createTrayIcon();
                        void setTrayIcon();
                        
                        void closeEvent(QCloseEvent *);
                        void changeEvent(QEvent *);
                        
                        QSystemTrayIcon *trayIcon;
                        QMenu *trayIconMenu;
                        QAction *showHideTray;
                        QAction *closeTray;
                        

                        private slots:
                        void trayIconClicked(QSystemTrayIcon::ActivationReason);
                        void showHideWindow();

                        };

                        #endif // MAINWINDOW_H@

                        This is mainwindow.cpp

                        @#include "mainwindow.h"
                        #include "ui_mainwindow.h"
                        #include <QMessageBox>
                        #include <QtSql/QSqlError>
                        #include <QMenu>
                        #include <QCloseEvent>
                        #include <QDir>

                        MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                        {
                        ui->setupUi(this);

                        createTrayActions();
                        createTrayIcon();
                        setTrayIcon();
                        trayIcon->show();
                        

                        }
                        void MainWindow::changeEvent(QEvent *event)
                        {
                        QMainWindow::changeEvent(event);
                        if(event->type() == QEvent::WindowStateChange) {
                        if(isMinimized())
                        this->hide();
                        }
                        }

                        void MainWindow::closeEvent(QCloseEvent *event)
                        {
                        if (trayIcon->isVisible()) {
                        showHideWindow();
                        event->ignore();
                        }
                        }

                        void MainWindow::showHideWindow()
                        {
                        if(this->isVisible())
                        {
                        this->hide();
                        showHideTray->setIcon(QIcon(":/images/arrow_up.ico"));
                        showHideTray->setText("Show Main Window");
                        }
                        else
                        {
                        this->show();
                        showHideTray->setIcon(QIcon(":/images/arrow_down.ico"));
                        showHideTray->setText("Hide Main Window");
                        }
                        }

                        void MainWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
                        {
                        if(reason == QSystemTrayIcon::DoubleClick)
                        showHideWindow();
                        }

                        void MainWindow::setTrayIcon()
                        {
                        trayIcon->setIcon(QIcon(":/images/Download.ico"));
                        }
                        void MainWindow::createTrayIcon()
                        {
                        trayIconMenu = new QMenu(this);
                        trayIconMenu->addAction(showHideTray);
                        trayIconMenu->addSeparator();
                        trayIconMenu->addAction(closeTray);

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

                        }
                        void MainWindow::createTrayActions()
                        {
                        showHideTray = new QAction(tr("&Hide Main Window"), this);
                        connect(showHideTray, SIGNAL(triggered()), this, SLOT(showHideWindow()));
                        showHideTray->setIcon(QIcon(":/images/arrow_down.ico"));
                        closeTray = new QAction(tr("&Exit"), this);
                        connect(closeTray, SIGNAL(triggered()), qApp, SLOT(quit()));
                        closeTray->setIcon(QIcon(":/images/Delete.png"));
                        }
                        MainWindow::~MainWindow()
                        {
                        delete ui;
                        delete trayIcon;
                        delete trayIconMenu;
                        delete showHideTray;
                        delete closeTray;
                        }@

                        1 Reply Last reply
                        0
                        • AlicemirrorA Offline
                          AlicemirrorA Offline
                          Alicemirror
                          wrote on last edited by
                          #15

                          If it can be useful for those is trying to solve this problem:

                          Actually I develop under linux ubuntu then rebuild under windows 7 ultimate and under xp. I use virtual machines but it doesn't matter, the machine is as a real computer (little slow).

                          Using the beta Qt-Quick and Qt-Creator + Qt-sdk (all Qt wrote correct, eh Andre ? :) :) ) I experience problems under Windows 7. Strange problems that probably are not so depending from the application itself.

                          Enrico Miglino (aka Alicemirror)
                          Balearic Dynamics
                          Islas Baleares, Ibiza (Spain)
                          www.balearicdynamics.com

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            giesbert
                            wrote on last edited by
                            #16

                            Hi khalidmushta..,

                            I created an app using sys tray icon which works on windows 7.
                            You can find the sources on "gitorious":http://gitorious.org/giesbert-s-software/giesbert-s-world-clock

                            The code, where I minimize / restore to / from system tray looks like this:

                            @
                            void GiWorldClock::hideEvent(QHideEvent* pEvent)
                            {
                            if(isMinimized())
                            {
                            ::ShowWindow(this->winId(), SW_HIDE);
                            m_pRestoreAction->setEnabled(true);
                            m_pMinAction->setDisabled(true);
                            }
                            QWidget::hideEvent(pEvent);
                            }

                            void GiWorldClock::showEvent(QShowEvent* pEvent)
                            {
                            QWidget::showEvent(pEvent);
                            ::ShowWindow(this->winId(), SW_SHOW);
                            m_pRestoreAction->setDisabled(true);
                            m_pMinAction->setEnabled(true);
                            }
                            @

                            Nokia Certified Qt Specialist.
                            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              giesbert
                              wrote on last edited by
                              #17

                              Ok, as I use win32 functions, it's n9ot portable :-) but it works on windows....

                              Nokia Certified Qt Specialist.
                              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                khalidmushtaq65
                                wrote on last edited by
                                #18

                                I started the debugger on and when I minimize application it displays following error..

                                (Internal error: pc 0x111 in read in psymtab, but not in symtab.)

                                When I close application, it shows no error and hides the application to tray..

                                1 Reply Last reply
                                0
                                • AlicemirrorA Offline
                                  AlicemirrorA Offline
                                  Alicemirror
                                  wrote on last edited by
                                  #19

                                  Just a small advice, what I will do in your case. Why don't remove from the windows the minimize button, leaving only the close, that works fine? Two buttons that do the same thing can confuse the user, not ?

                                  Enrico Miglino (aka Alicemirror)
                                  Balearic Dynamics
                                  Islas Baleares, Ibiza (Spain)
                                  www.balearicdynamics.com

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    khalidmushtaq65
                                    wrote on last edited by
                                    #20

                                    ya.. nice advice:-)

                                    1 Reply Last reply
                                    0
                                    • AlicemirrorA Offline
                                      AlicemirrorA Offline
                                      Alicemirror
                                      wrote on last edited by
                                      #21

                                      I had the same problem with a windowless device... Decided for this solution :)

                                      Enrico Miglino (aka Alicemirror)
                                      Balearic Dynamics
                                      Islas Baleares, Ibiza (Spain)
                                      www.balearicdynamics.com

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        manuel.gysin
                                        wrote on last edited by
                                        #22

                                        To remove the button is no solution...
                                        Here is a dirty hack, but it works.

                                        @void MainWindow::changeEvent(QEvent *e)
                                        {
                                        if(isMinimized())
                                        {
                                        show(); // The hack
                                        slotToggleVisibility(); // Your tray icon code
                                        }
                                        }@

                                        1 Reply Last reply
                                        0
                                        • R Offline
                                          R Offline
                                          Ryan
                                          wrote on last edited by
                                          #23

                                          If anyone still has this issue, the solution is to call qApp->processEvents() before calling hide(), ie.

                                          @void ApplicationWindow::changeEvent(QEvent *event)
                                          {
                                          QWidget::changeEvent(event);
                                          if (event->type() == QEvent::WindowStateChange)
                                          {
                                          QWindowStateChangeEvent e = (QWindowStateChangeEvent)event;
                                          // make sure we only do this for minimize events
                                          if ((e->oldState() != Qt::WindowMinimized) && isMinimized())
                                          {
                                          qApp->processEvents();
                                          hide();
                                          event->ignore();
                                          }
                                          }
                                          }
                                          @

                                          The test for the old state is probably unnecessary, but can't hurt.

                                          If this doesn't help in your particular situation, try calling hide from a single-shot timer, so that the call is outside the minimize event handler:

                                          @void ApplicationWindow::changeEvent(QEvent *event)
                                          {
                                          QWidget::changeEvent(event);
                                          if (event->type() == QEvent::WindowStateChange)
                                          {
                                          QWindowStateChangeEvent e = (QWindowStateChangeEvent)event;
                                          // make sure we only do this for minimize events
                                          if ((e->oldState() != Qt::WindowMinimized) && isMinimized())
                                          {
                                          QTimer::singleShot(0, this, SLOT(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