如何解决QTableWidget setText 返回属性错误
我的问题类似于PyQt - setText method of QTableWidget gets AttributeError。但是,在这种情况下,解决方案不起作用。
我正在尝试自动重写一些字段,其中要设置的值保存在数组中。 由于我有一些空行,我遍历这些行以确保名称与将要设置的值相关。
#statsPlayer is a class that holds the array through a getter method
#modelTable is the QTableWidget
#possibilities is the array that holds the names of the rows that are to be modified
possiblities = ['Tackles','Blocks','Interceptions','Passes','Succesful dribbles','Fouls Drawn','Goals','Assists','Penalties scored']
counterTracker = 0
statsGroup = statsPlayer.statsGetter()[0]
for row in range(modelTable.rowCount()):
if modelTable.verticalHeaderItem(row).text() in possiblities:
modelTable.item(row,0).setText(str(statsGroup[counterTracker]))
counterTracker += 1
我的问题是在前 3 行(铲球、拦截和拦截)之后,我收到一个属性错误:
AttributeError: 'nonetype' object has no attribute 'setText'
我试图修改的字段是空的,所以自然是无。但是为什么我可以编辑前 3 个值而不能编辑其余的值?
解决方法
QTableWidget 中的项目总是 None
,直到为它们设置任何数据(或 QTableWidgetItem)。
只需添加一个检查以确保该项目存在,如果不存在,则创建它并将其设置为表格:
item = modelTable.item(row,0)
if not item:
item = QtWidgets.QTableWidgetItem()
modelTable.setItem(row,item)
item.setText(str(statsGroup[counterTracker]))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。