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.
6 Answers
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.

- 1,344
- 17
- 15
-
I missed that TonyK's answer already solves the problem. TonyK's answer should be accepted. – d11 Nov 23 '12 at 10:56
You can use the 'ensureCursorVisible' method:
void QTextEdit::ensureCursorVisible () Ensures that the cursor is visible by scrolling the text edit if necessary.
This is not a slot, though, so you can't connect it to any signal -- you'll have to create something yourself that you can connect to the void textChanged() signal.
Disclaimer: I may have misunderstood your question -- I assume you want to scroll down when some text is appended to the text.

- 3,778
- 1
- 20
- 16
-
Note that this scrolls both vertically and horizontally which may not be what you want. – letmaik Jun 08 '18 at 17:10
-
When a text edit control is resized, QWidget::resizeEvent
is called. You just have to override this function in your subclass, and call verticalScrollBar -> setValue (verticalScrollBar -> minimum())
(or maximum()
).

- 16,761
- 4
- 37
- 72
-
Thanks for the answer! I'd like to develop a terminal client program for a special server. There is a checkbox with which you can lock/unlock the screen. If it is unlocked, then processed incoming packets written to the QPlainTextBox would automatically force scroll bar to be at the bottom (regardless cursor position). Your solution seems promising. I'll give you a feedback later... Thanks! – falconium Feb 10 '11 at 22:56
I have done in Pyqt.
self.scrollArea.verticalScrollBar().rangeChanged.connect(self.change_scroll)
--------
@pyqtSlot(int, int)
def change_scroll(self, min, max):
print("cambio", min, max)
self.scrollArea.verticalScrollBar().setSliderPosition(max)

- 49
- 1
- 8
Here I am posting my Solution as above solution dint work in my case.
I want to get the cursor at the beginning of QTextbrowser
.
By using QTextEdit::setTextCursor, you can move the visible cursor where you want:
// Go to beginning
QTextCursor textCursor = ui->textBrowser->textCursor();
textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1);
ui->textBrowser->setTextCursor(textCursor);
Hope, it will help to some one and save their precious time.

- 1,997
- 2
- 23
- 47
I am using QTextEdit and textEdit.verticalScrollBar().setValue(0)
doesn't work for me.
In my case, textEdit.moveCursor(QTextCursor.Start)
can scroll to the top.

- 598
- 6
- 11