Important: Please read the Qt Code of Conduct -

I'd like to highlight keywords in a QTextEdit

  • I'd like to do this :

    highlighter = new Highlighter(ui->editor);


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



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

  • 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.

  • 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.

  • 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?

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

Log in to reply