nerdoc
nerdoc

Reputation: 1093

PySide & Python3: Slot with QListWidgetItem produces wrong type?

I am sitting in front of a problem which drives me nuts. Maybe it's easy but I can't see the culprit.

I have a simple class which connects the currentItemChanged signal of a QListWidget to a custom slot of the same class. This signal has a (QListWidgetItem*, QListWidgetItem*) signature (there are no overloaded signals with the same name). The Slot only one QListWidgetItem, which should be no problem. See the code snippet:

class Overview(QObject):
    def __init__(self, parent=None)
    #---SNIP---
    item = QListWidgetItem(spec.name)
    item.setData(Qt.UserRole, spec)
    self.ui.listWidget.currentItemChanged.connect(
                    self.showDetails(QListWidgetItem))

    @Slot(QListWidgetItem)
    def showDetails(self, item):
        if item:
            spec = item.data(Qt.UserRole) # <---- PROBLEM

when I run this code, I always get the error message:

---SNIP---
    spec = item.data(Qt.UserRole)
TypeError: descriptor 'data' requires a 'PySide.QtGui.QListWidgetItem' object
but received a 'PySide.QtCore.Qt.ItemDataRole'

I tried everything, but I can't see what is the problem here. When I fill in a print(item) into the slot, it says: <class 'PySide.QtGui.QListWidgetItem'> - this is perfectly ok, so the received "item" is a QListWidgetItem again. The data method call seems right for me - has anyone an idea?

Upvotes: 0

Views: 465

Answers (1)

nerdoc
nerdoc

Reputation: 1093

OMG. I was so busy finding the answer on the slot side that I did not see the forest within all the trees...

The connect signature is wrong. I was calling the slot instead of passing it to the signal handler:

self.ui.listWidget.currentItemChanged.connect(self.showDetails)

solved the problem. Sometimes it's just good to throw a question out into the wild. Helps you to step back and overlook what you did ;-)

Upvotes: 1

Related Questions