Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable/disable a item in a QTreeView eventbased?

I want to enable/disable a checkable item in a QTreeWidget, when a specific signal is sent.

The following code dows not work:

model = QStandardItemModel()
view = QTreeView()
view.setModel(model)
rootItem = QStandardItem()
rootItem = model.invisibleRootItem()

categoryItem = QStandardItem(item)
categoryItem.setCheckable(True)
rootItem.appendRow(categoryItem)

signalSource.availabilityChanged.connect(categoryItem.setEnabled)

It produces the error:

TypeError: unhashable type: 'PySide.QtGui.QStandardItem'

Is there a solution for changing the state or data of a QStandardItem via signal/slot?

like image 386
leviathan Avatar asked Dec 05 '25 05:12

leviathan


1 Answers

This looks like a bug in PySide, as connect should accept any callable (the example code works correctly in PyQt4).

As a workaround, try wrapping QStandardItem methods in a lambda:

signalSource.availabilityChanged.connect(
    lambda enable: categoryItem.setEnabled(enable))

EDIT

To connect the items in a loop, use a default argument, like this:

for button in buttonList:
    item = QStandardItem("Test")
    ...
    button.toggled.connect(
        lambda enable, item=item: item.setEnabled(enable))
like image 197
ekhumoro Avatar answered Dec 07 '25 19:12

ekhumoro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!