Python unhashable type: QTreeWidgetItem

219 Views Asked by At

I'd like to reverse my key and value in a dict. Item numbers are created as QTreeWidgetItem, and dataBlock#s are the data object for the item number.

my_dict = { dataBlock1:item1, dataBlock2:item2, dataBlock3:item3}

inv_dict = {v:k for k,v in my_dict.items()}

I got the error:

TypeError: unhashable type: 'QTreeWidgetItem'

How do I reverse the key & value in this case. I need item numbers as keys in the inv_dict for another use purpose.

1

There are 1 best solutions below

0
Nima On

Since items are unhashable, one solution is that store each item in a list and use its index as the value in my_dict. Now you have integers as values and you can reverse the key values. Another way that I'm not sure about that, you can implement __hash__ function for your item's class.