Results 1 to 3 of 3

Thread: QTableView VerticalScrollBar connect and insert rows

  1. #1
    Join Date
    Mar 2020
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Question QTableView VerticalScrollBar connect and insert rows

    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?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,252
    Thanks
    304
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView VerticalScrollBar connect and insert rows

    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 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2020
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QTableView VerticalScrollBar connect and insert rows

    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

Similar Threads

  1. Replies: 4
    Last Post: 11th October 2014, 15:21
  2. table view (how to insert rows and columns)
    By narlapavan in forum Qt Programming
    Replies: 1
    Last Post: 14th February 2012, 11:50
  3. Replies: 4
    Last Post: 18th November 2011, 19:48
  4. TableView + SqlQueryModel: can't insert or edit rows
    By jiveaxe in forum Qt Programming
    Replies: 3
    Last Post: 27th September 2008, 22:55
  5. Insert rows...
    By steg90 in forum Qt Programming
    Replies: 13
    Last Post: 18th September 2007, 14:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.