QTableView.verticalHeader().setOffset() doesn't work

Matej Brezović matey.brezovic at gmail.com
Sun Mar 20 16:13:13 GMT 2022


Here is the MRE. If you replace view.verticalHeader() with
view.horizontalHeader(), the offset will be visible, but doing it with
verticalHeader doesn't seem to affect the table view at all.

from typing import Any, Optional

from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QWidget, QHBoxLayout,
QApplication, QTableView


class Model(QtCore.QAbstractTableModel):
    def __init__(self, parent: Optional[QWidget] = None) -> None:
        super(Model, self).__init__(parent)
        self.__data = []
        for i in range(10):
            row = [0, 1, 2, 3, 4, 5, 6, 7]
            self.__data.append(row)

    def rowCount(self, index: Optional[QtCore.QModelIndex] = None) -> int:
        return len(self.__data)

    def columnCount(self, index: Optional[QtCore.QModelIndex] = None) -> int:
        return len(self.__data[0])

    def data(self, index: QtCore.QModelIndex,
             role: QtCore.Qt.ItemDataRole = Qt.ItemDataRole.DisplayRole) -> Any:
        if role in [Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole]:
            return self.__data[index.row()][index.column()]
        return None


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(700, 700)

        self.central_widget = QWidget()
        self.central_widget_layout = QHBoxLayout(self.central_widget)

        view = QTableView()
        model = Model()
        view.setModel(model)
        view.verticalHeader().setOffset(-40)

        self.central_widget_layout.addWidget(view)
        self.setCentralWidget(self.central_widget)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec())


<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon>
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Sun, Mar 20, 2022 at 4:54 PM Maurizio Berti <maurizio.berti at gmail.com>
wrote:

> Can you provide a MRE to show how you tried to set the offset?
> Remember that the offset of headers is always updated whenever *any*
> change would result in a call to updateGeometries() (model size/layout
> changes), and, obviously, whenever scroll bars are updated (value/range
> change), including changes caused by the view receiving a resize event.
>
> Maurizio
>
> Il giorno dom 20 mar 2022 alle ore 14:13 Matej Brezović <
> matey.brezovic at gmail.com> ha scritto:
>
>> Tried setting offset for verticalHeader of QTableView in both PyQt5
>> (5.15.2) and PyQt6 (6.2.2), it doesn't do anything. Works for horizontal
>> headers but not for vertical ones.
>> I can't confirm if this is a problem just with PyQt or with Qt as well
>> because I don't use C++.
>>
>>
>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon> Virus-free.
>> www.avast.com
>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link>
>> <#m_379014115603901552_m_6015398719878155313_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>>
>
>
> --
> È difficile avere una convinzione precisa quando si parla delle ragioni
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
> http://www.jidesk.net
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20220320/a97b257e/attachment-0001.htm>


More information about the PyQt mailing list