如何在 QPlainTextEdit 小部件中突出显示整行文本?

How can I highlight a full row of text in a QPlainTextEdit widget?

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

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

这段代码显示了我到达的位置:

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)

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

感谢 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)