J
J
Jek2021-06-21 14:35:51
PyQt
Jek, 2021-06-21 14:35:51

How to set header in QtCore.QAbstractTableModel?

Hello. Created a data model from QtCore.QAbstractTableModel to display using QtableView.setModel how to set a horizontal header? I have never worked with a header.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jek, 2021-06-22
@jotrib

It is necessary to implement 2 methods in the tabular data model class and call setHeaderData by inserting a list with column names (ATTENTION: the number of names must match the number of columns in the data)

spoiler
def setHeaderData (self, int, Orientation , Header: list, role: int = Qt.ItemDataRole.EditRole):
        self.header = Header

    def headerData(self, section, orientation, role):
        if role == QtCore.Qt.DisplayRole:
            if orientation == QtCore.Qt.Horizontal:
                return self.header[section]
            if orientation == QtCore.Qt.Vertical:
                return str("List ") + str(section)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question