How to find item selected from customContextMenuRequested() on QTreeView item?
- by vinaym
I have implemented contextual menus in QTreeView items with the following code
MyDerivedQTreeView->setModel(MyDerivedQAbstractItemModel);
MyDerivedQTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(MyDerivedQTreeView,
SIGNAL(customContextMenuRequested(const QPoint &)),
MyDerivedQAbstractItemModel(),
SLOT(contextualMenu(const QPoint &)));
void MyDerivedQAbstractItemModel::contextualMenu(const QPoint& point)
{
QMenu *menu = new QMenu;
menu->addAction(QString("Test Item"), this, SLOT(test_slot()));
menu->exec(MyDerivedQTreeView->mapToGlobal(point));
}
MyDerivedQAbstractItemModel::contextualMenu() gets called and I can see the contextual menu.
Problem is contextual menu should be visible only if user right-clicks on an item and it should be customized as per the item selected.
How do I get whether/which item is selected from QPoint information? I am on Qt 4.5.3.