0

The solution given at the related question removes the grey band show in the image given. If you change QtWidgets.QHeaderView.Stretch into QtWidgets.QHeaderView.Fixed as done in below script this grey band is back and cells are fixed in either vertical or horizontal manner.

What desired is removing the grey band without resizing cell height or width. here

Note 1: original image is from user xwsz.

The code:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt


class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, data):
        super(TableModel, self).__init__()
        self._data = data

    def data(self, index, role):
        if role == Qt.DisplayRole:
            return self._data[index.row()][index.column()]

    def rowCount(self, index):
        return len(self._data)

    def columnCount(self, index):
        return len(self._data[0])
    
    def headerData(self, section, orientation, role):
        # section is the index of the column/row.
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                column_label = ['A', 'B1', 'B2']
                return column_label[section]

            if orientation == Qt.Vertical:
                row_label = ['Device', 'ID', 'Operation', 'Weigth', 'Row 5', 'No Show']
                return row_label[section]


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.table = QtWidgets.QTableView()

        data = [
          [4, 9, 2],
          [1, 0, 0],
          [3, 5, 0],
          [3, 3, 2],
          [7, 8, 9],
        ]
        
        self.model = TableModel(data)
#        self.table.verticalHeader().setDefaultSectionSize(50) # does not sove it.

#        self.table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
        
#        self.table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.table.setModel(self.model)

        self.setCentralWidget(self.table)


app=QtWidgets.QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec_()

Note 2: the original code from Martin Fitzpatrick can be found here and is modified here to show the issue.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ZF007
  • 3,708
  • 8
  • 29
  • 48

1 Answers1

2

The solution is to create a QHeaderView where the part that has no content was painted:

class VerticalHeaderView(QtWidgets.QHeaderView):
    def __init__(self, parent=None):
        super().__init__(QtCore.Qt.Vertical, parent)

    def paintEvent(self, event):
        super().paintEvent(event)
        h = self.offset()
        for i in range(self.count()):
            h += self.sectionSize(i)
        if h < self.rect().bottom():
            r = QtCore.QRect(self.rect())
            r.moveTop(h)
            painter = QtGui.QPainter(self.viewport())
            painter.fillRect(r, QtGui.QColor("white"))
header = VerticalHeaderView(self.table)
self.table.setVerticalHeader(header)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks, this did partly the trick. Posted solution works when header = VerticalHeaderView(self.table).. etc. is put before self.table.verticalHeader().setStyleSheet("border: 1px; border-right: 1px solid black;"). I did the same for HorizontalHeader but then the solid line doesn't show. The other way around works too but then the solid line continues forever. So I'm guess this then should be done by coloring left border of cell in column zero? Or left border of column zero. – ZF007 Oct 26 '20 at 09:53