Tree widget item signals.
-
wrote on 15 Nov 2013, 12:45 last edited by
I'm trying to make doubleclicking items in a tree widget do things. Code below is Python, but this should apply to C++ QT as well.
'itemDoubleClicked' is a signal of tree widget, not of a tree item.
@item = QtWidgets.QTreeWidgetItem(self.ui.tr_owned)
self.ui.tr_owned.itemDoubleClicked.connect(lambda symbol: hist_data.plot(symbol))@
The above works, but isn't specific to the item clicked.The QT documentation lists its parameters as this: 'void QTreeWidget::itemDoubleClicked(QTreeWidgetItem * item, int column) [signal]'
@
item = QtWidgets.QTreeWidgetItem(self.ui.tr_owned)
self.ui.tr_owned.itemDoubleClicked(item, 4).connect(lambda symbol: hist_data.plot(symbol))@
This doesn't work: 'TypeError: native Qt signal is not callable' -
wrote on 15 Nov 2013, 13:36 last edited by
Hi,
Signals are not "real" methods, so you can't call them. To make your thing, one possibility is to implement a class deriving from both QObject and QTreeWidgetItem. Then you could have signals specific to your items and connect them. Another possible way is to connect the signal of the tree widget to a slot filtering the items (the sending item will be the first parameter of the slot).
-
wrote on 17 Nov 2013, 11:07 last edited by
Thank you. Would you elaborate on the second solution? I can't figure out how to determine which item sent the signal.
-
wrote on 17 Nov 2013, 19:39 last edited by
This is how in c++ works, maybe gives an idea:
so define the object:
@treelist = new QTreeWidget(this);@connect the signal to a slot:
@ connect(treelist, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(listSelected(QTreeWidgetItem*, int)));@you can get the item info by column
@void MainWindow::listSelected(QTreeWidgetItem *item, int column) {
QString sitem = item->data(column, Qt::DisplayRole).toString();
...
}@ -
wrote on 19 Nov 2013, 16:37 last edited by
Thank you - I got it working based on y'alls advice. "the sending item will be the first parameter of the slot" was the key bit.
@self.ui.tr_owned.itemDoubleClicked.connect(self.dclick)
def dclick(self, item):
hist_data.chart(item.text(0))@Another working solution involved picking the item that's selected, but this is more elegant, since the treeitem seems to be passed implicitly as the first argument.
5/5