0

I want to add context menu to a tree widget for one of my project: I want that, when the user clicks in one of the item, a context menu appears giving the user the opportunity to copy that item and paste it in another position.

Searching in the Qt Assistant I came to find there is an easy way to add context menus to QTreeWidgets:

treeView->setContextMenuPolicy(Qt::ActionsContextMenu);
treeView->addAction(yourAction1);

The problem with this method is that it makes the context menu available even when there is no items in the tree widget not to mention the fact that identifying which was the item clicked is not straightfoward.

After some research I discovered there are some ways of doing what I want. For instance this and this (which are essentially the same) as well as this give some idea (and this as well). The problem is I disliked those solutions and I'm curious as to if there is no other way known to implement what I want in a more "direct" way such as what is available for QTreeWidget as mentioned at the top. Notice I'm here considering Qt 5.4 and forward.

ymoreau
  • 3,402
  • 1
  • 22
  • 60
Momergil
  • 2,213
  • 5
  • 29
  • 59
  • This question really is a duplicate of http://stackoverflow.com/q/14237020/1329652 Just because you dislike the answers doesn't mean you should ask again. – Kuba hasn't forgotten Monica Jul 28 '15 at 14:26
  • 1
    The answer to the original of your question presents a fully fleshed out solution. What you can definitely do is factor it out into a `QIdentityProxyModel`, and add methods there that make setting per-item context menus simple. For example, the proxy could check if an item has any actions added as a custom role. You could wrap them in a custom value type that can hold pointer collections, like `QList>` - this could then be set as a custom `ActionList` role on the item. The proxy would pick it up and act on it. – Kuba hasn't forgotten Monica Jul 28 '15 at 14:30

0 Answers0