2

我正在尝试删除在 QTextEdit 上设置的 QSyntaxHighLighter,我尝试:

 hig = self.textEdit.findChildren(QSyntaxHighlighter)
 del hig

但它不起作用

它设置为:

highlight = syntax.PythonHighlighter(self.textEdit, (self.le.text()))

我正在使用此突出显示来标记搜索的单词,但在下一次搜索中,以前的结果仍然突出显示

有任何想法吗?

@编辑

class PythonHighlighter (QSyntaxHighlighter):
keywords = []
def __init__(self, document, pattern):
    QSyntaxHighlighter.__init__(self, document)

    self.keywords.append(pattern)

    rules = []

    rules += [(r'%s' % w, 0, STYLES['keyword'])
        for w in PythonHighlighter.keywords]

    self.rules = [(QRegExp(pat), index, fmt)
        for (pat, index, fmt) in rules]



def highlightBlock(self, text):
    """Apply syntax highlighting to the given block of text.
    """
    # Do other syntax formatting
    for expression, nth, format in self.rules:
        index = expression.indexIn(text, 0)

        while index >= 0:
            # We actually want the index of the nth match
            index = expression.pos(nth)
            length = expression.cap(nth).length()
            self.setFormat(index, length, format)
            index = expression.indexIn(text, index + length)
    self.keywords = []
    self.setCurrentBlockState(0)
4

0 回答 0