1

I am developing a tool is Qgis3 using PyQt5. I am adding columns using a button and the setting column names using 'setHorizontalHeaderItem'. But it gives None while trying to read it. Following is the code.

    count=self.dlg.tableWidget.columnCount()
    self.dlg.tableWidget.setColumnCount(count+1)
    header= QTableWidgetItem(str(count+1))
    self.dlg.tableWidget.setHorizontalHeaderItem(count+1,header)
    self.dlg.lineEdit_4.setText(self.dlg.tableWidget.horizontalHeaderItem(count+1).text())

it gives an error- 'AttributeError: 'NoneType' object has no attribute 'text' ' I can't identify the mistake.

0

1 Answer 1

1

Try changing the following 2 lines, passing count as the column index instead of count+1: E.g.

self.dlg.tableWidget.setHorizontalHeaderItem(count,header)
self.dlg.lineEdit_4.setText(self.dlg.tableWidget.horizontalHeaderItem(count).text())

Because when you get your column count, if your table has, for example 3 columns, the index of the last column would be 2 (since indexing begins at 0). After you add another column, the index of the new column will be 3, which is your original count value.

You can see that in this minimal example:

tw = QTableWidget(3, 3)
items = [['r1c1', 'r1c2', 'r1c3'],
         ['r2c1', 'r2c2', 'r2c3'],
         ['r3c1', 'r3c2', 'r3c3']]
         
row = 0
for item in items:
    col = 0
    for cell in item:
        tw.setItem(row, col, QTableWidgetItem(items[row][col]))
        col += 1
    row+=1

for i in range(tw.columnCount()):
    tw.setHorizontalHeaderItem(i, QTableWidgetItem(f'Column {i+1}'))

count = tw.columnCount()
tw.setColumnCount(count+1)
header= QTableWidgetItem(f'Column {count+1}')
tw.setHorizontalHeaderItem(count, header)
    
tw.show()

print(count)
print(tw.horizontalHeaderItem(count).text())

When run in the QGIS Python console, the result is:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.