【问题标题】:How can I highlight a full row of text in a QPlainTextEdit widget?如何在 QPlainTextEdit 小部件中突出显示整行文本?
【发布时间】:2017-08-25 16:08:05
【问题描述】:

我使用 QPlainTextEdit 制作了一个小编辑器,我希望能够突出显示整行文本以显示哪一行有错误。

我可以格式化文本,但我不知道如何将光标位置设置为指定行上文本的开始和结束位置。

这个 sn-p 显示了我已经到达的位置:

editor = QtGui.QPlainTextEdit()

fmt = QtGui.QTextCharFormat()
fmt.setUnderlineColor(Qt.red)
fmt.setUnderlineStyle(QtGui.QTextCharFormat.SpellCheckUnderline)

# I'd like these values to encompass the whole of say, line 4 of the text
begin = 0 
end = 5

cursor = QtGui.QTextCursor(editor.document())
cursor.setPosition(begin, QtGui.QTextCursor.MoveAnchor)
cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor)
cursor.setCharFormat(fmt)

我可以仅从行号计算出光标要突出显示的起点和终点吗?

【问题讨论】:

  • 尝试类似:block = editor.document().findBlockByLineNumber(line); cursor.setPosition(block.position()); cursor.select(QTextCursor.LineUnderCursor).
  • 谢谢,成功了!

标签: python qt pyqt pyside


【解决方案1】:

感谢 Ekrumoro,我设法让这个工作变得如此:

editor= QtGui.QTextCharFormat()

fmt = QtGui.QTextCharFormat()
fmt.setUnderlineColor(Qt.red)
fmt.setUnderlineStyle(QtGui.QTextCharFormat.SpellCheckUnderline)

block = editor.document().findBlockByLineNumber(line)
blockPos = block.position()

cursor = QtGui.QTextCursor(editor.document())
cursor.setPosition(blockPos)
cursor.select(QtGui.QTextCursor.LineUnderCursor)
cursor.setCharFormat(fmt)

【讨论】:

    猜你喜欢
    • 2019-12-29
    • 2017-08-25
    • 2012-03-30
    • 2011-04-16
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多