Minimizing Application to Tray
Page |
1 |
Hi,
I have got a problem. I want my application to minimize to tray when I click on minimize button or close button.
I have got success with close button but when I click on minimize button, all the content on the window i.e. buttons, images, text etc disappears but a white window remains on screen with no content at all and when I click on tray and click show all the content becomes visible again.
I am using following code to minimize application to tray.
- {
- if(isMinimized()) {
- this->hide();
- event->ignore();
- }
- }
- }
Thanks for any kind of help..
24 replies
Hi, happy to find this post, I’m trying to solve the same problem too :)
What I think at the actual state of my tests (not got solution yet) is that the event should be managed with some other things. It seems a problem of repaint without the window in place of the white rectangle.
If I find more I update immediately!
Hi Khalidmushta,
what exactly do you want to achieve?
Minimize to system try, so to have a tray icon? Are you using QSystemTryIcon [doc.qt.nokia.com] ?
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.
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..
- {
- if(isMinimized())
- this->hide();
- event->ignore();
- }
- }
This is for the close button that works fine.
- {
- if (trayIcon->isVisible()) {
- this->hide();
- event->ignore();
- }
- }
- {
- ui.setupUi(this);
- icon->show();
- connect(quitAction,SIGNAL(triggered()),this,SLOT(close()));
- menu->addAction(quitAction);
- icon->setContextMenu(menu);
- restore->setEnabled(false);
- connect(restore,SIGNAL(triggered()),this,SLOT(showNormal()));
- menu->addAction(restore);
- }
- {
- switch (e->type()) {
- ui.retranslateUi(this);
- break;
- {
- 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.
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;
- }
- {
- Q_OBJECT
- public:
- ~MainWindow();
- bool createConnection();
- private:
- Ui::MainWindow *ui;
- // Tray Icon Functions
- void createTrayActions();
- void createTrayIcon();
- void setTrayIcon();
- private slots:
- 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>
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- createTrayActions();
- createTrayIcon();
- setTrayIcon();
- trayIcon->show();
- }
- {
- if(isMinimized())
- this->hide();
- }
- }
- {
- if (trayIcon->isVisible()) {
- showHideWindow();
- event->ignore();
- }
- }
- void MainWindow::showHideWindow()
- {
- if(this->isVisible())
- {
- this->hide();
- showHideTray->setText("Show Main Window");
- }
- else
- {
- this->show();
- showHideTray->setText("Hide Main Window");
- }
- }
- {
- showHideWindow();
- }
- void MainWindow::setTrayIcon()
- {
- }
- void MainWindow::createTrayIcon()
- {
- trayIconMenu->addAction(showHideTray);
- trayIconMenu->addSeparator();
- trayIconMenu->addAction(closeTray);
- trayIcon->setContextMenu(trayIconMenu);
- connect(
- trayIcon,
- this,
- );
- }
- void MainWindow::createTrayActions()
- {
- connect(showHideTray, SIGNAL(triggered()), this, SLOT(showHideWindow()));
- connect(closeTray, SIGNAL(triggered()), qApp, SLOT(quit()));
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- delete trayIcon;
- delete trayIconMenu;
- delete showHideTray;
- delete closeTray;
- }
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.
Hi khalidmushta..,
I created an app using sys tray icon which works on windows 7.
You can find the sources on gitorious [gitorious.org]
The code, where I minimize / restore to / from system tray looks like this:
- {
- if(isMinimized())
- {
- ::ShowWindow(this->winId(), SW_HIDE);
- m_pRestoreAction->setEnabled(true);
- m_pMinAction->setDisabled(true);
- }
- }
- {
- ::ShowWindow(this->winId(), SW_SHOW);
- m_pRestoreAction->setDisabled(true);
- m_pMinAction->setEnabled(true);
- }
You must log in to post a reply. Not a member yet? Register here!





