PyQt4 QTextCursor更改可选字符

时间:2015-05-22 14:23:42

标签: pyqt qt4 qtextedit qplaintextedit qtextcursor

如何更改QTextCursor的可选字符,例如添加点? 例如,输入" MyClass"在QPlainTextEdit节中

tc = self.textCursor()
tc.select(QtGui.QTextCursor.WordUnderCursor)
return tc.selectedText()

将返回" MyClass",但输入" MyClass。"将返回一个空的Qstring! 问题仍然存在,进入" MyClass.myMeth"只会回来" myMeth",但我需要" MyClass.myMeth" :/ 感谢

1 个答案:

答案 0 :(得分:1)

好的,我通过以下方式替换对WordUnderCursor的调用找到了解决方案:

def textUnderCursor(self):
        tc = self.textCursor()
        isStartOfWord = False
        if tc.atStart() or (tc.positionInBlock() == 0):
            isStartOfWord = True
        while not isStartOfWord:
            tc.movePosition(QtGui.QTextCursor.PreviousCharacter, QtGui.QTextCursor.KeepAnchor)
            if tc.atStart() or (tc.positionInBlock() == 0):
                isStartOfWord = True
            elif QtCore.QChar(tc.selectedText()[0]).isSpace():
                isStartOfWord = True
        return tc.selectedText().trimmed()