Search code examples
syntax-highlightingqmlqt5qtquick2

Qt5 Syntax Highlighting in QML


I am working on a QtQuick 2.0 presentation and I would like to embed some code samples. is it possible easily to create a syntax highlighting QML element.

The objective in question is to apply syntax highlighting to a TextEdit or a TextArea. I know Qt has a QSyntaxHighlighter, but the documentation is not very clear on how exactly to apply it to a TextEdit or a TextArea.

Can you provide examples on how it's done?


Solution

  • Qt Quick's TextEdit item exposes a textDocument property, of type QQuickTextDocument. This is explicitly exposed so you can use QSyntaxHighlighter directly with the document.

    QtQuick textEdit documentation for Qt 5.3