从QTextEdit中删除QSyntaxHighLighter

时间:2013-06-15 11:25:26

标签: python pyqt4

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

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

但它无法正常工作

设置为:

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

我正在使用此突出显示来标记搜索到的字词,但在下一次搜索中,之前的结果仍会突出显示

任何想法?

@Edit

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)

0 个答案:

没有答案