Reputation: 367
I stumbled upon this (it is, obviously, an extract from a bigger application):
import sys
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
if __name__ == '__main__':
app = QApplication(sys.argv)
d = {}
widget = QWidget()
d[widget] = 'hashable'
item = QListWidgetItem('abc')
d[item] = 'unhashable'
If you run this, on the last line you get:
TypeError: unhashable type: 'PySide2.QtWidgets.QListWidgetItem'
As far as I can tell any Qt object can be used as dict keys, just like any user-defined class instances.
I'm running PySide2 5.13.0, Python 3.6.4 on Windows 7. I get the same error on Ubuntu 18.04, Python 3.6.9, PySide 5.9.0a1.
Thanks for any hint.
Upvotes: 3
Views: 1034
Reputation: 244162
QListWidgetItem (similar to QTableWidgetItem and QTreeWidgetItem) is not hashtable since a QListWidgetItem associated with a row can change without notification unlike QObjects such as QWidget, QPushButton, etc.
If your goal is to associate information with a QListWidgetItem then you can use the setData()
and data()
methods.
import sys
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QApplication, QListWidget, QListWidgetItem, QWidget
if __name__ == "__main__":
app = QApplication(sys.argv)
w = QListWidget()
for i in range(10):
it = QListWidgetItem("abc-{}".format(i))
it.setData(Qt.UserRole, "data-{}".format(i))
w.addItem(it)
def on_currentItemChanged():
current = w.currentItem()
print(current.data(Qt.UserRole))
w.currentItemChanged.connect(on_currentItemChanged)
w.show()
sys.exit(app.exec_())
Upvotes: 5