如何从QSyntaxHighlighter中的QTextCharFormat获取工作setToolTip?

2024-09-27 21:35:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经实现了非常简单的语法highlighter,并将其与QTextEdit一起使用。你知道吗

class MyHighlighter(QtGui.QSyntaxHighlighter):

    def __init__(self, parent):
        QtGui.QSyntaxHighlighter.__init__(self, parent)

        self.Rules = []

        classFormat = QtGui.QTextCharFormat()
        classFormat.setFontWeight(QtGui.QFont.Bold)
        classFormat.setForeground(QtCore.Qt.darkMagenta)
        classFormat.setToolTip("this is very important!")

        self.Rules.append(
                ('keyword', classFormat)
            )

    def highlightBlock(self, text):

        for pattern, classFormat in self.Rules:
            expression = re.compile(pattern)
            for match in re.finditer(expression, text):
                index = match.start()
                length = match.end() - index
                self.setFormat(index, length, classFormat)

语法高亮显示正确设置文本格式,但工具提示不可用。根本看不见。你知道吗

我在一份旧的bug报告中发现了一些描述类似行为的错误,但似乎没有解决上述问题的方法: https://bugreports.qt.io/browse/QTBUG-21553

我如何解决这个问题以使工具提示正常工作?你知道吗

我想我可以在QTextEdit中使用html标记。但我不喜欢这个想法,因为它会增加文本预处理的复杂性(我正在处理大文件)。我也做了一些实验,看起来也很棘手。你知道吗


Tags: textselfforindexinitdefmatch语法

热门问题