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 search
Forum Updated to NodeBB v4.3 + New Features

Highlighting search

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.1k 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.
  • D Offline
    D Offline
    dvillalobos
    wrote on last edited by
    #1

    Hi all,

    I want to know how can I highlight a text like Qt Creator does when is searching text.

    Best regards.
    David

    Regards

    David

    1 Reply Last reply
    0
    • A Offline
      A Offline
      akonradwesolutions
      wrote on last edited by
      #2

      Take a look at [[Doc:QSyntaxHighlighter]]. If you display text using a [[Doc:QTextDocument]] or [[Doc:QTextEdit]] this might just be what you need.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dvillalobos
        wrote on last edited by
        #3

        Hi Arnold,

        Thanks for the reply, I use QTextDocument, what I do not know how to is show the yellow box when text is found. Qt Creator uses QSyntaxHighlighter?

        Regards

        David

        1 Reply Last reply
        0
        • A Offline
          A Offline
          akonradwesolutions
          wrote on last edited by
          #4

          I cannot say what QtCreator actually uses, but if you display your text using a [[Doc:QTextEdit]] then my suggestion would be that you subclass [[Doc:QSyntaxHighlighter]] and reimplement the highLightBlock method. This method will be called automatically once it is attached to a [[Doc:QTextDocument]]. The idea is that the syntax highlighter knows your search term and can apply a [[Doc:QTextCharFormat]] to the appropriate substring:

          @
          void MySyntaxHighlighter::highLightBlock(const QString& text)
          {
          int index = text.indexOf(searchTerm);
          if (index != -1) {
          QTextCharFormat currentFormat = format(0);
          currentFormat.setBackground(Qt::yellow);
          setFormat(index, searchTerm.length(), currentFormat);
          }
          }
          @

          The code above is untested and shall only give you a basic idea. It is of course a bit oversimplified - it for example does not take into account that there might be more than one occurrence of the search term in a block of text.

          Hope this helps.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dvillalobos
            wrote on last edited by
            #5

            Yes, it helps.

            Thanks.

            Regards

            David

            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