PDA

View Full Version : programmatically right click on QSystemTrayIcon



noa l
7th April 2011, 15:12
Hi All
I will appreciate your help in this matter:

Normally, the context menu of a QSystemTRayIcon will appear when user right-click on the icon.

i want it to show also when left click. So I catch "activated" with "trigger".
Now what do i do? Sure enough you will say this line is good: menu->show().

However - if user makes a left click before any right click action was performed - the context menu is opened in 0,0 position instead of near the icon.

Since the system tray can be located up or down the desktop - i do not want to compute the desired location myself. I would rather immitate the "right click" action programmatically, as if the user right clicked the middle of the icon.

I do not know how to do that - I searched the net and try many things.. but nothing worked. Please note the QSystenTrayIcon is not a QWidget...

I would appreciate any help - and if you have a piece of code that works i will be very greatfull to see it..
Thanks
Noa

giacomelli.fabio
7th April 2011, 23:09
I did it in this way


void supertray::click_on_icon(QSystemTrayIcon::Activati onReason motivo)
{
if (motivo == QSystemTrayIcon::DoubleClick)
{
myClip.setWindowFlags(Qt::WindowStaysOnTopHint/* | Qt::FramelessWindowHint */);
myClip.show();
}

else if ((motivo == QSystemTrayIcon::Context) || (motivo == QSystemTrayIcon::Trigger))
trayMenu->show();
}

noa l
10th April 2011, 07:14
Thank you for your reply.

But as far as i can tell you did exactly what i did , which is "trayMeny->show" (Did i understand correct?). So I am still with the same problem. If the menu has never shown before - it will be opened in (0,0) position.

Any other ideas?

viulskiez
10th April 2011, 10:38
I think it is quite simple. Call QMenu::popup() instead of QMenu::show().


Example:


void MyWidget::activated(QSystemTrayIcon::ActivationRea son reason)
{
switch(reason)
{
case QSystemTrayIcon::Trigger:
trayContexMenu->popup(QCursor::pos());
break;
// ....
}

}


Hope it's helps..

noa l
10th April 2011, 10:55
hello viulskiez

It looks as if it did the trick... !!!

I was not aware of this option ,Thank you very much :-)

Noa

viulskiez
10th April 2011, 13:42
You are welcome.