Search code examples
pythonpyqtqtextedit

QtWidgets.QTextEdit.textCursor().movePosition(); how to define the exact number of characters?


A method to find a substring and highlight it in QTextEdit has already been proposed in stackoverflow by vicent.

This is an elegant and effective method. However as it is used:

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

the highlighting goes to the end of the word where the substring is detected.

I am looking for a way to define the exact number of characters. Something like this (doesn't work!):

cursor.movePosition(len(pattern), 1)

to highlight only the substring.


Solution

  • Use a for-loop with QTextCursor::Right:

    for _ in pattern:
        cursor.movePosition(QtGui.QTextCursor.Right, 1)