Python 3.x 突出显示PySide.qtexdit中的文本

Python 3.x 突出显示PySide.qtexdit中的文本,python-3.x,pyside,Python 3.x,Pyside,我想突出显示PySide的QTextEdit中显示的文本中的一个单词,我发现PyQt非常好并且可以工作,但是PySide的QTextEdit不起作用 有人知道PySide有什么问题吗 这是我上面提到的答案中的代码: from PyQt4 import QtGui from PyQt4 import QtCore class MyHighlighter(QtGui.QTextEdit): def __init__(self, parent=None): super(MyH

我想突出显示PySide的QTextEdit中显示的文本中的一个单词,我发现PyQt非常好并且可以工作,但是PySide的QTextEdit不起作用

有人知道PySide有什么问题吗

这是我上面提到的答案中的代码:

from PyQt4 import QtGui
from PyQt4 import QtCore

class MyHighlighter(QtGui.QTextEdit):
    def __init__(self, parent=None):
        super(MyHighlighter, self).__init__(parent)
        # Setup the text editor
        text = """In this text I want to highlight this word and only this word.\n""" +\
        """Any other word shouldn't be highlighted"""
        self.setText(text)
        cursor = self.textCursor()
        # Setup the desired format for matches
        format = QtGui.QTextCharFormat()
        format.setBackground(QtGui.QBrush(QtGui.QColor("red")))
        # Setup the regex engine
        pattern = "word"
        regex = QtCore.QRegExp(pattern)
        # Process the displayed document
        pos = 0
        index = regex.indexIn(self.toPlainText(), pos)
        while (index != -1):
            # Select the matched text and apply the desired format
            cursor.setPosition(index)
            cursor.movePosition(QtGui.QTextCursor.EndOfWord, 1)
            cursor.mergeCharFormat(format)
            # Move to the next match
            pos = index + regex.matchedLength()
            index = regex.indexIn(self.toPlainText(), pos)

if __name__ == "__main__":
    import sys
    a = QtGui.QApplication(sys.argv)
    t = MyHighlighter()
    t.show()
    sys.exit(a.exec_())

它完全适用于PyQt,但当我将导入更改为PySide时,它将停止突出显示单词。

我发现在PySide中,方法movePosition需要3个参数网,如果此方法如下所示,代码将是正确的:

cursor.movePosition(QtGui.QTextCursor.EndOfWord, QtGui.QTextCursor.KeepAnchor, 1)

PySide和PyQT的API工作方式应该几乎相同。因此,在许多情况下,您可以更改
导入
行,而无需更改其他内容。那么,它到底是如何“不起作用”的呢?你能展示一些代码吗?我知道它们彼此非常相似,但在这一段中,我认为有些东西是完全不同的。我添加代码,谢谢