PDA

View Full Version : default contextMenu



Naveen
18th February 2006, 15:01
This may sound weird ...but
Could anyone please give me a code which has a widget(a simple widget which draws a rectangle in a QHBox) and this widget should implement a DefaultContextMenu and could u please say what this "contextMenuEvent()" is and what are the options this DefaultContextMenu gives it is like "Close","Maximise","Minimise","Restore"


Thanks In Advance
Naveen

jpn
18th February 2006, 15:44
There is no such thing as "default context menu" which would contain some default menu items. You need to construct the context menu by yourself. By changing the property QWidget::contextMenuPolicy changes the way context menu can be handled.



The default value of this property is Qt::DefaultContextMenu, which means the contextMenuEvent() handler is called. Other values are Qt::NoContextMenu, Qt::ActionsContextMenu, and Qt::CustomContextMenu. With Qt::CustomContextMenu, the signal customContextMenuRequested() is emitted.


So one way to popup a context menu is to override your widget's context menu event handler. The example below shows a way to show a context menu. Most probably you don't want to construct the menu each time it needs to be shown, rather construct it elsewhere and only show it here.



void YourWidget::contextMenuEvent(QContextMenuEvent* e)
{
QMenu menu(this);
QAction* act = menu.addAction("Something");
menu.exec(e->globalPos());
}