Reputation: 5003
I'm using a QTableView
with PyQt5. As soon as I start to display a few thousand rows, the performance is abysmal. I tried the suggestions from this question but the Qt Graphics View framework and QTreeView
are not viable options for me. Does anybody have other ideas for optimizing the performance of a QTableView
?
Upvotes: 1
Views: 581
Reputation: 36
You can mirror the effect of QTreeView
's setUniformRowHeights
by implementing sizeHintForRow
:
class UniformRowHeights(QTableView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._row_height = None
def sizeHintForRow(self, row):
model = self.model()
if row < 0 or row >= model.rowCount():
# Mirror super implementation.
return -1
return self.get_row_height()
def get_row_height(self):
if self._row_height is None:
self._row_height = max(self._get_cell_heights())
return self._row_height
def changeEvent(self, event):
# This for instance happens when the style sheet changed. It may affect
# the calculated row height. So invalidate:
self._row_height = None
super().changeEvent(event)
def _get_cell_heights(self, row=0):
self.ensurePolished()
option = self.viewOptions()
model = self.model()
for column in range(model.columnCount()):
index = model.index(row, column)
delegate = self.itemDelegate(index)
if delegate:
yield delegate.sizeHint(option, index).height()
Upvotes: 2