QTextEdit:仅在滚动条位于底部时自动向下滚动 [英] QTextEdit: scroll down automatically only if the scrollbar is at the bottom

查看:671
本文介绍了QTextEdit:仅在滚动条位于底部时自动向下滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个QTextEdit,可以显示很多文本.它是不可编辑的.假设我想从头开始阅读一些内容,向上滚动,但是随后添加了新行,并且滚动条自动移至底部.使用各种程序时(无论使用哪种语言编写),我都会遇到类似的问题.一个人如何处理这个问题?

There's a QTextEdit that displays quite a lot of text. It is NOT editable. Suppose I want to read something around the beginning, scroll up, but then a new line is added and the scrollbar automatically goes to the bottom. I experience similar problems when using various programs (regardless of the language they were written in). How does one deal with this problem?

将新行添加到文本时我想要的行为:

The behavior I want when a new line is added to the text:

  • 如果滚动条位于底部,则自动向下滚动.
  • 如果滚动条在其他位置,请不要滚动

我想 sureCursorVisible() 并不是解决方案,因为QTextEdit是不可编辑的,因此用户不会在其中单击,并且光标的位置与垂直滚动条的位置不同.

I suppose that ensureCursorVisible() is not the solution, since the QTextEdit is not editable, the user won't click inside it, and the position of the cursor is not the same as the position of the vertical scrollbar.

推荐答案

我将创建滚动条位置侦听器,该滚动条将侦听滚动位置(并检查滚动条是否位于底部).

I would make Scroll bar position listener, which will remember position on scrolling (and also check is it at the bottom or not).

然后,当添加新行时,检查它是否在底部,是否向下滚动,是否在其他位置,然后滚动回到该位置.

Then, when new line is added, check is it at bottom, if is scroll down, if is somewhere else then scroll back to that position.

选中此 QScrollBar ,您可以通过horizo​​ntalScrollBar()从QTextEdit中获取它和verticalScrollBar().

Check this QScrollBar, you can grab it from QTextEdit via horizontalScrollBar() and verticalScrollBar().

更具体地讲,我将插槽与来自QScrollBar的信号相连- valueChanged(int值)并按照此处中所述的值进行操作.

More concrete, I would connect slot with signal from QScrollBar - valueChanged(int value) and play with values as it is described here.

这篇关于QTextEdit:仅在滚动条位于底部时自动向下滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆