Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Thanks.

like image 570
antoyo Avatar asked Nov 13 '22 17:11

antoyo


1 Answers

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.

like image 199
Mat Avatar answered Dec 18 '22 20:12

Mat