模型有不同
item data roles
对于不同的数据。有一个
Qt::DisplayRole
,
Qt::EditRole
和A
Qt::UserRole
在其他中。在本例中,您希望显示与实际数据不同的内容,因此可以添加一个新角色,例如
Qt::UserRole+1
用于索引。
然后,您希望您的代理在
setModelData
:
def setModelData(self, editor, model, index):
cbIndex = editor.currentIndex()
model.setData(index, cbIndex, Qt.UserRole+1)
# we want a nice displayable though
model.setData(index, countryIndexToDisplayable(cbIndex), Qt.DisplayRole)
当然,您将以类似的方式检索要在编辑器中设置的数据:
def setEditorData(self, widget, index):
widget.setCurrentIndex(index.data(Qt.UserRole+1))
根据您的模型和视图,您可能可以使用
QT::编辑角色
这是为了这个目的。如果您随后使用本地类型作为显示角色,则不需要进行任何自定义绘制,但如果您愿意,也可以这样做。