Tree widget item signals.



  • 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'



  • 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).



  • Thank you. Would you elaborate on the second solution? I can't figure out how to determine which item sent the signal.



  • 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();
    ...
    }@



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.