View Full Version : ContextMenu

20th February 2006, 11:15
How do I restrict my contextmenu to be enabled only within my widget....
say my widget is Rectangle and i want to enable my context menu only within this rectangle.....

How am I restrict my menu.exec() to the coordinates of my rectangle...

Thanks in Advance

20th February 2006, 11:21
If you dont want play with calculating you can reinplement mousePressEvent only for your rectangle;)

20th February 2006, 15:42
I don't quite get your point. If you are implementing a context menu event for your widget, isn't it within your widget then?

Another thing is, if you wanna show the context menu only for a specific part of your widget.
Then you could set your widget's contextMenuPolicy to Qt::CustomContextMenu and connect to the customContextMenuRequested signal which provides a position parameter.

20th February 2006, 15:51
I mean reinplementing mousePressEvent() that can give ignore right button pressed

Chicken Blood Machine
20th February 2006, 23:13
I mean reinplementing mousePressEvent() that can give ignore right button pressed

Reimplementing contextMenuEvent() is the right thing to do, not mousePressEvent().

20th February 2006, 23:32
Although I haven't tried this, and I am not at a comp w/ Qt installed right now and won't be for several hours, but as far as I understand the docs, you can also do the following:

1. Create all the actions you want in the menu.
2. Add these actions to the widget, using QWidget::addAction(...)
3. Set the widgets contextPolicy to Qt::ActionsContextMenu

If I understand correctly, this way, when your widget receives the context menu event, it will display its QWidget::actions() as context menu.


21st February 2006, 00:46
Is the issue here about having a context menu for the whole widget or only for a part of it? Is that rectangle a widget on its own or is it just some hotspot not being a standalone widget itself? If the former, then all above mentioned methods apply, but if the latter, the issue is about checking whether the event is to be handled or ignored and you have to do it by checking coordinates of the event -- if they are within specified bounds, execute the menu (QMenu::exec), otherwise just do nothing.

21st February 2006, 08:10
What i'm actually trying out is that .....

When we minimise a window and then right click on it, we a shown with these options right("close","maximise","minimise","move","size","restore")...... and even when we right click on the tiltle bar(in blue) when the window is open we are shown with these options...

These are given to us by the operating System ...

IN my application....
What i did was, I cut off the title bar( the thing in blue in every window we open with minimise,restore, and close options ) and implemented a widget which is a simple rectangle on top of my UI inteface of my application and I was trying to implement a defaultContextMenu on this and tried to see whether this could act as the context menu given to us by the operating system......

Is there any thing which could give us this and
And if it does not exists what could be the solution given to this.....

This is where I'm stuck .....Please give me the solution in detail.... including the code to do this ......:confused:

21st February 2006, 11:41
This is where I'm stuck .....Please give me the solution in detail.... including the code to do this ......
All though you have the right to ask this, you should be aware that this is not very appreciated on forums such as this one.
People like to help, but you should show some effort to solve your own problems, therefore asking for the full solution is not liked, never the less, allowed.

It also doesn't help if you post double, and then when the double post was deleted post it again...

21st February 2006, 11:54
What exactly do you want to achieve? Maybe there are better ways to do this. But if you insist on doing it this way, just create that rectangle as a QFrame and either reimplement its contextMenuEvent directly if you code by hand or install an event filter on the frame to catch its events and handle them elsewhere. With Qt4 there is also an option to use custom context menu on the frame which gives an ability to use signals and slots for handling the menu.