How to make my QTableView show the latest appended data
-
How to make my QTableView to follow and display the latest data row appended from a data stream unless the user is browsing older data?
I want my app to always scroll to the last item unless the user has used the scroll bar to browse to a previous row. But start following new data again when scrolled back to the end of the data.
I've added this to my update handler in my TableModel (PyQt5):
@pyqtSlot(list) def update_data(self, data): global myview self.beginInsertRows(QModelIndex(), self.rowcnt, self.rowcnt) self.data.append(data) self.rowcnt = self.rowcnt + 1 self.endInsertRows() ## scroll to new row myview.scrollToBottom()
scrollToBottom() works, but how do I determine whether the last row is currently displayed or user has scrolled to some other location? eg. switch to follow mode, vs. browse mode and back.
Seems like this could be a built-in ScrollMode, eg. Follow & Browse
-
@brookbot said in How to make my QTableView show the latest appended data:
So a simple comparison is not going to work.
A QScrollbar has min/max values which you can compare with the current value.
Returns true if the given row is hidden; otherwise returns false." - what does hidden mean?
setRowHidden is very clear about this: "If hide is true row will be hidden, otherwise it will be shown."
-
You can check the current state of the scrollbar before inserting a new row - if it's at the bottom then use scrollToBottom afterwards.
-
You can check the current state of the scrollbar before inserting a new row - if it's at the bottom then use scrollToBottom afterwards.
@Christian-Ehrlicher Granted, I figured as much, but what is the class name and function? I see only QAbstractScrollArea and no functions for accessing state of the actual scrollbar...
-
@Christian-Ehrlicher Granted, I figured as much, but what is the class name and function? I see only QAbstractScrollArea and no functions for accessing state of the actual scrollbar...
@brookbot said in How to make my QTableView show the latest appended data:
I see only QAbstractScrollArea
Then you should take a look into the documentation and search for scrollbar.
-
@brookbot said in How to make my QTableView show the latest appended data:
I see only QAbstractScrollArea
Then you should take a look into the documentation and search for scrollbar.
@Christian-Ehrlicher Ok, so looking at https://doc.qt.io/qt-5/qabstractitemview.html I found the scrollbar hidden under "List of all members, including inherited members". There seems to be a number of ways to get the value - via the "value" attribute or the signal "sliderMoved".
setValue() seems to have no effect, only scrollToBottom() seems to work.
The bigger issue however is that when scrolled to the bottom, the slider value is not the same as the row count and depends on the size of or number of columns displayed in the view. So a simple comparison is not going to work. eg. I can have 5 rows of data and the scrollbar value is still 0 even though I've called scrollToBottom()
There must be a better way to do this?
FYI - documentation can be interpreted many ways. Just because its documented does not mean its clear to everyone. For example: "isRowHidden - Returns true if the given row is hidden; otherwise returns false." - what does hidden mean? Does it mean the row is not in the current view area, obscured by another widget, or there is some other feature that allows hiding rows in a table? Its just not clear, and I can already derive the description given from the function name. What I don't know is what is meant by Hidden.
-
@brookbot said in How to make my QTableView show the latest appended data:
So a simple comparison is not going to work.
A QScrollbar has min/max values which you can compare with the current value.
Returns true if the given row is hidden; otherwise returns false." - what does hidden mean?
setRowHidden is very clear about this: "If hide is true row will be hidden, otherwise it will be shown."
-
@brookbot said in How to make my QTableView show the latest appended data:
So a simple comparison is not going to work.
A QScrollbar has min/max values which you can compare with the current value.
Returns true if the given row is hidden; otherwise returns false." - what does hidden mean?
setRowHidden is very clear about this: "If hide is true row will be hidden, otherwise it will be shown."
@Christian-Ehrlicher Ok, thanks - comparing .value() with .maximum() works for me. The only annoy thing is that the scroll bar keeps moving even though the rows displayed are not. A bit distracting, but I expect that will go away when there is a lot of data.