How to program scrollbar to jump to bottom/top in case of change in QPlainTextEdit or QTextEdit area?

falconium picture falconium · Feb 8, 2011 · Viewed 26.4k times · Source

How to program scrollbar to jump to bottom/top in case of change in QPlainTextEdit or QTextEdit area? It looks like it doesn't have any controlling function.

Answer

d11 picture d11 · Nov 23, 2012

QTextEdit and QPlainTextEdit are both inherited from QAbstractScrollArea. The QAbstractScrollArea object provides access to the scrollbar through the verticalScrollBar() method.

Thus, to jump to the top:

ui.textEdit->verticalScrollBar()->setValue(0);

And to jump to the bottom:

ui.textEdit->verticalScrollBar()->setValue(ui.textEdit->verticalScrollBar()->maximum());

This should work for both QTextEdit and QPlainTextEdit.