QListWidget moving to end of list automatically
12,274
Solution 1
listWidget->scrollToBottom()
would work
Solution 2
Should work with scrollToItem().
Author by
cftmon
Updated on June 13, 2022Comments
-
cftmon almost 2 years
I have a
QListWidget
that has many items added to it, however i want theQListWidget
to auto-scroll to the last item everytime a new item is added to it.Is this even possible? -
Troyseph about 9 yearsCould you conditionally
scrollToBottom()
only if it is currently at the bottom? -
MarSoft about 3 yearsThis won't work for the item you have just added. Or more precisely, it won't work reliably. Often by the time you added a new item, the list view does not yet know about its presence and hence cannot scroll to it.
-
MarSoft about 3 yearsYou can check that by accessing scrollbar widget:
scrollbar = myListView.verticalScrollBar(); if(scrollbar == NULL || scrollbar.value() == scrollbar.maximum()) { addItem(); myListView.scrollToBottom(); } else { addItem(); }
Here we check if scrollbar exists at all (just to be safe), and if it is missing or we are already at bottom then we do scroll after adding item. Otherwise we just add item without scrolling.