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

◇◆丶佛笑我妖孽 提交于 2019-11-30 18:08:48

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.

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.

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

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.

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!