如果突出显示取决于文本,那么@oetzi的解决方案就是指定的解决方案,因为尽管您删除了行,相同的文本仍将保持突出显示,如果相反,突出显示仅取决于行的位置,那么可能的解决方案是使用 QSyntaxHighlighter。
在以下示例中,您可以输入与空格分隔的整数,这些数字表示将突出显示的行的位置(位置从 0 开始):
import sys
from PyQt5.QtCore import pyqtSlot, QRegExp
from PyQt5.QtGui import QColor, QRegExpValidator, QSyntaxHighlighter, QTextCharFormat
from PyQt5.QtWidgets import (
QApplication,
QLineEdit,
QPlainTextEdit,
QVBoxLayout,
QWidget,
)
class SyntaxHighlighter(QSyntaxHighlighter):
def __init__(self, parent):
super(SyntaxHighlighter, self).__init__(parent)
self._highlight_lines = dict()
def highlight_line(self, line, fmt):
if isinstance(line, int) and line >= 0 and isinstance(fmt, QTextCharFormat):
self._highlight_lines[line] = fmt
tb = self.document().findBlockByLineNumber(line)
self.rehighlightBlock(tb)
def clear_highlight(self):
self._highlight_lines = dict()
self.rehighlight()
def highlightBlock(self, text):
line = self.currentBlock().blockNumber()
fmt = self._highlight_lines.get(line)
if fmt is not None:
self.setFormat(0, len(text), fmt)
class Widget(QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self._lineedit = QLineEdit(textChanged=self.onTextChanged)
regex_validator = QRegExpValidator(QRegExp(r"[0-9 ]+"))
self._lineedit.setValidator(regex_validator)
self._plaintextedit = QPlainTextEdit()
self._highlighter = SyntaxHighlighter(self._plaintextedit.document())
lay = QVBoxLayout(self)
lay.addWidget(self._lineedit)
lay.addWidget(self._plaintextedit)
for i in range(10):
self._plaintextedit.appendPlainText("line %d" % i)
self.resize(320, 240)
@pyqtSlot(str)
def onTextChanged(self, text):
fmt = QTextCharFormat()
fmt.setBackground(QColor("yellow"))
self._highlighter.clear_highlight()
for e in text.split():
line = int(e)
self._highlighter.highlight_line(line, fmt)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
![在此处输入图像描述](https://i.stack.imgur.com/qjmZN.png)