Manuel Zompetta
Manuel Zompetta

Reputation: 414

getting values of table view headers

I'm not able to access to the values of the headers of a table widget.

I'm able to set them like:

self.table_widget.setHorizontalHeaderLabels(words)

I've tried all the methods of header view object without any positive result.

Printing the header value with:

print(self.tableWidget.verticalHeader())

I obtain the object

<PyQt5.QtWidgets.QHeaderView object at 0x10ebc1798>

Upvotes: 4

Views: 2004

Answers (1)

eyllanesc
eyllanesc

Reputation: 243965

You have to iterate using the horizontalHeaderItem():

labels = []
for c in range(self.tableWidget.columnCount()):
    it = self.tableWidget.horizontalHeaderItem(c)
    labels.append(str(c+1) if it is None else it.text())
print(labels)

Upvotes: 4

Related Questions