如何在QTreeView项目中找到从customContextMenuRequested()中选择的项目?

vin*_*aym 4 qt qt4

我已使用以下代码在QTreeView项目中实现了上下文菜单

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));
}
Run Code Online (Sandbox Code Playgroud)

调用MyDerivedQAbstractItemModel :: contextualMenu(),我可以看到上下文菜单.

问题是只有当用户右键单击某个项目并且应根据所选项目进行自定义时,才能看到上下文菜单.

如何从QPoint信息中选择/选择哪个项目?我在Qt 4.5.3.

Lei*_*iaz 13

在构建自定义菜单之前,您可以使用QTreeView的indexAt()方法获取点击的项目.