I would like to create a menu when the mouse is above a Qwidget and the right button of the mouse is pressed
I looked at mouseevent example but I am not sure how to display the menu.
did anybody came across this or has an idea how to do it??
thanks
I would like to create a menu when the mouse is above a Qwidget and the right button of the mouse is pressed
I looked at mouseevent example but I am not sure how to display the menu.
did anybody came across this or has an idea how to do it??
thanks
I think you can use QMenu and its function void popup ( const QPoint & p, QAction * atAction = 0 ) for this.
I would guess that there is a widget in your app, and when you right click on it a menu should popup. So yu take your widget's signal
Qt Code:
To copy to clipboard, switch view to plain text mode
and connect it to a slot. Inside this slot create a QMenu object, populate it with necessary data and call popup.
I'm a rebel in the S.D.G.
Do you have some examples
for what I understood
fisrt i need
Qt Code:
connect(mywidget, SiGNAL(rightClick()), this , popupMenu()) ...To copy to clipboard, switch view to plain text mode
then declare QMenu into the popupMenu()
Qt Code:
QMenu menu; menu.addAction(a); menu.popup(pos,at); ...To copy to clipboard, switch view to plain text mode
Does the rightclick() SIGNAL exist for a QTableview??
Last edited by SunnySan; 11th November 2008 at 12:50.
no. you get a contextMenuEvent, when the right mouse button is clicked.
(if you configure the QWidget properly: see QWidget::setContextMenuPolicy() )
Lets say you have a table and a right click on it should call a context menu.
Qt Code:
// create table, let it be as follows To copy to clipboard, switch view to plain text mode
Then declare (as a slot) and implement your popupYourMenu slot
Qt Code:
{ // create popupMenu as QMenu object // populate it popupMenu->popup(pos); }To copy to clipboard, switch view to plain text mode
That should help.
I'm a rebel in the S.D.G.
waynew (10th January 2010)
just create a QMenu in the contextMenuEvent(QGraphicsSceneContextMenuEvent *event) function and add QAction in the menu. And u can connect these QAction to different slots which is called when u select a particular option from the menu.
Example:
contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
QAction action1(QIcon("./abcd.png"), "action 1", this);
QAction action2(QIcon("./abcd2.png"), "action 2", this);
connect(&action1, SIGNAL(triggered()), this, SLOT(xyz()));
connect(&action2, SIGNAL(triggered()), this, SLOT(xyz2()));
QMenu myMenu;
myMenu.addAction(&action1);
myMenu.addAction(&action1);
}
I hope this may help you.
Ya, QWidget::contextMenuEvent is a better option.
You will also need to show the menu using QMenu::exec.
Also be careful about the actions being created on stack.
Thanks guys for your guidance but it doesn't seem to work.
where do you defined the right click ??
I add this code to my mainwindow.cpp
Qt Code:
To copy to clipboard, switch view to plain text mode
and
Qt Code:
{ qDebug()<<":popuprightclickMenu"; //just to see if activated connect(&action1, SIGNAL(triggered()), this, SLOT(about())); QMenu *popupMenu; popupMenu->addAction(&action1); popupMenu->addAction(&action1); popupMenu->popup(pos); }To copy to clipboard, switch view to plain text mode
BUT I can not see even the debug comments in my console...
This signal customContextMenuRequested is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu. Please check contextMenuPolicy for you widget.
Bookmarks