QTableWidget setText返回属性错误

2024-04-25 14:33:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题类似于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个值而不能编辑其余的值呢


Tags: ofthethatisarraymethodrowattributeerror
1条回答
网友
1楼 · 发布于 2024-04-25 14:33:56

QTableWidget中的项始终是None,直到为它们设置了任何数据(或QTableWidgetItem)

只需添加一个检查以确保该项存在,如果不存在,则创建该项并为表设置它:

    item = modelTable.item(row, 0)
    if not item:
        item = QtWidgets.QTableWidgetItem()
        modelTable.setItem(row, 0, item)
    item.setText(str(statsGroup[counterTracker]))

相关问题 更多 >