PDA

View Full Version : painting SVGs like text



hartmut
7th January 2014, 15:59
I have written a QItemDelegate :: paint() function of a QTableView, where I draw a mixture of text and small symbols. Some of the symbols come from a symbol font but others have to be provided by my program. Like the text, the symbols are monochrome and should be displayed with the current pen color and background color, which change with the selection state of the table cell. What would be the best strategy?

I used to define the symbols in bitmap ressources where I used only three colors: black, white and gray (to create a basic anti aliasing) and I manually converted these three colors pixel by pixel to the current pen, backgrould and average color.
Now I would like to find a scalable solution with SVG graphics and true antialiasing. Do I really have to edit the color values of the SVG XML code at runtime (as described here http://www.qtcentre.org/archive/index.php/t-53946.html ) or is there a simpler solution?
I also thought of creating a font instead of SVG graphics. Is it possible to use a "private" font, which is not installed on the system? It should work under Windows and on a Mac.

ChrisW67
7th January 2014, 21:47
I used to define the symbols in bitmap ressources where I used only three colors: black, white and gray (to create a basic anti aliasing) and I manually converted these three colors pixel by pixel to the current pen, backgrould and average color.

If you can code drawing the symbols then you could paint fully anti-aliased versions of them in the correct colours in the paint() method of the delegate. QPixmapCache could also be useful to minimise reconstruction. You could also look into how you might use a monochrome mask with the image composition operations in Qt to produce your coloured foreground image.


Now I would like to find a scalable solution with SVG graphics and true antialiasing. Do I really have to edit the color values of the SVG XML code at runtime (as described here http://www.qtcentre.org/archive/index.php/t-53946.html ) or is there a simpler solution?

That is how SVG specifies colours, so that is how you change them. It need not be difficult:

Create the SVG.
Edit the file and replace the colour codes with markers, e.g. {{bg}} (or entities).
Ship this file with your project.
Load the SVG file once into a QByteArray at program start.

When you need the SVG,

Copy the array
Use QByteArray::replace() to put your desired colours into the SVG
Feed the result to QSvgRenderer.

Caching the results would be a good idea.

I also thought of creating a font instead of SVG graphics. Is it possible to use a "private" font, which is not installed on the system? It should work under Windows and on a Mac.

Yes. QFontDatabase::addApplicationFont(). The font file can come from a local file or Qt resource system.

hartmut
8th January 2014, 14:50
Thanks a lot. I tried the font option and it works very well. That's a really elegant solution.