rigth click of mouse to create sub menu
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
Re: rigth click of mouse to create sub menu
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
Code:
void customContextMenuRequested
( const QPoint & pos
)
and connect it to a slot. Inside this slot create a QMenu object, populate it with necessary data and call popup.
Re: rigth click of mouse to create sub menu
Do you have some examples
for what I understood
fisrt i need
Code:
connect(mywidget, SiGNAL(rightClick()), this , popupMenu())
...
then declare QMenu into the popupMenu()
Code:
menu.addAction(a);
menu.popup(pos,at);
...
Does the rightclick() SIGNAL exist for a QTableview??
Re: rigth click of mouse to create sub menu
no. you get a contextMenuEvent, when the right mouse button is clicked.
(if you configure the QWidget properly: see QWidget::setContextMenuPolicy() )
Re: rigth click of mouse to create sub menu
Lets say you have a table and a right click on it should call a context menu.
Code:
// create table, let it be as follows
connect(table,
SIGNAL(customContextMenuRequested
( const QPoint &)),
this,
SLOT(popupYourMenu
(const QPoint &)));
Then declare (as a slot) and implement your popupYourMenu slot
Code:
void YourClass
:popupYourMenu
(const QPoint & pos
) {
// create popupMenu as QMenu object
// populate it
popupMenu->popup(pos);
}
That should help.
Re: rigth click of mouse to create sub menu
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.
Re: rigth click of mouse to create sub menu
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.
Re: rigth click of mouse to create sub menu
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
Code:
connect(ui.
mytableView,
SIGNAL(customContextMenuRequested
( const QPoint &)),
this,
SLOT(popuprightclickMenu
(const QPoint &)));
and
Code:
void mainwindow
::popuprightclickMenu(const QPoint & pos
) {
qDebug()<<":popuprightclickMenu"; //just to see if activated
QAction action1
( "action 1",
this);
connect(&action1, SIGNAL(triggered()), this, SLOT(about()));
popupMenu->addAction(&action1);
popupMenu->addAction(&action1);
popupMenu->popup(pos);
}
BUT I can not see even the debug comments in my console...
Re: rigth click of mouse to create sub menu
This signal customContextMenuRequested is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu. Please check contextMenuPolicy for you widget.