简体   繁体   中英

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 (ie those not falling outside the current scroll range)?

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM