I want to know how can i set the custom header names in QTableview
when i create a QTableview i get the column and row header names as 1,2,3,4. I want to know how can i set my own column and header titles.
I got the solution as required, Hope it could help some one who comes across the same situation
If you're using a QTableView
with your own model you need to implement the headerData()
method in the model to return data for the header. Here's a snippet to show just column headings - change the header_labels
value to change the header text.
class TableModel(QAbstractTableModel):
header_labels = ['Column 1', 'Column 2', 'Column 3', 'Column 4']
def __init__(self, parent=None):
QAbstractTableModel.__init__(self, parent)
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.header_labels[section]
return QAbstractTableModel.headerData(self, section, orientation, role)