在 QPlainTextEdit 中突出显示部分文本

Highlighting portions of text in QPlainTextEdit

我在 QPlainTextEdit 中有一个列表,想要突出显示特定行,比如第 5 行。

看了各种教程和例子,PyQt5的文档好像比较少

可行的代码,我想突出显示行。 对于另一个小部件的建议,我也很感激。我不想编辑此列表,只是显示它并突出显示行。

import sys
from PyQt5.QtWidgets import *

app = QApplication(sys.argv)
main = QWidget()
main.resize(250, 150)
main.size
tbox = QPlainTextEdit(main)
for nr in range(1,5):
    tbox.appendPlainText('%d'%nr)

## highlight line 2
## wait a while
## unhighlight line 2
## highlight line 4
main.show()
sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import (QPlainTextEdit, QApplication, QWidget)

app = QApplication(sys.argv)
main = QWidget()
main.resize(250, 150)
main.size
tbox = QPlainTextEdit(main)
condition = 0
for nr in range(1, 5):
    if condition % 2 == 0:
        tbox.appendHtml(f"<span style='background-color: yellow;'>{nr}</p>")
    else:
        tbox.appendHtml(f"<span style='background-color: white;'>{nr}</p>")
    # tbox.appendPlainText('%d' % nr)
    condition = condition + 1
main.show()
sys.exit(app.exec_())

如果突出显示取决于文本,那么 @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_())