Python Forum
[PyQt] QTableView set header labels
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QTableView set header labels
#3
Problem solfed in table format class:
# table format class
class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, data):
        super(TableModel, self).__init__()
        self._data = data
        #self.rows_nr, self.columns_nr = data.shape
        self.hheaders = ["ID", "Datum", "Gewicht", "Bmkg"]           # <<<<<<<<<<<<<<< NEW LINE

    def data(self, index, role):

        if role == Qt.DisplayRole:
            value = self._data[index.row()][index.column()]

            if index.column() == 2:    # Betrag
                return "%.1f" % float(value)
            #elif index.column() == 1:  # Datum
            #    return value.strftime("%Y-%m-%d")
            else:
                return value

        if role == Qt.TextAlignmentRole:
            value = self._data[index.row()][index.column()]

            if index.column() == 0 or index.column() == 2:   # ID, Betrag
                return Qt.AlignmentFlag.AlignVCenter + Qt.AlignmentFlag.AlignRight
            else:
                return Qt.AlignmentFlag.AlignVCenter


    def rowCount(self, index):
        # The length of the outer list.
        return len(self._data)

    def columnCount(self, index):
        # The following takes the first sub-list, and returns
        # the length (only works if all rows are an equal length)
        return len(self._data[0])

    def headerData(self, section, orientation, role):           # <<<<<<<<<<<<<<< NEW DEF
        # row and column headers
        if role == QtCore.Qt.DisplayRole:
            if orientation == QtCore.Qt.Horizontal:
                return self.hheaders[section]
        return QtCore.QVariant()
Reply


Messages In This Thread
QTableView set header labels - by HeinKurz - Jan-21-2023, 11:10 AM
RE: QTableView set header labels - by Axel_Erfurt - Jan-21-2023, 12:37 PM
RE: QTableView set header labels - by HeinKurz - Jan-23-2023, 08:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 QTableView SQLite : edit cell HeinKurz 2 2,426 Mar-27-2023, 10:41 AM
Last Post: HeinKurz
  [PyQt] QStyledItemDelegate and QTableView malonn 1 1,662 Feb-07-2023, 07:15 PM
Last Post: malonn
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 997 Oct-27-2022, 09:29 PM
Last Post: random_nick
  [PyQt] QTableView: scroll to top cell of the screen random_nick 2 2,877 Oct-08-2022, 12:29 AM
Last Post: random_nick
  [PyQt] [Solved]Add a Blank Row To QTableView Extra 3 5,589 Oct-02-2022, 04:53 PM
Last Post: Extra
  How to update the list of a combo box in a QTableView panoss 10 6,276 Feb-05-2022, 03:24 PM
Last Post: panoss
  [PyQt] How to Copy-Paste a table from Office apps to QTableView? Vittorio 5 7,285 Aug-05-2021, 11:14 AM
Last Post: Axel_Erfurt
  [PyQt] Qtableview adapte size to WBPYTHON 3 11,348 Mar-23-2020, 01:51 AM
Last Post: deanhystad
  [PyGUI] Showing text in QTableView sequence 0 3,089 Jan-20-2019, 05:00 PM
Last Post: sequence
  [PySide2][PyQt5] update QTableView cpuin 0 5,276 Mar-07-2018, 10:20 PM
Last Post: cpuin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020