Eleftheria
Eleftheria

Reputation: 143

How to read and get the values from a QTableWidget using python?

I have a tablewidget where the user should enter numerical values - the user should be unable to enter text.

Then, I would like to read and store these values in a list.

mytable = self.dlg.tableWidget
lista = []
listb = []
# i is always in range 4 in my code
for i in range(4):
    # j is always the length of the unique values list of a field of a qgis layer,
    # selected by the user on a previous step
    for j in range(un_values_len):
        a_item = mytable.item(i, j)
        a_name = str(a_item.text())
        lista.append(a_item)
        listb.append(a_name)


    print lista, listb

When I run the code I get the following error

'NoneType' object has no attribute 'text'

Upvotes: 1

Views: 9993

Answers (1)

Peter Wang
Peter Wang

Reputation: 1838

If the user enters text into the table through a widget like lineEdit, which you might be doing because you check if it's a number or not, you have to get the text through cellWidget, not item.

Try this:

a_name = mytable.cellWidget(i, j).currentText()

Upvotes: 3

Related Questions