PDA

View Full Version : rigth click of mouse to create sub menu



SunnySan
10th November 2008, 17:26
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

lyuts
10th November 2008, 19:57
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


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.

SunnySan
11th November 2008, 11:44
Do you have some examples
for what I understood
fisrt i need


connect(mywidget, SiGNAL(rightClick()), this , popupMenu())
...


then declare QMenu into the popupMenu()


QMenu menu;
menu.addAction(a);
menu.popup(pos,at);
...


Does the rightclick() SIGNAL exist for a QTableview??

caduel
11th November 2008, 11:54
no. you get a contextMenuEvent, when the right mouse button is clicked.
(if you configure the QWidget properly: see QWidget::setContextMenuPolicy() )

lyuts
11th November 2008, 12:23
Lets say you have a table and a right click on it should call a context menu.



// create table, let it be as follows
QTableWidget *table = new QTableWidget;

connect(table, SIGNAL(customContextMenuRequested ( const QPoint &)), this, SLOT(popupYourMenu(const QPoint &)));



Then declare (as a slot) and implement your popupYourMenu slot


void YourClass:popupYourMenu(const QPoint & pos)
{
// create popupMenu as QMenu object
// populate it

popupMenu->popup(pos);
}


That should help.

ankit17.ag
11th November 2008, 13:12
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.

aamer4yu
12th November 2008, 07:08
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.

SunnySan
12th November 2008, 10:22
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


connect(ui.mytableView, SIGNAL(customContextMenuRequested ( const QPoint &)), this, SLOT(popuprightclickMenu(const QPoint &)));


and



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()));

QMenu *popupMenu;
popupMenu->addAction(&action1);
popupMenu->addAction(&action1);

popupMenu->popup(pos);
}


BUT I can not see even the debug comments in my console...

pastor
12th November 2008, 11:29
This signal customContextMenuRequested is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu. Please check contextMenuPolicy for you widget.