I have a dictionary of tuple:name pairs. I display the names in a QListWidget, but I retrieve and work with the tuples -- the names are strictly for display purposes. The setFlags line in the code below allows me to edit the display names, but what I actually want to be able to do is edit the tuple data. I tried adding the Qt.EditRole bit, but ended up with nothing but blank lines in the QListWidget for my trouble. How can I let the user edit the tuple data (via user interface) and then have my code query the dictionary to update the display name?
for tuple in tuples:
name = dict[tuple]
this_item = QListWidgetItem(name)
this_item.setData(Qt.UserRole,tuple)
# this_item.setData(Qt.EditRole, tuple)
this_item.setFlags(this_item.flags() | Qt.ItemIsEditable)
self.addItem(this_item)
To edit the data behind the name, I'll add a method within my QListWidget that creates a custom editing environment:
def edit_items(self):
dialog = MyQDialog(self.parent())
table = QTableWidget(self.count(),2,dialog)
for row in range(0, self.count()):
spec = repr(self.item(row).data(32).toPyObject())
name = self.item(row).text()
spec_item = QTableWidgetItem(spec)
name_item = QTableWidgetItem(name)
table.setItem(row,0,name_item)
table.setItem(row,1,spec_item)
layout = QHBoxLayout()
layout.addStrut(550)
layout.addWidget(table)
dialog.setLayout(layout)
dialog.show()
and then go from there.