Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何突出显示QPlainTextEdit小部件中的整行文本?_Python_Qt_Pyqt_Pyside - Fatal编程技术网

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

Python 如何突出显示QPlainTextEdit小部件中的整行文本?,python,qt,pyqt,pyside,Python,Qt,Pyqt,Pyside,我使用QPlainTextEdit制作了一个小编辑器,我希望能够突出显示整行文本,以显示哪一行有错误 我可以格式化文本,但我无法确定如何将光标位置设置为指定行上文本的开始和结束位置 这段代码显示了我的目标: editor = QtGui.QPlainTextEdit() fmt = QtGui.QTextCharFormat() fmt.setUnderlineColor(Qt.red) fmt.setUnderlineStyle(QtGui.QTextCharFormat.SpellChec

我使用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)

尝试如下操作:
block=editor.document().findBlockByLineNumber(第行);cursor.setPosition(block.position());cursor.select(QTextCursor.lineundersor)
。谢谢,成功了!