Search code examples
pythonpyqtpyqt5qtablewidget

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>

Solution

  • 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)