PDA

View Full Version : Own function in widget menu when right click.



RSX
11th June 2009, 17:52
I know the thread title is a bit confusing but that's because I don't know how to call it.
What I want to get is, when I right-click on some widget i.e. spinBox I get this:
http://img8.imageshack.us/img8/696/exampled.png (http://img8.imageshack.us/i/exampled.png/)
but I'd like to add to this list my own functions. Any idea how to do it?

Lykurg
11th June 2009, 18:40
See QWidget::customContextMenuRequested()

RSX
11th June 2009, 19:51
Humm, so far I managed to do:



QAction* randomize = new QAction(this);
randomize->setText("Randomize");
connect(randomize, SIGNAL(triggered()), this, SLOT(test()));
ui->spinBox->addAction(randomize);


and changed in desinger, spinBox contextMenuPolicy to actionsContextMenu.

This works fine but I want to keep the default options (Undo, redo, copy, paste etc.) and only append mine option at the end.

I checked what you gave me there Lykurg, but cannot figure out much of this.

Lykurg
11th June 2009, 20:08
actionsContextMenu

Should be CustomContextMenu! Use the emited customContextMenuRequested signal and connect it to your own slot. There you fetch the widgets actions via QWidget:actions() create a menu out of them, add your own actions and and execute it.

OR

set to actionsContextMenu, use the way you mentioned by adding the custom functions to the widget and it should work


EDIT: If you want the same look and feel use createStandardContextMenu() and alter the retrieved QMenu with your own actions.

Sorry for the confusing answers, but you will find all necessary informations...

spirit
11th June 2009, 20:19
maybe this (http://www.qtcentre.org/forum/p-qtreewidgetitem-have-contextmenu--post93472/postcount3.html) can help you.