csvan
csvan

Reputation: 9454

PySide - Get list of all visible rows in a table

Given that I have an instance of QTableView (or a subclass thereof), connected to a subclass of QAbstractTableModel (or functionally equivalent model + view), is it possible to get a list of the indexes of all rows currently visible to the user (i.e. those not falling outside the current scroll range)?

It would be great if the solution scales to different window/screen sizes.

Upvotes: 1

Views: 1409

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40492

You can obtain the item position using QAbstractItemView::visualRect. It is in the viewport coordinates, so we need to check if it is in the viewport rect. Here is an example:

viewport_rect = QRect(QPoint(0, 0), self.view.viewport().size())
for row in range(0, self.model.rowCount()):
  rect = self.view.visualRect(self.model.index(row, 0))
  is_visible = viewport_rect.intersects(rect)

This example works only with one column, but you can add a for loop for iterate over all columns.

In this code items are considered visible if they are partially visible. If you want to get only items that are completely visible, use contains instead of intersects.

Upvotes: 2

Related Questions