我使用 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)
我可以仅根据行号计算出光标要突出显示的起点和终点吗?
我只是尝试了Maya 2017并看到他们已经过去了PySide2,这很棒,但我的所有工具都有import PySide或者from PySide import ...在其中.
显而易见的解决办法是查找/替换import PySide来import PySide2,希望一切后,目前仍然可以工作,但我还是希望能够支持老版本的玛雅.
我的想法是有一个单行解决方案来查找/替换,如:
import (PySide2 if "PySide2" in sys.modules else PySide)
Run Code Online (Sandbox Code Playgroud)
但这回归: Error: invalid syntax
有没有人有任何替代方案的想法?我想尝试将它保持在单行上,这样当我有条件时,它就很容易替代:
from PySide import QtCore, QtGui
Run Code Online (Sandbox Code Playgroud)
谢谢!