[PyQt] changing SVG group rendering color
johnossenfort at yahoo.com
Thu Apr 15 18:37:55 BST 2010
Hi list members!
I've been hacking away at this for quite some time and found no good examples of what I am trying to do on Google, so I hope that someone can point me in the right direction here (I am also very new to Python, so I will also accept ANY tips on pythonic style, code, etc.)
What I want to do is display groups from an SVG file, and then individually and dynamically change the colors for some groups but not others after certain mouse events. My original idea was to read in the groups as QGraphicsSvgItems, render them, and then change the brush colors and redraw them as needed. I'm thinking now that maintaining a reference to the original file precludes any later brush/pen changes unless I use a QSvgGenerator to write out an entirely new SVG file with the color changes embedded! There don't seem to be any functions related to brush color changes for QGraphicsSvgItems to help with this. Another thought was trying to convert the SVG shapes to pixmaps and then applying brush/pen changes, but I have been unable do that as well.
Here is some of my code to hopefully show the direction I have taken:
from xml.etree import ElementTree
from PyQt4 import QtGui
from PyQt4 import QtSvg
def __init__(self, name, renderer):
def __init__(self, SVG_FILE, parent=None):
self.rend = QtSvg.QSvgRenderer(SVG_FILE)
""" Produce a list of group Elements from the SVG, each containing an id and multiple 'path' attributes """
tree = ElementTree.parse(SVG_FILE)
for element in group_list:
n = SvgElement(element.get("id"), self.rend)
OK, so that works fine to display all the groups and re-build the whole picture in place (I do some additional coding to get the position of each group using a 'renderer.boundsOnElement' call). But now when I want to redraw a group with a color I am at a loss! I tried to overwrite the paint function for SvgElement thinking that if I could render the original image in alternate colors even once it would be a start, but nothing is displayed at all. All attempts to associate a QPainter with each Svg item have failed as well, but probably just due to my lack of understanding ;-)
Any help is much appreciated!
More information about the PyQt