-2

My issue is similar to PyQt - setText method of QTableWidget gets AttributeError. However, in this case the solution doesn't work.

I'm attempting to rewrite some fields automatically in which the values to be set are held in an array. As I have some empty rows I iterate over the rows to ensure that the name is relevant to the value that will be set.

    #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

My issue is after the first 3 rows (Tackles, Blocks and Interceptions), I receive an attribute error:

AttributeError: 'NoneType' object has no attribute 'setText'

The fields that I am attempting to modify are empty, so naturally they will be None. But why is it that I can edit the first 3 values but not the rest?

Awais Shah
  • 17
  • 1
  • 6

1 Answers1

0

Items in a QTableWidget are always None until any data (or QTableWidgetItem) is set for them.

Just add a check to ensure that the item exists, and if it doesn't, create it and set it for the table:

    item = modelTable.item(row, 0)
    if not item:
        item = QtWidgets.QTableWidgetItem()
        modelTable.setItem(row, 0, item)
    item.setText(str(statsGroup[counterTracker]))
musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Thanks for the reply, I'll try this in the morning. I think I tried something similar by checking if it was equal to None but no luck. – Awais Shah Mar 04 '21 at 01:09