poppy
poppy

Reputation: 277

Multiple visual text cursors in QPlainTextEdit

In Sublime Text, if you hold control while clicking, you can add another cursor to the document, allowing you to type and edit in multiple places at once. I'm trying to recreate this with Qt's QPlainTextEdit. The logic seems pretty simple...

However, it seems that in order to do these things, I have to understand exactly how QPlainTextEdit works, and while I have a loose understanding, I find the source to be very dense and difficult to navigate.

While this feature is very important to me, I can't afford to spend a month on it. I don't know how to reasonably proceed. Is there some open source text editor built with Qt that has this feature? Can I in fact implement it without thoroughly understanding how QPlainTextEdit works? Is there some other solution I have not considered? I thought this would be trivial, or at the very least someone might have solved it before, but that doesn't appear to be the case. Any advice is appreciated.

Upvotes: 2

Views: 1391

Answers (3)

Jim
Jim

Reputation: 638

Nowadays QtCreator has full multi-cursor features:

  • alt+click and alt+shift+up/down to add a cursor
  • alt+double click to select word and add a cursor
  • ctrl+d to select next selection occurrence and add a cursor

and more: https://doc.qt.io/qtcreator/creator-how-to-insert-multiple-cursors.html

Upvotes: 0

Boris Dalstein
Boris Dalstein

Reputation: 7738

Is there some open source text editor built with Qt that has this feature?

QtCreator is open-source (source code) and has partial multiple-cursor functionality. While it does not support Ctrl+Click to add a new cursor, you can do Shift+Alt+DownArrow to add a cursor to the following line (same column), like in SublimeText, and then insert text to all the lines at once.

It seems implemented in their class TextEditorWidget, which inherits QPlainTextEdit and reimplements plenty of its virtual methods. I don't exactly know how they implemented the multi-cursor functionality, but probably looking at the code of TextEditorWidget can give hints.

However, keep in mind that QtCreator does not have full multi-cursor functionality like in Sublime Text or CLion: the mutliple cursors are necessarily in the same column, and you cannot move the multiple cursors left/right. For example something I often do in Sublime is multiple selection at the beginning of lines, followed by Ctrl+RightArrow to jump all cursors "one word to the right". You cannot do that in QtCreator as of August 2020.

There is an open feature request for having true multiple-cursor functionality in QtCreator:

https://bugreports.qt.io/browse/QTCREATORBUG-16013

Upvotes: 2

Rahul Iyer
Rahul Iyer

Reputation: 20995

However, it seems that in order to do these things, I have to understand exactly how QPlainTextEdit works, and while I have a loose understanding, I find the source to be very dense and difficult to navigate.

To some extent, but not a lot. Once you get the cursor positions, you know the positions in the document where text needs to be inserted. QTextEdit already supports multiple cursor positions:

https://doc.qt.io/qt-5/richtext-cursor.html

Rich text documents can have multiple cursors associated with them, and each of these contains information about their position in the document and any selections that they may hold. This cursor-based paradigm makes common operations, such as cutting and pasting text, simple to implement programmatically, yet it also allows more complex editing operations to be performed on the document.

and

Multiple Cursors

Multiple cursors can be used to simultaneously edit the same document, although only one will be visible to the user in a QTextEdit widget. The QTextDocument ensures that each cursor writes text correctly and does not interfere with any of the others.

Since this feature already exists, it may not be as difficult as thought to read the documentation and implement the feature.

As QPlainTextEdit is implemented by using most of the tech behind QTextEdit and QTextDocument, maybe it will meet your requirements.

Upvotes: 1

Related Questions