Keep scrolling QTableView to the last line as the model grows

I have a QTableView in a pyqt application. I am constantly adding rows to the base model. And what I want is to view the constant scroll to the last, last line (is this a behavior called "autoscrolling"?). But instead, the view does not scroll at all (automatically) and remains in its position.

Is there any way to activate this behavior for autoscrolling or do I need to encode something to achieve it?

Cheers, Wolfgang

+4
source share
1 answer

There is no default auto-scroll feature, but you can make the behavior relatively simple. Your model will highlight rowsInserted when inserting / adding rows. You can connect to this signal and call scrollToBottom in your view.

However, there is one problem. The view needs to adjust itself because it will not place the element at the bottom when rowsInserted . Calling scrollToBottom within QTimer.singleShot solves this because QTimer will wait until there are pending events (for example, updating the view).

Assuming the model is stored as self.model and the view is self.view , here is how it would look:

 self.model.rowsInserted.connect(self.autoScroll) 

and autoScroll :

 def autoScroll(self): QtCore.QTimer.singleShot(0, self.view.scrollToBottom) 

Or, if you prefer not to have a separate method for this:

 self.model.rowsInserted.connect(lambda: QtCore.QTimer.singleShot(0, self.view.scrollToBottom)) 
+3
source

Source: https://habr.com/ru/post/1436993/


All Articles