How to find item selected from customContextMenuRequested() on QTreeView item?

Posted by vinaym on Stack Overflow See other posts from Stack Overflow or by vinaym
Published on 2010-04-19T09:18:03Z Indexed on 2010/04/19 10:13 UTC
Read the original article Hit count: 288

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about qt

Related posts about qt4