[Interest] Force QPainter color draw for SVG files?

mat //common mail mat.common at gmail.com
Tue Apr 17 16:49:11 CEST 2012


Another solution (without resource files):

#ifndef SVGICONS_H
#define SVGICONS_H

#include <QObject>
#include <QPixmap>
#include <QIcon>
#include <QtSvg>

class SvgIcons
{
private:
  static QString GeneralSvgPart();
  static QPixmap GeneratePixmap(int width, int height, QString
*lastSvgPart);

public:
  //FileToolBar
  static QIcon Icon_Close(int width, int height, QColor color);
  static QIcon Icon_PrintPreview(int width, int height, QColor color);
  static QIcon Icon_Print(int width, int height, QColor color);
  ...
  //HelpToolBar
  static QIcon Icon_Help(int width, int height, QColor color);
  static QIcon Icon_Info(int width, int height, QColor color);
  ...
};

#endif // SVGICONS_H

-----------------------------------------------------

#include "svgicons.h"

QString SvgIcons::GeneralSvgPart()
{
  return QString(
  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" \
  "<!-- Created with Inkscape (http://www.inkscape.org/) -->" \
  "<svg" \
  "   xmlns:dc=\"http://purl.org/dc/elements/1.1/\"" \
  "   xmlns:cc=\"http://creativecommons.org/ns#\"" \
  "   xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" \
  "   xmlns:svg=\"http://www.w3.org/2000/svg\"" \
  "   xmlns=\"http://www.w3.org/2000/svg\"" \
  "   xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\""
\
  "   xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"" \
  "   width=\"24\"" \
  "   height=\"24\"" \
  "   id=\"svg2\"" \
  "   version=\"1.1\"" \
  "   inkscape:version=\"0.48.0 r9654\"" \
  "   sodipodi:docname=\"noname.svg\">" \
  "  <defs" \
  "     id=\"defs4\" />" \
  "  <sodipodi:namedview" \
  "     id=\"base\"" \
  "     pagecolor=\"#ffffff\"" \
  "     bordercolor=\"#646464\"" \
  "     borderopacity=\"1.0\"" \
  "     inkscape:pageopacity=\"0.0\"" \
  "     inkscape:pageshadow=\"2\"" \
  "     inkscape:zoom=\"16\"" \
  "     inkscape:cx=\"11.946628\"" \
  "     inkscape:cy=\"14.556665\"" \
  "     inkscape:document-units=\"px\"" \
  "     inkscape:current-layer=\"layer1\"" \
  "     showgrid=\"true\"" \
  "     inkscape:showpageshadow=\"false\"" \
  "     inkscape:window-width=\"1280\"" \
  "     inkscape:window-height=\"962\"" \
  "     inkscape:window-x=\"-8\"" \
  "     inkscape:window-y=\"-8\"" \
  "     inkscape:window-maximized=\"1\"" \
  "     inkscape:snap-nodes=\"false\">" \
  "    <inkscape:grid" \
  "       type=\"xygrid\"" \
  "       id=\"grid2999\" />" \
  "  </sodipodi:namedview>" \
  "  <metadata" \
  "     id=\"metadata7\">" \
  "    <rdf:RDF>" \
  "      <cc:Work" \
  "         rdf:about=\"\">" \
  "        <dc:format>image/svg+xml</dc:format>" \
  "        <dc:type" \
  "           rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />" \
  "        <dc:title />" \
  "      </cc:Work>" \
  "    </rdf:RDF>" \
  "  </metadata>"
  "  <g" \
  "     inkscape:label=\"Layer 1\"" \
  "     inkscape:groupmode=\"layer\"" \
  "     id=\"layer1\">");
}

QPixmap SvgIcons::GeneratePixmap(int width, int height, QString
*lastSvgPart)
{
  QPixmap pixmap(width, height);
  pixmap.fill(QColor(0, 0, 0, 0));

  QPainter painter(&pixmap);

  QByteArray ba;
  ba.append(GeneralSvgPart() + *lastSvgPart);

  QSvgRenderer svgRenderer(ba);
  svgRenderer.render(&painter);

  return pixmap;
}


QIcon SvgIcons::Icon_Close(int width, int height, QColor color)
{
  QString lastSvgPart = QString(
  "  <path" \
  "
style=\"fill:none;stroke:%1;stroke-width:3;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none\""
\
  "     d=\"m 1.5,1.5 21,21 m 0,-21 -21,21\"" \
  "     id=\"path2984\"" \
  "     inkscape:connector-curvature=\"0\"" \
  "     sodipodi:nodetypes=\"cccc\" />" \
  "  </g>" \
  "</svg>").arg(color.name());

  return QIcon( GeneratePixmap(width, height, &lastSvgPart) );
}

-----------------------------------------------------

int btnsIconSize = 24; // 24x24
QColor btnsIconColor = QColor(102, 102, 102);

btnFile_Close = new QToolButton(panelButtons);
btnFile_Close->setIconSize(QSize(btnsIconSize, btnsIconSize));
//btnFile_Close->setIcon( QIcon(":/resources/images/close.svg") );
*btnFile_Close->setIcon( SvgIcons::Icon_Close(btnsIconSize, btnsIconSize,
btnsIconColor) );*


http://qt-project.org/forums/viewthread/12205/


2012/4/15 jaume dominguez faus <jaume at land.aau.dk>

> Hi list.
>
> I guess the answer is "No". But maybe.... is it possible to force
> QPainter to paint only in, say "red", when I'm rendering a SVG that is
> black?
> Imagine that you have a set of symbols in SVG files similar to a font
> characters, and you want to paint them in different colors when they are
> selected. Is it possible to do it without making the "selected version"
> of each SVG file?
>
> Thanks.
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120417/9fcca53d/attachment.html>


More information about the Interest mailing list