[PyQt] changing SVG group rendering color

Jason H scorp1us at yahoo.com
Fri Apr 16 16:50:23 BST 2010


Qt does not support modifying SVG content (without modifying and reparsing the file)



----- Original Message ----
From: John Ossenfort <johnossenfort at yahoo.com>
To: pyqt at riverbankcomputing.com
Sent: Thu, April 15, 2010 1:37:55 PM
Subject: [PyQt] changing SVG group rendering color

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

class SvgElement(QtSvg.QGraphicsSvgItem):
    def __init__(self, name, renderer):
        QtSvg.QGraphicsSvgItem.__init__(self)
        self.setElementId(name)
        self.setSharedRenderer(renderer)
        self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True); 

class SvgViewerPane(QtGui.QGraphicsScene):
    def __init__(self, SVG_FILE, parent=None):
        QtGui.QGraphicsScene.__init__(self, parent)
        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)
makeGroupList(tree.getroot(), group_list)

    for element in group_list:
        n = SvgElement(element.get("id"), self.rend)
        self.addItem(n)

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!
John


      
_______________________________________________
PyQt mailing list    PyQt at riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt



      


More information about the PyQt mailing list