Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Infinite rows in QTableWidget?



  • Is there a way to tell QTableWidget to support infinite rows?

    If not, how could I detect when the user scrolls to the last row, in order to automatically add more for the user?



  • @Publicnamer since rowCount() returns an int there is your theoretical limit.



  • there are scroll mode, overshooting policy and frame rate settings. From them, you maybe able to know the actual location of scrolling and can decide when more rows are added or replaced.

    Maybe create a few frames and restart from the beginning when the last frame reaches. Sure, you update the frames after they pass.



  • @artwaw I don't think you read my question.


  • Lifetime Qt Champion

    Hi
    Normally a custom model is used instead
    https://doc.qt.io/qt-5/qtwidgets-itemviews-fetchmore-example.html

    key here is
    bool canFetchMore(const QModelIndex &parent) const override;
    void fetchMore(const QModelIndex &parent) override;

    Adding QTableWidget* on scrolling might not be a smooth experience.



  • @mrjj Is the QTableWidget efficient so that if there are millions of empty cells, there is not a million times additional use of RAM?


  • Lifetime Qt Champion

    @Publicnamer
    Hi
    Yes, cells that are not allocated returns a NULL ptr for item() so it wont waste ram as such but
    creating millions of items will take time.

    So for huge models, often a custom model is used where the internal data structure fits the demand for such a huge amount of rows/items.

    But what will work best depends on what you actually need to do.



  • @mrjj Regarding the methods canFetchMore and fetchMore, I subclassed QTableWidget and added these methods,
    but I am finding they are never called. Also none of the cells that previously appeared now appear.
    I'm wondering if this is a slightly advanced topic and a more practical solution would be better.

    Is there a way to detect when the user has scrolled to the last row?



  • @JoeCFD Looking in the class documentation and parent classes' as well, I don't see an overshoot policy mentioned anywhere.


  • Lifetime Qt Champion

    Hi,

    You can check the visualRect of the last index. If it's valid then it means the index is shown.



  • @Publicnamer

        QScrollerProperties properties = QScroller::scroller( your widget )->scrollerProperties();
        QVariant overshoot_policy = QVariant::fromValue< QScrollerProperties::OvershootPolicy >( QScrollerProperties::OvershootAlwaysOff );
        properties.setScrollMetric( QScrollerProperties::VerticalOvershootPolicy, overshoot_policy );
        properties.setScrollMetric( QScrollerProperties::HorizontalOvershootPolicy, overshoot_policy );
        QVariant frame_rates = QVariant::fromValue< QScrollerProperties::FrameRates >( QScrollerProperties::Fps60 );
        properties.setScrollMetric( QScrollerProperties::FrameRate, frame_rates );
        QScroller::scroller( your widget )->setScrollerProperties( properties );
    


  • @Publicnamer out of curiosity - why bother with custom model when you can choose the path of least effort and employ QSqlTableModel (or derive something from QIdentityProxyModel) with Sqlite? With Qt you can instantiate in-memory db or, if during your project you discover that memory becomes an issue, quickly drop it from memory to disk?
    Sqlite is lightning fast.
    Sure, it requires a bit of thinking ahead (sorting, potential delegates for the table view) but in my experience all "convenience" item views are simply too much of a problem.



  • @JoeCFD That seems to have no effect on my QTableWidget. I set the policy to OvershootAlwaysOn.
    When I scroll using the mouse's thumbwheel is still stops at the defined last row.

    Is there a signal that I can catch to detect when scrolling has stopped?
    Thanks.



  • @Publicnamer I am using it for tablewidget in my code to avoid overshooting. This piece of code works fine for me on Ubuntu.
    https://www.qtcentre.org/threads/16937-QTableWidget-scrolling-to-bottom-of-the-table



  • @SGaist said in Infinite rows in QTableWidget?:

    You can check the visualRect of the last index. If it's valid then it means the index is shown.

    OK yes I can listen for the vertical scrollbar's valueChanged signal and check for the visualRect there.



  • @Publicnamer Actually that doesn't work, because all rows have valid visualItemRect's.

        QRect rect = mytable->visualItemRect (item);
        if (rect.isValid()) { /* always true */ }
    

    The only solution I see is to use itemAt to find out what's at the bottom of the table.



  • @Publicnamer
    It depends just what you want, what is in your model, and whether you know how much is in the backing model.

    Your question seems to relate to paging the data, or something based off that. Rather than looking at visual rects --- especially if they don't work --- the alternative is to look at the scrollbar. That reflects how far through the data the user is.

    One possible implementation is given in Pagination in QtableWidget, @raven-worx's algorithmic outline there.

    I have seen others (unfortunately can't find references) where the algorithm is to maintain the scrollbar height such that there is always a "gap" at the bottom which the user can scroll into, and that fires a fetch of more data and recalibration of the scrollbar, which still has a "gap" at the bottom. Think of when you use an application which produces large or endless output, like a web page receiving more and more data or showing photos where more and more are to come (e.g. some tumblr pages). The scrollbar always allows the user to scroll some more. @raven-worx's suggestion could be adapted to that.

    If you Google for stuff involving QTableWidget or QTableView and pagination you may find some useful code/ideas.


Log in to reply