[Interest] QTreeView and context menu

Graham Labdon Graham.Labdon at avalonsciences.com
Thu Feb 27 16:31:14 CET 2014


Thanks for the hints

From: Clément Geiger [mailto:clement.geiger at gmail.com]
Sent: 27 February 2014 13:57
To: Graham Labdon
Cc: Interest at qt-project.org
Subject: Re: [Interest] QTreeView and context menu

Hello Graham,
You can use the contextMenuPolicy Qt::CustomContextMenu. In that case, when your QTreeView receives a right-click, it sends a signal contextMenuRequested(QPoint) that you can connect to a slot. In that slot, you are able to know which item has been clicked through QTreeView::indexAt(QPoint). You can then compose your context menu (use QMenu) as needed, and show it with QMenu::exec().
If you already subclassed QTreeView for a reason or another, there is another way: leave the contextMenuPolicy as is and reimplement the contextMenuEvent method. From there on it's the same as with the 1st method (retrieve clicked index, compose menu and execute it).
Cheers

Clément

2014-02-27 9:09 GMT+01:00 Graham Labdon <Graham.Labdon at avalonsciences.com<mailto:Graham.Labdon at avalonsciences.com>>:
Hi
I have developed a class derived from QAbstractItemModel and am using it to display data in a QTreeView.
I want to display a context menu when the user right clicks on an item in the tree. The contents of this menu are dependent on which item in the tree has bee clicked and the result of the click will result in an additional row being inserted into the model.
I would be grateful of some guidance of how to achieve this.

Thanks

_______________________________________________
Interest mailing list
Interest at qt-project.org<mailto:Interest at qt-project.org>
http://lists.qt-project.org/mailman/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140227/17912fc7/attachment.html>


More information about the Interest mailing list