QTextEdit / QPlainTextEdit 滚动至顶部 / 底部
解决方法一
QTextEdit 与 QPlainTextEdit 均继承自 QAbstractScrollArea。 QAbstractScrollArea 类提供访问滚动条的方法 。
verticalScrollBar();
因此,跳转到顶部,方法如下:
ui->textEdit->verticalScrollBar()->setValue(0);
以及,跳转到底部:
ui->textEdit->verticalScrollBar()->setValue(ui->textEdit->verticalScrollBar()->maximum());
This should work for both QTextEdit and QPlainTextEdit.
原文:
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.
解决方法二
TextEdit 控件改变大小时,QWidget::resizeEvent
,会被调用。可以在你的子类中重写此函数,然后调用
verticalScrollBar->setValue(verticalScrollBar->minimum());
或
verticalScrollBar->setValue(verticalScrollBar->maximum());
原文:
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()).