Unsolved QListView ScrollToBottom doesn't scroll to the very end
-
Hello
I want to achieve the 'Auto scroll' effect on QListView component, currently I am using scrollToBottom() function to get to the very end, but it always stays a little bit above the complete end of the list, see the image attached below.
I think there might be some feature in the QtDesigner that I haven't discovered yet, but I was not able to figure out how to scroll to the very end, so the bottom line doesn't get clipped this bad.
I am using Python with PySide6 and I am importing the ui file converted to the .py file using pyside6-uic tool.Thanks to anybody answering, I really appreciate any piece of help, since I'm fairly new to the whole Qt stuff, but I like to learn and I hope my question is just not completely stupid.
The image:
-
Hi and welcome to devnet,
Shouldn't you rather use scrollTo using QAbstractItemView::PositionAtBottom ?
-
I don't know if I'm doing anything wrong, but the code implemented throws me an exception
TypeError: 'PySide6.QtWidgets.QListView.scrollTo' called with wrong argument types: PySide6.QtWidgets.QListView.scrollTo(ScrollHint) Supported signatures: PySide6.QtWidgets.QListView.scrollTo(PySide6.QtCore.QModelIndex, PySide6.QtWidgets.QAbstractItemView.ScrollHint = PySide6.QtWidgets.QAbstractItemView.ScrollHint.EnsureVisible)
The line I added looks like this:
self.ui.phpConsole.scrollTo(QAbstractItemView.PositionAtBottom)
I just changed the line mentioned previously with this one, but the error shows up.
-
You are missing the index argument.
-
Hey I tried it by creating an QModelIndex based on the current data of the model:
class LogModel(QAbstractListModel): ... def log(self, message: str): self.experiment_logs.append(message) self.layoutChanged.emit() current_index = self.createIndex(len(self.experiment_logs)-1, 0) self._list_view.scrollTo(current_index, QAbstractItemView.PositionAtBottom)
the following QModelIndex is returned in the last step:
<PySide6.QtCore.QModelIndex(100,0,0x0,LogModel(0x3d8ad00)) at 0x7f06c843a280>
where the "100" matches the length - 1 of the list which is the data structure behind the LogModel.But when I call
scrollTo
, as suggested by you, nothing happends.I am also tried the following (Which should be wrong, because the QModelIndex should also start at zero?):
current_index = self.createIndex(len(self.experiment_logs), 0)
Any further thoughts?
-
Hi and welcome to devnet,
What does
_list_view
do in that model ? That's not its place.
Also, emitting layoutChanged for adding a row of data is wrong. There are two signals to signify that you are adding new data and when you are done, use them. -
Yes the
_list_view
was at the wrong place. I am now emitting a signal to the correct place.I got it working with:
LogModel:
def log(self, message: str): self.experiment_logs.append(message) top_index = self.createIndex(self.rowCount()-2, 0) bottom_index = self.createIndex(self.rowCount()-1, 0) self.dataChanged.emit(top_index, bottom_index) self.signal.scroll_list_view.emit()
Main:
self.log_model.signal.scroll_list_view.connect( lambda: self.list_view_log.scrollToBottom() )
So I guess the
scrollToBottom
works fine if the correct Signal is emitted? -
When you add data, there's a begin/endInsertRows pair of function that you should call. Their a responsible for notifying the view that things are changing with regard to the amount of the data.