qtpopupmenuqtreewidget

QTreeWidget right click menu


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 am adding actions to my tree widget, so that when you right click on it, a popup with these actions appears. However, when I add items to my tree widget and I right click on them, the same popup appears. What I would like to do is that when you right click on the tree widget, a tree widget popup menu appears and when you right click on items, another corresponding popup menu appears. Does anybody knows how to do this?


Solution

  • First you should set the context menu policy to CustomContextMenu:

    treeView->setContextMenuPolicy(Qt::CustomContextMenu);
    

    Then you can connect to the QWidget::customContextMenuRequested(const QPoint&) signal and show your context menu.