仅QTreeView选择的QT上下文菜单

时间:2015-05-26 12:15:30

标签: c++ qt

我有一个上下文菜单,我只想在QTreeView中的项目顶部出现。什么时候在空白的顶部,我什么都不想做。 这就是我现在所拥有的

    http://mirror.ops.rhcloud.com/nexus/content/groups/public/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar (422 KB at 2600.6 KB/sec)
[INFO] Packaging webapp
[INFO] Assembling webapp [SpringMVC6] in [/var/lib/openshift/55644f6ee0b8cd41470000de/app-root/runtime/repo/target/Mazurov20]
[INFO] Processing war project
[INFO] Copying webapp resources [/var/lib/openshift/55644f6ee0b8cd41470000de/app-root/runtime/repo/src/main/webapp]
[INFO] Webapp assembled in [391 msecs]
[INFO] Building war: /var/lib/openshift/55644f6ee0b8cd41470000de/app-root/runtime/repo/deployments/ROOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 38.069s
[INFO] Finished at: Tue May 26 07:22:48 EDT 2015
[INFO] Final Memory: 15M/131M
[INFO] ------------------------------------------------------------------------
Preparing build for deployment
Deployment id is ae0b5aa5
Activating deployment
Starting MySQL 5.5 cartridge
Starting PHPMyAdmin cartridge
Starting jbossews cartridge
Found 127.10.169.129:8080 listening port
-------------------------
Git Post-Receive Result: success
Activation status: success
Deployment completed with status: success

谢谢!

1 个答案:

答案 0 :(得分:6)

首先,您需要使用QAbstractItemView::indexAt()函数在光标下找到模型索引。获取无效索引将指示您单击任何树视图项。所以,你的代码看起来像:

void MainTreeViewController::showContextMenu(const QPoint& pos)
{
    // Do not show menu if clicked outside of tree view nodes.
    QModelIndex idx = mtreeView->indexAt(pos);
    if (!idx.isValid())
        return;

    QPoint globalPos = mtreeView->mapToGlobal(pos);
    QMenu rightClickMenu;
    for(int i = 0; i < kCharModelRightClickOptionsCount; ++i){
        rightClickMenu.addAction("Menu option");
    }

    QAction* selectedItem = rightClickMenu.exec(globalPos);
    if (selectedItem){

    }
}