PDA

View Full Version : QTableView VerticalScrollBar connect and insert rows



dibfibo
23rd March 2020, 15:40
Hello,

I have been using Qt for a couple of weeks so my posts will be frequent in this period :).

I have a QTableView which queries my MySQL database.
If I run "select *" the table is 500k rows and 12 columns.
To load the data I thought about this solution:

1 - Run the query with a limit.

2 - When the value of the VerticalScrollBar is equal to its maximum value, make a PushButtonInsert appear.
(I tried to use a "connect" but it doesn't work)

3 - When the PushButtonInsert is clicked I would like to insert the data of another query into the table, with limit and offset so as not to have duplicate data in the table.

How can I accomplish steps 2 and 3?

d_stranz
23rd March 2020, 18:34
The QAbstractItemModel class contains two methods that are designed to handle this problem: QAbstractItemModel::canFetchMore() and QAbstractItemModel::fetchMore(). What you want to do is an advanced use of these features.

Basically, you need a "proxy model" class to act as a gateway between your SQL model and the view. The proxy model knows how many rows the full query returned, but tells the view that the row count is less than that. The view will call the proxy's canFetchMore() and fetchMore() methods to fill up the table on screen until the proxy returns false for the canFetchMore() method.

Look at the Qt Fetch More (https://doc.qt.io/qt-5/qtwidgets-itemviews-fetchmore-example.html) example. It is similar to what you want to do. You would have to add a "batchSize" parameter to control how many new items are added with each call to fetchMore(). You could add a slot to your proxy class that responds to the button click, and in that slot you would call your fetchMore() method. fetchMore() increases the count by the batch size (up to the total number of entries), and calls the beginInsetRows() / endInsertRows() functions to tell the view more data is available.

dibfibo
24th March 2020, 21:46
I had already read about "fetchmore" and "canfetchmore" and noticed this example, but thank you so much because I wasn't sure I was on the right way and your words are more comprehensible than docs. XD
Now I am already working to complete the other missing parts of the software but surely this feature will not be missing in the second release. HOPE