Qt support for coloring SVG in a QIcon

2019-07-26 16:10发布

It appears that Qt doesn't support the stroke/fill options on the path tag within a SVG:

<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8" viewBox="0 0 8 8">
   <path fill="green" stroke="red" d="M2 0c1 2-2 3-2 5s2 3 2 3c-.98-1.98 2-3 2-5s-2-3-2-3zm3 3c1 2-2 3-2 5h3c.4 0 1-.5 1-2 0-2-2-3-2-3z" />
</svg>

I load this into my application via Qt's resource loader, using a QIcon:

QIcon icon(":/svgs/fire.svg");

It displays as black on white regardless of the SVG properties. When I open the SVG in Firefox, the colors are honored. (Note, I've also tried hex values for the color arguments).

Am I overlooking something? Is there another way to color SVGs/QIcons in Qt?

标签: c++ qt svg qt4
2条回答
ら.Afraid
2楼-- · 2019-07-26 16:27

The solution for me was to instead load the SVG as a QImage... However, I'm finding that the image is loaded in with some sort of default size (which is pretty small). when I call QImage::scaleToHeight, it's scaling it as if it were a raster image, not a vector (hence, there is pixelation, etc).

So now I'm trying to figure out how to specify the size when loading an SVG as a QImage. I'll report back if I find the solution...

查看更多
倾城 Initia
3楼-- · 2019-07-26 16:40

I am having a similar issue but from another part of Qt.. i am using a QAction to create a button on QToolBar. When i set the icon of the QAction to an SVG resource, i end up with a pixelated image.

I am running a retina display machine, tried both scaling up my svg co-ordinates, and changing my display dpi, but both do not work.

I came to the conclusion that Qt implicitly transforms the vector image to a raster image for QToolBar QAction icons. I don't know if this help you or not, but i though it's worth mentioning.

查看更多
登录 后发表回答