A.
QComboBox
总是
使用模型存储其数据。如果你不自己设置模型,它会创建自己的模型
QStandardItemModel
. 方法,例如
addItem
和
itemData
只需使用任何已设置的底层模型存储和检索值。默认情况下,组合框使用
Qt.UserRole
def data(self, index, role):
row = index.row()
column = index.column()
if role == QtCore.Qt.DisplayRole and column == 0:
project = self._projects[row]
name = project.name()
return name
elif role == QtCore.Qt.UserRole and column == 0:
project = self._projects[row]
id = project.id()
return id
def setData(self, index, value, role):
row = index.row()
column = index.column()
if role == QtCore.Qt.UserData and column == 0:
project = self._projects[row]
project.setId(value) # or whatever