Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextEdit highlight all characters in a given range
Forum Updated to NodeBB v4.3 + New Features

QTextEdit highlight all characters in a given range

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Panke
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Panke
        wrote on last edited by
        #3

        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
        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          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.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Panke
            wrote on last edited by
            #5

            [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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved