【发布时间】: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). -
谢谢,成功了!