我似乎不能让pyqt5中的滚动条自动跟随,也不能在y位置不在末尾时自动跟随。
我试过了:
ccursor=self.textCursor()
sbar=self.verticalScrollBar()
# get the max value
csbarval=float(sbar.value())
maxsbarval=float(sbar.maximum())
print("sbar value:{}".format(csbarval))
print("sbar max:{}".format(maxsbarval))
self.append(data)
if (float(maxsbarval) > 0.0) and (csbarval/maxsbarval > 0.98):
ccursor.movePosition(QtGui.QTextCursor.End)
是否有api可以自动执行此操作?
发布于 2019-10-01 00:15:50
是的,所以我在一个安静的地方思考了一下,结果是:
class TEdit(QtWidgets.QTextEdit):
def scrollbarBehaviour(self,data):
scrollbar=self.verticalScrollBar()
current_pos=scrollbar.value()
current_max=scrollbar.maximum()
at_bottom_of_text=(current_pos>=current_max -4)
self.append(data)
if at_bottom_of_text:
scrollbar.setValue(scrollbar.maximum())
else:
scrollbar.setValue(current_pos)
需要注意的是,在将数据附加到文本区域之前,要弄清楚滚动条是否处于最大值/末尾。另外,需要显式设置滚动条,否则它会自动滚动到末尾。
https://stackoverflow.com/questions/58165058
复制相似问题