Search code examples
c++qtcaretpaint

C++/Qt: drawing a caret


I'm learning Qt. I just started to write a text editor from scratch by inheriting QAbstractScrollArea. I'm doing this just for practice. But now I'm faced with the problem of displaying a caret. What comes to my mind is painter.drawLine and QTimer. Can you give some advices on this. I would also be glad hear some strategies to implement block and underscore caret.

Off Topic: Is this a bug? StackOverflow says there are two answers for this question but I'm seeing only one!


Solution

  • Take at a look at paintEvent() in QLineEdit. It has a timer that toggles the cursor on and off. The real cursor drawing is done via QTextLayout::drawCursor.

    For a text editor in general, have a look at QPlainTextEdit and QTextEdit.