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. Highlighting text in a QTextBrowser

Highlighting text in a QTextBrowser

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.1k Views
  • 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    I am implementing a search function for my help viewer based on QTextBrowser. What I would like to do, once a page has been loaded containing the search item text, is to highlight each of the occurrences of the text with background color = yellow. The user can then reload the document (hitting F5 calls the widget's reload() function) to remove the highlighting.

    It appears that I can fetch the text block from the QTextDocument and the corresponding QTextFormat easily enough, but it seems that there is no way to edit it (i.e., change the background color of a selection)?

    What would be the best way to implement this?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      If all you have is plain text you can just replace the formatting for the selected part like this:

      QTextCharFormat fmt;
      fmt.setBackground(Qt::yellow);
      
      QTextCursor cursor(textBrowser->document());
      cursor.setPosition(start_pos, QTextCursor::MoveAnchor);
      cursor.setPosition(end_pos, QTextCursor::KeepAnchor);
      cursor.setCharFormat(fmt);
      

      If you have some formatting you will have to iterate over text blocks in that range, get the format from them, modify the color and set back.

      1 Reply Last reply
      6
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Looks like a job for QSyntaxHighlighter.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        5
        • R Offline
          R Offline
          Robert Hairgrove
          wrote on last edited by
          #4

          @Chris-Kawa , @SGaist : Thank you very much for the suggestions. Will this also work for HTML text?

          Chris KawaC 1 Reply Last reply
          0
          • R Robert Hairgrove

            @Chris-Kawa , @SGaist : Thank you very much for the suggestions. Will this also work for HTML text?

            Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Robert-Hairgrove said in Highlighting text in a QTextBrowser:

            Will this also work for HTML text?

            Html is only an input/output format. Internally QTextBrowser stores content in QDocument the same whether it's plaint text, rich text, markdown or html.

            R 2 Replies Last reply
            3
            • Chris KawaC Chris Kawa

              @Robert-Hairgrove said in Highlighting text in a QTextBrowser:

              Will this also work for HTML text?

              Html is only an input/output format. Internally QTextBrowser stores content in QDocument the same whether it's plaint text, rich text, markdown or html.

              R Offline
              R Offline
              Robert Hairgrove
              wrote on last edited by
              #6

              @Chris-Kawa Thanks! Now I need to test this.

              1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                @Robert-Hairgrove said in Highlighting text in a QTextBrowser:

                Will this also work for HTML text?

                Html is only an input/output format. Internally QTextBrowser stores content in QDocument the same whether it's plaint text, rich text, markdown or html.

                R Offline
                R Offline
                Robert Hairgrove
                wrote on last edited by
                #7

                @Chris-Kawa ... Thank you, this works like a charm!

                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