PDA

View Full Version : Adding contextmenu for QTreeWidgetItem .



riarioriu3
25th September 2012, 12:01
Inside constructor
---------------------

connect(ui.UserSpecificMaterial_treeWidget, SIGNAL(customContextMenuRequested(const QPoint &)),this, SLOT(ContMenu(const QPoint &)));

Inside slot
-------------


void MyContMenu::ContMenu(const QPoint &pos)
{
QTreeWidgetItem *item = ui.UserSpecificMaterial_treeWidget->itemAt(pos);
if (!item)
return;
QMenu *menu = new QMenu(ui.UserSpecificMaterial_treeWidget);
myAction = menu->addAction("Remove");
myAction->setIcon(QIcon(QString::fromUtf8("Resources/Remove.png")));
myAction->setShortcut(tr("Ctrl+D"));
myAction->setStatusTip(tr("Remove the respective material from the User DB"));
menu->exec(ui.UserSpecificMaterial_treeWidget->viewport()->mapToGlobal(pos));
/---code to remove the item ./ }

In the above code whenever i right click on the QTreeWidgetItem it will show me the contextmenu having a single menuitem named Remove . What i want is whenever the user click on that remove menuitem at that time only it should remove that QTreeWidgetItem from the qtreewidget . but in the above code after rigtclick even if i click on any part of the UI it removes the respective QTreeWidgetItem from the treewidget , which i want to avoid .

Thankss in advance .

Robbie
25th September 2012, 13:01
It doesn't look like you're determining which menu item was selected. Try something like this:


QAction *selectedAction = menu->exec(ui.UserSpecificMaterial_treeWidget->viewPort()->mapToGlobal(pos));
if (selectedAction == myAction)
{
/*** code to remove the item ***/
}

riarioriu3
25th September 2012, 13:08
Its done .. Thankx brother ..