PDA

View Full Version : How to pass a TreeWidgetItem in QAction trigger signal/slot



chithara
9th May 2019, 13:24
Hi,

I created a right click menu option for a QTreeWidgetItem using CustomContextMenu. Under this, created a QAction ,uopn which I need to pass the currentTreeItem in signal triggered(). How can i achieve that?

Found that we can call QAction->setData().. But not sure, how to convert QTreeWidgetItem to a QVariant ?

Any suggestion please

d_stranz
10th May 2019, 01:53
How are you using your QTreeWidget? Is it part of a larger composite QWidget of some sort? The easiest thing to do is to handle the triggered() signal in a slot for that composite widget, and once in the slot use the QTreeWidget pointer to retrieve the current item. (QTreeWidget::currentItem())

If you are handling the triggered() signal in a different class from the one that contains the QTreeWidget, then you can add a slot to that class and connect it to the QTreeWidget::currentItemChanged() signal and save the pointer to the current item in a member variable. You use that later when handling the QAction::triggered() signal.

chithara
10th May 2019, 08:44
Thanks very much :)
As my QAction and QTreeWidget are in same class, I followed the first approach where I am using (QTreeWidget::currentItem()) in the triggered() slot to retrieve the current item.