突出显示QPlainTextEdit中的文本部分

时间:2019-08-24 08:27:37

标签: python pyqt pyqt5 highlight 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_())

2 个答案:

答案 0 :(得分:2)

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

enter image description here

答案 1 :(得分:1)

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_())

screenshot