4

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>
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Manuel Zompetta
  • 394
  • 4
  • 17

1 Answers1

4

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • ok, now I've to find the way to add this as first line of an xls file and the adding the data for each row: my final purpose is exporting a table widget in an excel file including vertical and horizontal headers. can you help me? or do I need writing another post? tsk – Manuel Zompetta Jan 02 '19 at 16:15