PDA

View Full Version : [SOLVED] Help Request - Table with Right-Click Context Menu



Sarol
3rd May 2011, 16:07
Good Day Everyone,

I have been using QT Designer (3.3.6) for about 2 weeks now and while I understand the basic concepts it seems I need help with some of the not so obvious things.

In this caseI have a QTable in my main form. What I am looking to do is add a Right-Click context menu on the table, that has a select set of options (i.e. Delete, Add, Modify). Upon clicking on one of the options it would call a method (or I suppose the proper terminolgy is Slot) that I have written. I currently have the method set up on a button so when the user clicks it, everything works. Now I'm just looking to add the Context Menu for quicker access.

In the research I have done I keep running across this QPopupMenu option, but not sure how to use that just yet. I have also searched the forums and did not see anything that really explains whats going on.

Any and all help would be appreciated, even if it is a "Hey check out this post <insert link here>."

Thanks,

Sarol

john_god
3rd May 2011, 20:11
In your widget form change the property "contestMenuPolicy" (in your right pane) to DefaultContextMenu. Right click the widget and choose "Go to Slot" -> "CustomContextMenuRequest" . It will create a function for you. Now in that function add a QMenu:


void someWidget::someWidgetContextMenu(QPoint pos)
{
QMenu menu;
menu.addAction(someActionCreatedByYou1);
menu.addAction(someActionCreatedByYou2);
menu.exec(QCursor::pos());
}

Voilá. You will have to add proper includes i.e #include<QMenu>

Sarol
3rd May 2011, 21:01
Greetings john_god,

Thank you for the response. While I did not have the contestMenuPoliy option you mentioned, I was able to see where I was going wrong in setting up my menu, based off the code snippet you posted.

I greatly appreciate your help in this matter.

Sarol