QTextEdit highlight all characters in a given range



  • Moin!

    I have a QTextEdit containing plain text and a regular expression. The task is to draw all characters that belong to a (sub)match in a different colour with a distinct colour for each level of nesting. What I already have is a sequence of ([start_position, stop_position), colour) with the positions in the text that must me highlighted.

    I've tried to accomplish this in roughly "this way":http://paste.pocoo.org/show/309477/ .

    The problem with this approach is twofold:

    First if someone enters text after "is" the text is painted with the same brush as the highlighted text. But it should be painted in a "normal" brush (i.e. with colour black). To solve this, the hole text is erased at every change to regex or text and first inserted with normal brush, after this specific ranges are highlighted. This leads to the second problem: The cursor always jumps to the end of the text, when text is inserted, although I'm setting the position to the position before the text has been changed.

    Can someone point me to a better solution to highlight the submatches or explain the behaviour of QTextCursor?



  • Did you already have a look at "QSyntaxHighlighter":http://doc.qt.nokia.com/stable/qsyntaxhighlighter.html? It looks that this can at least solve some of your problems.



  • I had a look at it. But I'm not sure, if using QSyntaxHighlighter solves my problems. As far as I can tell, QSyntaxHighlighter calls highlightBlock(text) for a block, if it is i.e. changed. But in my program the highlighting must be triggered when either the text or the regex is changed. Furthermore a match may span several blocks* and I would have to program around this. Last but not least I now the positions (in terms of QString[pos]) of the words that must be highlighted. How can I map these positions the QTextBlocks in an eay way?

    • Don't know much about QTextDocument


  • edit->textCursor() returns a copy of the cursor. This means that if you change your variable cursor, this does not affect the text edit. You must set back the cursor with edit->setTextCursor(cursor) in order to put the cursor back where it was before.

    Regarding the highlighting itself: With your approach you must redo all of calculations once the user has changed the text - even if he/she only added a single character. This could add significant delays if the text is reasonably long.

    You do the highlighting itself by setting a QTextCharFormat, that spans all characters. If you add new characters in between they always get the same format as its surrounding neighbors.

    For your text cursor/text document questions, you might find these methods helpful:

    "QTextDocument::findBlock()":http://doc.qt.nokia.com/4.7.old/qtextdocument.html#findBlock
    "QTextCursor::setCharFormat()":http://doc.qt.nokia.com/4.7.old/qtextcursor.html#setCharFormat

    Feel free to ask further questions. That QTextDocument thingy is a wild beast sometimes :-)

    [EDIT] PS:
    It's completely ok to paste short code snippets directly here in the forum. Just wrap them into two @-chars, which does some nice formatting for you.



  • [quote author="Volker" date="1293233344"]edit->textCursor() returns a copy of the cursor. This means that if you change your variable cursor, this does not affect the text edit. You must set back the cursor with edit->setTextCursor(cursor) in order to put the cursor back where it was before.
    [/quote] Thanks, that helps.
    [quote]
    Regarding the highlighting itself: With your approach you must redo all of calculations once the user has changed the text - even if he/she only added a single character. This could add significant delays if the text is reasonably long.
    [/quote]
    That is true and I am aware of this. Not sure how I could change it though. If you add a character
    the regex must be reevaluated or you can't be sure, if you are still right. Since this is only a pet project, i will only investigate further, when I really need to.

    [quote]
    Feel free to ask further questions. That QTextDocument thingy is a wild beast sometimes :-)
    [/quote]

    Thanks and merry christmas


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.