How to program a scroll bar to go to the bottom / top in case of a change in the QPlainTextEdit or QTextEdit area?

How to program a scroll bar to go to the bottom / top when changing the QPlainTextEdit or QTextEdit area? It looks like it has no control function.

+7
source share
5 answers

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

So, to go to the top:

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

And to go to the bottom:

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

This should work for both QTextEdit and QPlainTextEdit.

+14
source

You can use the "sureCursorVisible" method:

 void QTextEdit::ensureCursorVisible () Ensures that the cursor is visible by scrolling the text edit if necessary. 

This is not a slot, so you cannot connect it to any signal - you will need to create something yourself so that you can connect to the void textChanged () signal.

Disclaimer: I may have misunderstood your question - I assume you want to scroll down when text is added to the text.

+7
source

When the text editing control changes, QWidget::resizeEvent called. You just need to override this function in your subclass and call verticalScrollBar -> setValue (verticalScrollBar -> minimum()) (or maximum() ).

+3
source

Here I submit my Solution , as indicated above, in my case.

I want to get the cursor at the beginning of QTextbrowser .

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 this helps someone and saves them valuable time.

0
source

I did 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) 
0
source

All Articles