0

How can I change the selectable characters of a QTextCursor, like adding a dot? For example entering "MyClass" in a QPlainTextEdit the stanza

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

will returns "MyClass", but entering "MyClass." will returns an empty Qstring! The problem continues, entering "MyClass.myMeth" will just returns "myMeth", but I need "MyClass.myMeth" :/ Thanks

LionelR
  • 361
  • 1
  • 3
  • 12
  • The behaviour depends on the position of the cursor. If it's after the dot, nothing will be selected; if it's before the dot, the dot will be selected; and if it's anywhere else, `MyClass` will be selected. So `WordUnderCursor` is very poorly named, since it clearly isn't selecting words (or at least, not *only* words). What it appears to be doing is selecting runs of characters between boundaries - although it's not clear how the boundaries are defined. – ekhumoro May 22 '15 at 17:25

1 Answers1

1

Ok, I find a solution by replacing the call to WordUnderCursor by :

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()
LionelR
  • 361
  • 1
  • 3
  • 12