【问题标题】:How to to center selected text in QTextBrowser如何在 QTextBrowser 中居中选定文本
【发布时间】:2017-01-07 01:30:44
【问题描述】:

下面发布的代码创建了QTextBrowser 窗口,其中填充了100 行文本:从MESSAGE-0000 一直到MESSAGE-0099

from PyQt4 import QtCore, QtGui
app=QtGui.QApplication([])

textBrowser = QtGui.QTextBrowser()
for i in range(100):
    textBrowser.insertPlainText('MESSAGE-%04d'%i + '\n')

textBrowser.show()
app.exec_()

问题:如何在其文本中找到行号:MESSAGE-0051,然后选择或突出显示它,然后滚动到它,使 selected-highlightet 行位于QTextBrowser 窗口的顶部边缘,所以结果如下所示:

如何实现?

【问题讨论】:

    标签: python qt pyqt qlineedit qtextbrowser


    【解决方案1】:

    如果你向后搜索,它会自动将选定的行滚动到视口的顶部:

    textBrowser.moveCursor(QtGui.QTextCursor.End)
    textBrowser.find('MESSAGE-0051', QtGui.QTextDocument.FindBackward)
    

    (当然,如果你搜索MESSAGE-0095,它不会将选定的行放在顶部,因为视图不能向下滚动那么远)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-19
      • 2019-10-14
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多