QTableWidgetItem.data(self, int *role*) 中的 "role" 参数是什么?

What is the "role" argument in QTableWidgetItem.data(self, int *role*)?

我正在查看文档:

我可以将行 IDList.append(item.data()) 放入我的代码中,并将其打印为正确的值。但奇怪的是,在那一行之后,它给了我这个错误:

TypeError: QTableWidgetItem.data(int): not enough arguments

我不知道为什么错误消息出现在打印行之后,但我认为这不重要。文档中 "int role" 是什么意思?能举个例子吗?

一个项目有多种与之关联的数据,例如文本、字体、背景、工具提示等。roleItemDataRole enum 中的一个值,允许您指定您想要的数据类型。

其中一些数据项还具有访问器函数。所以这两行代码是等价的:

font = item.font()
font = item.data(QtCore.Qt.FontRole)

数据角色是可扩展的。您可以使用以 Qt.UserRole 开头的任何值将自定义角色与您自己的数据相关联:

MyRole = QtCore.Qt.UserRole + 2
item.setData(MyRole, [1, 2, 3])
...
data = item.data(MyRole)