1

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.

4

1 に答える 1

2

を使用してアイテムの位置を取得できますQAbstractItemView::visualRect。これはビューポート座標にあるため、ビューポート rect にあるかどうかを確認する必要があります。次に例を示します。

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)

この例は 1 つの列でのみ機能しますが、forすべての列を反復処理するためのループを追加できます。

このコードでは、項目が部分的に表示されている場合、項目は表示されていると見なされます。完全に表示されているアイテムのみを取得する場合は、contains代わりに を使用しintersectsます。

于 2013-07-11T22:32:29.407 に答える