Using multiple QSyntaxHighlighter in one QPlainTextEdit


I have written two syntax highlighter: one for CSS and one for HTML.
They work well.

But, I want to color CSS code in HTML code (inside <style> tag), so I want to be able to use multiple QSyntaxHighlighter in one QPlainTextEdit.

Is there a way to do what I want?
I cannot instantiate two QSyntaxHighlighter objects on the same QPlainTextEdit because only the second object will be used.



You’ll need to handle switching between the different highlighting modes yourself, within a single syntax highlighter.

I’d start with your HTML highlighter, since it should be able to tell when you’re inside a <style> tag. Use the setCurrentBlockState to mark that block as CSS, and delegate to the CSS renderer.

The basic idea is illustrated in the QSyntaxHighlighter docs (switching between comment/code modes) and the example.

Answered By – Mat

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.