QTreeWidget right click menu

后端 未结 4 1758
离开以前
离开以前 2021-02-03 11:31

I looked around and it seems that the problem is present not only for tree widget but also for other widgets. But in my case, I found a solution, although an incomplete one. I a

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-03 12:16

    Take a look at overloading QAbstractItemModel and providing your own OnContextMenuRequested. Via this function you can have different items create different context menus.

    Here's some shortened pseudo-ish code from one of my projects that may be helpful:

    void MyModel::OnContextMenuRequested(const QModelIndex& index, const QPoint& globalPos)
    {
    // find 'node' corresponding to 'index'
    
    vector > actions = node->GetActions(true);
    if(actions.size()==0) return;
    
    // the ptr list helps us delete the actions
    boost::ptr_list actionPtrList;
    QList qtActions;
    for(unsigned int i=0;isetData(qVariantFromValue(actions[i].second));
        actionPtrList.push_back(act);
        qtActions.append(act);
    }
    
    // create and show the context menu
    QMenu *menu = new QMenu("Item actions",NULL);
    actionPtrList.push_back(menu);
    QAction* act = menu->exec(qtActions,globalPos);
    if(act==NULL) return;
    
    // act on the resulting action 'act'
    }
    

提交回复
热议问题