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. I'd like to highlight keywords in a QTextEdit
Forum Updated to NodeBB v4.3 + New Features

I'd like to highlight keywords in a QTextEdit

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.0k 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.
  • C Offline
    C Offline
    conatic
    wrote on last edited by
    #1

    I'd like to do this :

    @
    highlighter = new Highlighter(ui->editor);
    highlighter->addKeyword(ui->textEdit->toPlainText());
    highlighter->setKeywordColor(Qt::blue);

    ui->editor->setPlainText(txt);
    @

    but I can't manage to do it. Does anyone could help me ?

    conatic

    http://www.fulgor-it.info

    conatic

    [EDIT: code formatting, please wrap in @-tags, Volker]

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

      Hi and welcome to DevNet.

      Please wrap code snippets in @-tags, they are formatted nicely then and are much better to read. I've changed that for you, but it would be nice if you add them yourself in the future.

      Regarding your questione: I don't get what you want to do and what the actual problem is. Could you rephrase/explain a bit more, please.

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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        conatic
        wrote on last edited by
        #3

        thanks for advice. I'd like to use Syntax Highlighting in QTextEdit. I 'd like to highlight only a few keywords, the result of a search I'd like to make a class derived from QSyntaxHighlighter and a method to add the keywords to highlight. I have googled but I don't find exactly what I want.

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

          Did you read the documentation of QSyntaxHighlighter::highlightBlock()?
          That is the key function to re-implement. You get the text of your document by block. That means that it can contain multiple words or sentences. The documentation example uses a regexp to highlight, but you can use any method you want. If you give your subclass a few methods to manipulate the list of keywords it needs to highlight, it should be easy enough to adapt the exaple to match against those keywords and set the highlighting for those. What did you try so far?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            conatic
            wrote on last edited by
            #5

            Not yet, I was in hurry so I have tried a QScintilla widget it works I can look for words.

            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