99
1010# SOURCE: https://github.com/gil9red/SimplePyScripts/blob/f49a0c3462176ccc34bf31dffbe6fd88d1baa0bd/qt__pyqt__pyside__pyqode/lazy__qtwidgets_itemviews_fetchmore_example__QAbstractListModel.py#L19
1111class FileListModel (QAbstractListModel ):
12- numberPopulated = pyqtSignal (int )
12+ numberPopulated = pyqtSignal (int , int , int )
1313
1414 def __init__ (self , batch_size = 50 , parent = None ):
1515 super ().__init__ (parent )
@@ -21,11 +21,15 @@ def __init__(self, batch_size=50, parent=None):
2121 def rowCount (self , parent : QModelIndex = None ) -> int :
2222 return self .current_file_count
2323
24+ @property
25+ def total_file_count (self ) -> int :
26+ return len (self .total_file_list )
27+
2428 def data (self , index : QModelIndex , role = Qt .DisplayRole ) -> QVariant :
2529 if not index .isValid ():
2630 return QVariant ()
2731
28- if index .row () >= len ( self .total_file_list ) or index .row () < 0 :
32+ if index .row () >= self .total_file_count or index .row () < 0 :
2933 return QVariant ()
3034
3135 if role == Qt .DisplayRole or role == Qt .ToolTipRole :
@@ -41,29 +45,33 @@ def data(self, index: QModelIndex, role=Qt.DisplayRole) -> QVariant:
4145 return QVariant ()
4246
4347 def canFetchMore (self , parent : QModelIndex = None ) -> bool :
44- return self .current_file_count < len ( self .total_file_list )
48+ return self .current_file_count < self .total_file_count
4549
4650 def fetchMore (self , parent : QModelIndex = None ):
47- remainder = len ( self .total_file_list ) - self .current_file_count
48- itemsToFetch = min (self .batch_size , remainder )
49- if itemsToFetch <= 0 :
51+ remainder = self .total_file_count - self .current_file_count
52+ items_to_fetch = min (self .batch_size , remainder )
53+ if items_to_fetch <= 0 :
5054 return
5155
5256 self .beginInsertRows (
53- QModelIndex (), self .current_file_count , self .current_file_count + itemsToFetch - 1
57+ QModelIndex (),
58+ self .current_file_count ,
59+ self .current_file_count + items_to_fetch - 1 ,
5460 )
5561
56- self .current_file_count += itemsToFetch
62+ self .current_file_count += items_to_fetch
5763
5864 self .endInsertRows ()
5965
60- self .numberPopulated .emit (itemsToFetch )
66+ self .numberPopulated .emit (
67+ items_to_fetch , self .current_file_count , self .total_file_count
68+ )
6169
6270 def set_total_file_list (self , fileList : list ):
6371 self .beginResetModel ()
6472
6573 self .total_file_list = fileList
6674 self .current_file_count = 0
67- self .numberPopulated .emit (0 )
75+ self .numberPopulated .emit (0 , 0 , 0 )
6876
6977 self .endResetModel ()
0 commit comments