[Interest] Add context menu to QTreeWidget based on Specific Item being clicked

Andreas Pakulat apaku at gmx.de
Fri May 10 22:48:35 CEST 2013


On Fri, May 10, 2013 at 10:06 PM, Michael Jackson <imikejackson at gmail.com>wrote:

> I am trying to add a "right click" or context menu to a QTreeWidget where
> the menu that gets popped up has menus that are specific to the item being
> clicked on. So far all my attempts have failed.
> I have tried to add my higher level QWidget class (The parent of the
> QTreeWidget) as an Event Filter for the QTreeWidget but I am not receiving
> the mouse clicked events. I tried adding the QAction to the QTreeWidgetItem
> directly by getting the QWidget from the QTreeWidgetItem while it is being
> constructed but at that point in the code the QTreeWidgetItem does not have
> a QWidget yet so that does not work.
> I am hoping it is something simple that I am just missing. Any help would
> be greatly appreciated.

Your three options are:

a) override contextMenuEvent in the QTreeWidget and map the x/y position to
an item using QTreeWidget::itemAt() (make sure to keep the default context
menu policy on the QTreeWidget

b) add QAction's to the QTreeWidget and set the contextMenuPolicy to
Qt::ActionsContextMenu, then fetch the list of selected items from the
TreeWidget in the actions triggered-slots using QTreeWidget::selectedItems

c) set the contextMenuPolicy to Qt::CustomContextMenu and connect yourself
to the 'customContextMenuRequested' signal, then again map from the given
point to the item using QTreeWidget::itemAt()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130510/4b3a302e/attachment.html>

More information about the Interest mailing list