How to Auto-scroll QT Text Field Widget

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:

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_()

Answers


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

    self._console.append('%s : '%n)

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_()

Need Your Help

How to release ViewController after pushviewcontroller

iphone memory-management uiimageview

I have 2 view, named viewcontroller1,viewcontroller2. every viewcontroller has load about 3 pictures(load it in XIB) , when i try to use [self.navigationcontroller pushviewcontroller], to push view...

Formflattening and file size reduction / unreferenced objects

java pdf itext

I have got a question regarding the behaviour of iText when formflattening and didn't find anything in the web (or just unanswered questions). This is what I do:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.