PDA

View Full Version : Tray icon & taskbar app button hide



been_1990
9th May 2009, 19:05
I want to have my application to be maximized while being shown at the systray in Windows. I tried this (following the QT Demos):


private:
QSystemTrayIcon *trayIcon;
..............{}.................
trayIcon->setVisible(true);
trayIcon->show();

But that only crashes the application. And I'm clueless. I also needed to have the app's taskbar button to hide itself. I did some searching but didnt find anything up-to-date. Any help is welcome and thanked.:D

Fastman
10th May 2009, 12:07
For example:

app.h


private:
QSystemTrayIcon *trayIcon;
QMenu *trayIconMenu;

QAction *minimizeAction;
QAction *maximizeAction;
QAction *restoreAction;
QAction *quitAction;

void createActions();
void createTrayIcon();
void showMessage();


app.cpp


app::app(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
...
createActions();
createTrayIcon();
trayIcon->setIcon(QIcon(QString::fromUtf8(":/app/Resources/Add.png")));
trayIcon->show();
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason )),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReas on)));
...
}

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

restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void HSM::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);

trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setToolTip(tr("My app"));
}

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

restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void app::showMessage()
{
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon();
trayIcon->showMessage(tr("My app"), tr("Some msg"), icon, 100);
}

void app::iconActivated(QSystemTrayIcon::ActivationReas on reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
show();
break;
case QSystemTrayIcon::DoubleClick:
show();
break;
case QSystemTrayIcon::MiddleClick:
showMessage();
break;
default:
;
}
}

been_1990
11th May 2009, 01:48
Thanks! The tray icon is working really great. And that helped me understand QT a little bit more.:)

And now...
How do I hide its taskbar button?
http://www.qtcentre.org/forum/attachment.php?attachmentid=3245&stc=1&d=1242002809

been_1990
12th May 2009, 11:52
Anyone?....

ComaWhite
12th May 2009, 13:04
you can try like how I did it.



void
MainWindow::closeEvent(QCloseEvent *event) {
if (Settings::enableSystemTray()) {
if (Settings::minimizeToTrayOnClose()) {
hide();
event->ignore();
}
} else {
QMainWindow::closeEvent(event);
}
}

been_1990
14th May 2009, 03:24
Can you explain what that does? And how to call it(send an event)

auba
15th May 2009, 12:10
closeEvent is called when the underlaying window system wants to close the main window. On windows by Alt-F4 or the close button.
If enabled in the program's settings, you would provide the main window to be closed at all, but only be minimized to the tray.

been_1990
15th May 2009, 20:37
Oh... so it hides the window but doesnt close it... (what was I thinking...) but thats not what I need..
I want my window to be shown, but remove the taskbar button.

auba
15th May 2009, 23:50
Maybe something like



void
MainWindow::showEvent(QShowEvent *event) {
trayIcon->hide();
QMainWindow::showEvent(event);
}
}
void
MainWindow::hideEvent(QHideEvent *event) {
trayIcon->show();
QMainWindow::hideEvent(event);
}
}


(Assumed, neither tested nor knowed)

been_1990
16th May 2009, 06:44
Ok, will take a look at it

been_1990
17th May 2009, 00:00
That's not it. I dont want to hide the trayIcon. I want to hide the taskbar button.

auba
17th May 2009, 10:25
As the taskbar content is managed by the explorer.exe, this is something deep inside of your Vista...

I believe there was something that the taskbar entry was not shown when the main window of the app had no frame

setWindowFlags(Qt::FramelessWindowHint);


Another approach is shown on Codeguru (http://www.codeguru.com/cpp/frameworks/advancedui/article.php/c3227/). This example uses the MFC, but also explains what has to be done.

been_1990
17th May 2009, 11:30
mine is already set to Qt::FramelessWindowHint, doesnt change it. So I guess the solution is OS dependant.

auba
17th May 2009, 11:37
I've never seen any explorer.exe on my system, so... yes :D Indeed, I think it is window manager dependent.

talk2amulya
17th May 2009, 11:45
here is how to hide the taskbar button, it was already explained to you in one of the earlier posts:


void mymainwindow::closeEvent(QCloseEvent *event) {
hide();
event->ignore();
}

notice that you'll have to subclass QMainWindow, here mymainwindow is subclass of QMainWindow..if you have any other questions, go through this thread again, your problem has already been solved.

talk2amulya
17th May 2009, 11:47
I've never seen any explorer.exe on my system, so... yes :D Indeed, I think it is window manager dependent.

explorer.exe is always running on your windows system..killing that process removes your whole taskbar..check your task manager, you'll find explorer.exe staring at you like deer in headlights.

auba
17th May 2009, 12:01
I do not run windows at all as I have to work - not to play. I only use Vista to start my games :) but thats OT

been_1990
17th May 2009, 13:36
here is how to hide the taskbar button, it was already explained to you in one of the earlier posts:


void mymainwindow::closeEvent(QCloseEvent *event) {
hide();
event->ignore();
}

notice that you'll have to subclass QMainWindow, here mymainwindow is subclass of QMainWindow..if you have any other questions, go through this thread again, your problem has already been solved.

But doesnt that only hide my window? And if that really is the solution how do I call closeEvent()? I tried several ways but I really didnt get it. I need the taskbar button to be always hidden. I'm really lost here... :confused:

talk2amulya
17th May 2009, 14:53
ok, so normally the way it works is when you have your window maximised, you dont have any tray icon..when you close the window, the taskbar button disappears and tray icon appears..then you double click the tray icon or choose maximise from its context menu, and the window appears and taskbar button appears..isnt that how you want it? if you do, close event would be called when you click close icon of window..or you can call it yourself if you want your application to start minimized..remember that when window is shown, taskbar button would always be there, there is no way to avoid that and i dont understand why anyone would even want that..you can use showEvent, hideEvent, closeEvent as mentioned in the posts..closeEvent code that i posted earlier would close your window and remove the taskbar button, right then you have to show the tray icon in tray, add that code..further, you can use the code posted by Fastman to work out the context menu of the tray icon..and you have your job done.

been_1990
17th May 2009, 19:14
A image is worth many words.This is what I need:

From this:
http://www.qtcentre.org/forum/attachment.php?attachmentid=3272&stc=1&d=1242583905

Note the taskbar button.

To this:
http://www.qtcentre.org/forum/attachment.php?attachmentid=3273&stc=1&d=1242583905

Note no taskbar button.

been_1990
20th May 2009, 15:14
Any replies?

auba
20th May 2009, 15:25
...Another approach is shown on Codeguru (http://www.codeguru.com/cpp/frameworks/advancedui/article.php/c3227/). This example uses the MFC, but also explains what has to be done.


..remember that when window is shown, taskbar button would always be there, there is no way to avoid that and i dont understand why anyone would even want that..

What is left to say?

been_1990
22nd May 2009, 17:24
QSplashScreen removes the taskbar button, but nothing usefull can happen there.

saimel
12th March 2011, 22:28
Hi, I wanna know if you could resolve the problem with the taskbar icon because I want to do it now but I have no idea about how to do it