QListWidget on click check item type

Viewed 16

I have a QListWidget + click action logic like:

class CustomListWidget(QListWidgetItem):
    def __init__(self):
        QListWidgetItem.__init__(self, "Custom text")

    def custom_method(self, qWidget):
        # do something
# self is QWindow
self.myListWidget1 = QListWidget()

l1 = CustomListWidget()
l2 = QListWidgetItem("Test")

self.myListWidget1.insertItem(1, l1)
self.myListWidget1.insertItem(1, l2)

self.myListWidget1.itemClicked.connect(self.onItemClicked)

# [....]

def onItemClicked(self, item): 
    item.__class__ = CustomListWidget
    item.custom_method(self)

My issue is that inside onItemClicked the item is always QListWidgetItem and I can not separate by type. Is there any way to get/check the class of the item, so that I can cast or just use the custom method if the item was instantiated as CustomListWidget or not?

For example, this won't work before l1 will be ok with the cast toward CustomListWidget but I can't/don't want to do the same for l2

0 Answers
Related