Results 1 to 6 of 6

Thread: Is it possible to change color of a QGraphicsSvgItem ?

  1. #1
    Join Date
    Apr 2012
    Location
    Paris
    Posts
    11
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Is it possible to change color of a QGraphicsSvgItem ?

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to change color of a QGraphicsSvgItem ?

    Change the colour specified in the SVG XML and load the item again.

  3. The following user says thank you to ChrisW67 for this useful post:

    Guiom (1st April 2013)

  4. #3
    Join Date
    Apr 2012
    Location
    Paris
    Posts
    11
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to change color of a QGraphicsSvgItem ?

    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..

  5. #4
    Join Date
    Apr 2012
    Location
    Paris
    Posts
    11
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to change color of a QGraphicsSvgItem ?

    If anyone is interested, i have created his class

    graphicscolorsvgitem.h
    Qt Code:
    1. #ifndef GRAPHICSCOLORSVGITEM_H
    2. #define GRAPHICSCOLORSVGITEM_H
    3.  
    4. #include <QGraphicsSvgItem>
    5. #include <QDomDocument>
    6. #include <QList>
    7.  
    8. class GraphicsColorSvgItem : public QGraphicsSvgItem
    9. {
    10. public:
    11. GraphicsColorSvgItem(QString svgContent, QGraphicsItem *parent = 0);
    12. virtual ~GraphicsColorSvgItem();
    13.  
    14. void setColor(QColor c);
    15.  
    16. private:
    17. QDomDocument _svgXML;
    18.  
    19. void changeAttributes(QString attName, QString attValue);
    20. void recursiveChangeAttributes(QDomNode node, QString attName, QString attValue);
    21. };
    22.  
    23. #endif // GRAPHICSCOLORSVGITEM_H
    To copy to clipboard, switch view to plain text mode 

    graphicscolorsvgitem.cpp
    Qt Code:
    1. #include "graphicscolorsvgitem.h"
    2.  
    3. #include <QSvgRenderer>
    4. #include <QRgb>
    5. #include <QDebug>
    6.  
    7. GraphicsColorSvgItem::GraphicsColorSvgItem(QString svgContent, QGraphicsItem *parent) :
    8. _svgXML()
    9. {
    10. _svgXML.setContent(svgContent);
    11. setSharedRenderer(new QSvgRenderer(_svgXML.toByteArray()));
    12. }
    13.  
    14. GraphicsColorSvgItem::~GraphicsColorSvgItem()
    15. {
    16. delete renderer();
    17. }
    18.  
    19. void GraphicsColorSvgItem::setColor(QColor c)
    20. {
    21. changeAttributes("fill", c.name().toUpper());
    22. changeAttributes("stroke", c.name().toUpper());
    23. renderer()->load(_svgXML.toByteArray());
    24. }
    25.  
    26. void GraphicsColorSvgItem::changeAttributes(QString attName, QString attValue)
    27. {
    28. QDomElement rootElem = _svgXML.documentElement();
    29.  
    30. QDomNode n = rootElem.firstChild();
    31. while(!n.isNull())
    32. {
    33. if(n.isElement())
    34. {
    35. QDomElement e = n.toElement();
    36. if(e.hasAttribute(attName))
    37. e.setAttribute(attName, attValue);
    38.  
    39. if(n.hasChildNodes())
    40. recursiveChangeAttributes(n.firstChild(), attName, attValue);
    41. }
    42. n = n.nextSibling();
    43. }
    44. }
    45.  
    46. void GraphicsColorSvgItem::recursiveChangeAttributes(QDomNode node, QString attName, QString attValue)
    47. {
    48. QDomNode n = node;
    49. while(!n.isNull())
    50. {
    51. if(n.isElement())
    52. {
    53. QDomElement e = n.toElement();
    54. if(e.hasAttribute(attName))
    55. e.setAttribute(attName, attValue);
    56.  
    57. if(n.hasChildNodes())
    58. recursiveChangeAttributes(n.firstChild(), attName, attValue);
    59. }
    60. n = n.nextSibling();
    61. }
    62. }
    To copy to clipboard, switch view to plain text mode 

    If you have some remarks, I'm open.

  6. #5
    Join Date
    Jun 2014
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to change color of a QGraphicsSvgItem ?

    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!

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to change color of a QGraphicsSvgItem ?

    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?

Similar Threads

  1. How can I change the filename of a QGraphicsSvgItem
    By Hogwarts in forum Qt Programming
    Replies: 1
    Last Post: 18th September 2012, 14:16
  2. Change in color map?
    By bigjoeystud in forum Qwt
    Replies: 4
    Last Post: 8th September 2010, 21:00
  3. Replies: 2
    Last Post: 29th March 2010, 13:34
  4. Replies: 3
    Last Post: 22nd January 2010, 17:46
  5. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 23:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.