PDA

View Full Version : PYQT5 QTableWidget Retrieving values from cells



trevor44
28th July 2019, 19:00
I'm using PYQT5 and trying to loop though the rows in my table to retrieve the values in cells adding them to a list. Below is one of the many versions of the code I've written.




def grouping(self):
nrows = self.Table.rowCount()
group = []

for row in range(0,nrows):
item = self.Table.item(row, 2)
item_text = item.text()

group.append(item)

print(group)



If I don't use the "item_text = item.text()" line my list only contains the qtablewidgetitem objects. If I try to use the "item_text = item.text()" line or even add ".text()" to the item= line I get an error saying "AttributeError: 'NoneType' object has no attribute 'text'". Any help would be greatly appreciated.

Added after 13 minutes:

I should have also mentioned I'm setting up the table using the setItem function. The items are numbers defaulted as 1, that the user can edit if they wish to regroup the rows.

anda_skoa
28th July 2019, 20:42
Are you sure you are getting valid table widget item objects back?

The error sounds as if the item(row, column) function does not actually return anything, e.g. no such column or similar access error.

Cheers,
_

trevor44
28th July 2019, 22:11
anda_skoa - I think you are correct. I believe I made an elementary mistake when setting up my loop now that I look back at my code in it entirety. I will take a closer look when I return home tonight. Thank you.