pythonpyqtpyqt6qscintilla

Extra wide CARET in QScintilla and PyQt6


I am building a code editor in QScintilla and PyQt6. I wanted to create an extra wide CARET (blinking cursor) like in terminal:

see image

When I try modifying the CARET width using the default method

editor = QScintilla()
editor.setCaretWidth(15)

it has a fixed maximum effect, meaning that any number bigger than 12px will not change the width anymore. Is there a way to make the CARET even wider?


Solution

  • Setting the caret width is inappropriate, because that sets the width of the vertical caret, which obviously presents two issues:

    We need another, and more appropriate solution.

    QScintilla is a Qt port of Scintilla, and, similarly to PyQt, its documentation is either non existent or irrelevant, if not misleading (as it is for PySide in most cases).

    While for Qt Python bindings it's normally enough to rely on the official C++ docs, QScintilla forces us to use the original Scintilla documentation.

    The Scintilla interface works through "messages" sent to its control, with the first parameter being the target parameter/option/etc. and the following being the possible arguments.

    Most of the general use functions are already implemented in QScintilla (and its Python binding), but for anything else, it's necessary to use the above interface through the SendScintilla() function, implemented in the QsciScintillaBase, from which QsciScintilla inherits. Most of the QScintilla functions are actually wrappers around that functions.

    By looking at the documentation above, we can find that it is possible to use the SCI_SETCARETSTYLE message, followed by the wanted style, that also includes a CARETSTYLE_BLOCK.

    Therefore, the solution is relatively simple and effective, once you know how Scintilla works:

    editor.SendScintilla(editor.SCI_SETCARETSTYLE, editor.CARETSTYLE_BLOCK)