calling model's data() method on demand
-
i have a table view which should be able to display millions of rows. to make this efficient, i have decided not to populate the table view at once. i want to populate only the visible area of the table plus say 100 rows. then when the user scrolls down, populate that part of the table.
currently when i tell the model it has say 10000 rows,
data()
is called 10000 times and the table is fully filled.how can i make an on-demand call to
data()
? -
Hi,
What kind of on demand do you have in mind ?
-
i want
data ()
to be called when the table cell becomes visible.
in other words, there's no sense to fill the whole table if only day 20 rows of it are visible. -
@user4592357 said in calling model's data() method on demand:
in other words, there's no sense to fill the whole table if only day 20 rows of it are visible.
Where do you get the data from? Did you take a look at QAbstractItemModel::canFetchMore?
-
@Christian-Ehrlicher said in calling model's data() method on demand:
Did you take a look at QAbstractItemModel::canFetchMore?
This is the correct solution but a word of warning: that's a one-time-only thing though. once the user scrolled to the bottom you have all the data loaded in memory and you go back to the starting point.
-
@Christian-Ehrlicher
thanks. i'm looking at Fetch More Example and it seems to be what i need, but...@VRonin
also it has another bad side. say the user scrolls down really fast. in that case i would like to populate only the visible area of the table. whereas this method will populate model up until the current scrollbar position. -
How much data do you want to display that you need a delayed load? And how do you handle such data?
-
@Christian-Ehrlicher
still not sure. but i want the table to be populated immediately. i'll decide on the number later, maybe 100000?the data is to be taken by executing a sql query.
the way i picture it is to execute the query by limiting the number of results (say,LIMIT 100000
), and if the current row is greater than what i have currently (i.e. 100000), then i'll query the database again by specifying a limit and offset (LIMIT 100000 OFFSET 100000
). -
So you must do a select count(*) before. And in the end you've all data in the model. Nothing retains you from doing a select as soon as data() is called for a specific row so I don't see any problem - yu just have to remember which rows you already fetched.
-
@Christian-Ehrlicher
yes, i doSELECT COUNT(*) FROM TABLE
before anything else. that's the problem. when i set the row count to that number,data()
is called for all the rows, and i don't know how to "stop" it. -
Did you check for which role? I doubt it's the DisplayRole ...
-
@Christian-Ehrlicher
yes it's display role -
The DisplayRole is normaly only requested when the cell is visible - please provide a small example so we can reproduce your issue.