I have a custom Qt widget which I used to display disassembly and I am looking to add syntax coloring to it.
Currently, I simply set the QPen to a solid color, construct the text I want to display, and render it to the QPainter at the appropriate coordinates.
The question is, what is the best approach to adding syntax coloring? I've thought of a few:
- I could simply divide the coloring into logical blocks, each preceded by setting the QPen to the desired color.
- I could have special escape characters which represent a change in the color palette, and render 1 character at a time.
- I could do a modification of #1 and create a list of std::pair, then I could simply iterate the list setting the color and drawing the text as I pop items off the front of the list.
- Something entirely different?
I know that each of the 3 approaches I've listed will technically work, but I'm looking for a very efficient solution. This code will be called a lot. And since this is an interactive debugger, if this code is slow, someone rapidly stepping or tracing will see a visible slowdown.
EDIT: I'm aware of QSyntaxHighlighter and QTextDocument. The main issue is that these don't generally suite my purposes very well. I have several columns which all have dividers and can be slid back and forth. To give you an idea, Here's a link to a screenshot of my debugger. As you can see it isn't really like a text document at all. In fact it is closer to a list or table. But there is already a bunch of custom drawing going on making a normal QTextDocument somewhat impractical.
EDIT: I was incorrect, It seems that QTextDocument can render directly to a QPainter. Looks like what I need!
EDIT: It is unclear how to control where and how QTextDocument or QTextLayout will draw on a QPainter. I've attempted to use them to no avail. So if someone could provide a rudimentary example, that would be very helpful.