PDA

View Full Version : Is it possible to change color of a QGraphicsSvgItem ?



Guiom
29th March 2013, 09:25
All is in the title...
How can i change the color of a QGraphicsSvgItem ?

I have test to reimplement the paint method and change color of Brush and Pen but the item keep the color defined in the svg file...

Is it possible ?

Thanks

ChrisW67
30th March 2013, 06:02
Change the colour specified in the SVG XML and load the item again.

Guiom
30th March 2013, 08:09
Thank for your answer.

I was hoping a simple solution :(.
So I need to specialize a QGraphicsSvgItem to add the color managment and use QSvgRenderer to load again the SVG..

Guiom
31st March 2013, 19:22
If anyone is interested, i have created his class

graphicscolorsvgitem.h


#ifndef GRAPHICSCOLORSVGITEM_H
#define GRAPHICSCOLORSVGITEM_H

#include <QGraphicsSvgItem>
#include <QDomDocument>
#include <QList>

class GraphicsColorSvgItem : public QGraphicsSvgItem
{
public:
GraphicsColorSvgItem(QString svgContent, QGraphicsItem *parent = 0);
virtual ~GraphicsColorSvgItem();

void setColor(QColor c);

private:
QDomDocument _svgXML;

void changeAttributes(QString attName, QString attValue);
void recursiveChangeAttributes(QDomNode node, QString attName, QString attValue);
};

#endif // GRAPHICSCOLORSVGITEM_H


graphicscolorsvgitem.cpp


#include "graphicscolorsvgitem.h"

#include <QSvgRenderer>
#include <QRgb>
#include <QDebug>

GraphicsColorSvgItem::GraphicsColorSvgItem(QString svgContent, QGraphicsItem *parent) :
QGraphicsSvgItem(parent),
_svgXML()
{
_svgXML.setContent(svgContent);
setSharedRenderer(new QSvgRenderer(_svgXML.toByteArray()));
}

GraphicsColorSvgItem::~GraphicsColorSvgItem()
{
delete renderer();
}

void GraphicsColorSvgItem::setColor(QColor c)
{
changeAttributes("fill", c.name().toUpper());
changeAttributes("stroke", c.name().toUpper());
renderer()->load(_svgXML.toByteArray());
}

void GraphicsColorSvgItem::changeAttributes(QString attName, QString attValue)
{
QDomElement rootElem = _svgXML.documentElement();

QDomNode n = rootElem.firstChild();
while(!n.isNull())
{
if(n.isElement())
{
QDomElement e = n.toElement();
if(e.hasAttribute(attName))
e.setAttribute(attName, attValue);

if(n.hasChildNodes())
recursiveChangeAttributes(n.firstChild(), attName, attValue);
}
n = n.nextSibling();
}
}

void GraphicsColorSvgItem::recursiveChangeAttributes(QD omNode node, QString attName, QString attValue)
{
QDomNode n = node;
while(!n.isNull())
{
if(n.isElement())
{
QDomElement e = n.toElement();
if(e.hasAttribute(attName))
e.setAttribute(attName, attValue);

if(n.hasChildNodes())
recursiveChangeAttributes(n.firstChild(), attName, attValue);
}
n = n.nextSibling();
}
}


If you have some remarks, I'm open.

hawkroot
29th June 2014, 23:07
Hello!

I need to change line color in my SVG file.
I using this class, but it doesn't work. I don't see image on my Item.

itemSVG = new GraphicsColorSvgItem(QString(":/default/measure_menu_time/default.svg"));
itemSVG->setColor(QColor("#FFFFFF"));

itemSVG->setPos(30,3);
itemSVG->setParentItem(this);
itemSVG->setOpacity(0.8);
itemSVG->setScale(0.068);

Thanks!

ChrisW67
30th June 2014, 03:52
Does this resource, ":/default/measure_menu_time/default.svg", exist?
Does it show if you use a generic QGraphicsSvgItem?
Have you added the item to your scene?
Is the location (30,3) inside the view?