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

原文链接:
https://9to5answer.com/how-to-program-scrollbar-to-jump-to-bottom-top-in-case-of-change-in-qplaintextedit-or-qtextedit-area

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐