0

A code below creates a window with QTextBrowser widget. For five seconds the new lines of text added to QTextBrowser using its .insertPlainText() method. As soon as text fills an entire field QTextBrowser automatically adds a scroll bar. But it does nothing to scroll down so the last line of text would be always visible (or readable) by the user.

I would like to know how to make QTestBrowser scroll down automatically so the last line of the text is always within its text field or visible. How to achieve it?

Screenshot: enter image description here

Code:

import sys, time
from PyQt4 import QtCore, QtGui

class MyDialog(QtGui.QDialog):
    def __init__( self, parent = None ):
        super(MyDialog, self).__init__(parent)
        self._console = QtGui.QTextBrowser(self)        
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self._console)
        self.setLayout(layout)
    def updateField(self):
        for m in range(5):
            for n in range(100):
                self._console.insertPlainText('%s : '%n)
                QtGui.qApp.processEvents()
            time.sleep(1)

if ( __name__ == '__main__' ):
    app = None
    if ( not QtGui.QApplication.instance() ):
        app = QtGui.QApplication([])
    dlg = MyDialog()
    dlg.show()
    dlg.updateField()
    if ( app ): app.exec_()
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • possible duplicate of [How to program scrollbar to jump to bottom/top in case of change in QPlainTextEdit or QTextEdit area?](http://stackoverflow.com/questions/4939151/how-to-program-scrollbar-to-jump-to-bottom-top-in-case-of-change-in-qplaintexted) – NoDataDumpNoContribution Jul 01 '14 at 10:43

2 Answers2

3

Just use .append instead of .insertPlainText to ensure auto scrolling:

    self._console.append('%s : '%n)
ngulam
  • 995
  • 1
  • 6
  • 11
1

Since QTextBrowser is sub-classed from QTextEdit widget be we can use its .ensureCursorVisible() method.

To make QTextBrowser scroll down automatically as it is being filled with text all we need to do is to add an extra line to enable its property responsible for this feature: self._console.ensureCursorVisible()

Here is the working code solution:

import sys, time
from PyQt4 import QtCore, QtGui

class MyDialog(QtGui.QDialog):
    def __init__( self, parent = None ):
        super(MyDialog, self).__init__(parent)
        self._console = QtGui.QTextBrowser(self) 
        self._console.ensureCursorVisible()       
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self._console)
        self.setLayout(layout)
    def updateField(self):
        for m in range(5):
            for n in range(100):
                self._console.insertPlainText('%s : '%n)                    
                QtGui.qApp.processEvents()
            time.sleep(1)

if ( __name__ == '__main__' ):
    app = None
    if ( not QtGui.QApplication.instance() ):
        app = QtGui.QApplication([])
    dlg = MyDialog()
    dlg.show()
    dlg.updateField()
    if ( app ): app.exec_()
alphanumeric
  • 17,967
  • 64
  • 244
  • 392