pythonpyqt5cursor-positionqscintilla

Move text-cursor of QsciScintilla to the left or right in PyQt5


I was wondering what specific commands I can execute to move a text cursor in QsciScintilla to the left or right? I know for a QPlainTextEdit, you can execute the commands:

self.textEdit.moveCursor(QTextCursor.Left)

or:

self.textEdit.moveCursor(QTextCursor.Right)

Are there any similar commands for QsciScintilla?

I tried:

# left cursor movement
line, index = self.sci.getCursorPosition()
if index == 0 and line != 0:
   #move to back line
elif index != 0: 
   self.sci.setCursorPosition(line, index - 1)

Solution

  • There's two ways to do this:

    1. Using the low-level api to send keyboard commands directly to the underlying scintilla editor:

      • Move Left

          self.sci.SendScintilla(Qsci.QsciScintillaBase.SCI_CHARLEFT)
        
      • Move Right

          self.sci.SendScintilla(Qsci.QsciScintillaBase.SCI_CHARRIGHT)
        
    2. Using the high-level api to set the line and index explictly:

      • Move Left

          line, index = self.sci.getCursorPosition()
          if index:
              self.sci.setCursorPosition(line, index - 1)
          elif line:
              self.sci.setCursorPosition(
                  line - 1, self.sci.lineLength(line - 1) - 1)
        
      • Move Right

          line, index = self.sci.getCursorPosition()
          if index < self.sci.lineLength(line):
              self.sci.setCursorPosition(line, index + 1)
          elif line < self.sci.lines():
              self.sci.setCursorPosition(line + 1, 0)