Nod*_*ers 6 python qt pyqt pyside
我使用 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)
Run Code Online (Sandbox Code Playgroud)
我可以仅根据行号计算出光标要突出显示的起点和终点吗?
感谢 Ekrumoro,我设法让这个工作像这样:
editor = QtGui.QPlainTextEdit()
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)
Run Code Online (Sandbox Code Playgroud)