PDA

View Full Version : QMenu popup: how close when clicked outside



powermax
27th February 2009, 01:34
Hi,

I have 2 QMenus (trayIconMenu1 and trayIconMenu2).



QMenu *trayIconMenu1 = new QMenu(this);
QMenu *trayIconMenu2 = new QMenu(this);


The first QMenu popups itself when i right click the icon in the system tray. I popup the second one when i left click the icon in the system tray.


trayIconMenu2->popup(QCursor::pos());

The problem is that the second one does not automatically close whenever i click outside the menu. The first one does close automatically.

I tried to inherit QMenu and overwrite mousePressEvent, changeEvent, focusOutEvent, event. I also tried these functions in the parent widget, which is a QDialog. None of these functions are called when I click outside the popup.

What event should I use so that the second QMenu automatically closes? Or is there something else I need to do?

Thx

talk2amulya
27th February 2009, 17:36
why dont u close your first menu when u pop-up your second menu?

jpn
28th February 2009, 12:24
Or perhaps you could dynamically populate the menu with help of QMenu::aboutToShow().

powermax
1st March 2009, 23:01
Thx for the replies.

@talk2amulya
The second menu and the first menu are independent of each other. When I left click the system tray icon only the second menu should popup. (The first menu only shows when I right click the system tray icon.) So the second menu should close in every case I press the mouse (left, right or middle click) somewhere outside the menu. The first menu is never invoked.

@jpn
I also thought of this to use it somewhere in the proces, but I dont see how I can use this function to close the second menu.

I would expect an event when I click outside the menu, but I cant find it.
So I don't understand what exactly happens when the user right clicks the menu followed by a click outside the menu. It seems like qt does some extra things in that case and not when the user does the same with a left click.

jpn
2nd March 2009, 07:03
@jpn
I also thought of this to use it somewhere in the proces, but I dont see how I can use this function to close the second menu.

Actually my idea was to use a single menu instead of two menus. For example you could try to use QSystemTrayIcon::Trigger vs. QSystemTrayIcon::Context to determine how to re-populate the menu on the fly.

powermax
4th March 2009, 03:18
It works now. I need to use the following code:


trayIconMenu2->activateWindow();
trayIconMenu2->popup(QCursor::pos());

Now I will dynamically populate my menu :).
Thx for the hints.