Question

Let me say first, that I have been stuck on this problem for several days. In a dialog, we have a QTextEdit with editable "tokens" (characters, or pair of characters). These tokens need to be emphasized and connected with a QHash<int, MyType *>.

In practice, this means, that parts of the text need to be emphasized and identifiable.

Example:

Hello Doll y !


So far, we have come up with a solution based on QTextEdit's implementation of HTML. After selecting the token with QTextCursor, the selected text would be deleted and replaced with an anchor with it's href attribute being the ID. Like so:

Hello doll<a href="1">y</a>!

Of course, the HTML insertion is not a problem. Neither the detection of the token inside the entire HTML. This standpoint runs into a wall at the moment you try to jump to a plain-text cursor location based on a location in the HTML.


The question is:

  1. How do I get the plain-text positions of a token with a given ID
  2. How do I get token's ID based on specific plain-text position

Any help would be much appreciated, even if it was a completely different solution compared to ours.

Was it helpful?

Solution

I am a fan of the QSyntaxHighlighter. I think it maybe an easier solution for this.

http://qt-project.org/doc/qt-5/qtwidgets-richtext-syntaxhighlighter-example.html

If the usage of the highlighter doesn't match what you are looking for, I would look into using the Rich Text Processing of Qt. It is made to do exactly what you are looking for and much more. But the learning curve is a little steeper.

http://qt-project.org/doc/qt-5/richtext.html

http://qt-project.org/doc/qt-5/examples-richtext.html

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top