Search code examples
qtsvgqt5qgraphicssceneqtsvg

How to make QSvgGenerator produce vectorized SVG instead of bitmapped SVG?


I'm trying to export my QGraphicsScene to an SVG like this:

void MyScene::toSvg(QString filename)
{
    QSvgGenerator svgGen {};

    svgGen.setFileName(filename);
    svgGen.setSize({ 200, 200 });
    svgGen.setViewBox(QRect(0, 0, 200, 200));

    QPainter painter {};
    painter.begin(&svgGen);
    render(&painter);
    painter.end();
}

MyScene is inherited from QGraphicsScene.

In my scene I have objects that are inherited from QGraphicsItem.

My item renders like this:

void Node::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
    Q_UNUSED(widget)
    Q_UNUSED(option)

    painter->save();

    QPainterPath path;
    const QRectF rect(-m_size.width() / 2, -m_size.height() / 2, m_size.width(), m_size.height());
    path.addRoundedRect(rect, m_cornerRadius, m_cornerRadius);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->fillPath(path, QBrush(m_color));

    painter->restore();
}

Now the problem is that I'm only getting a bitmapped SVG so it's basically useless. Something like this:

<image x="38" y="59" width="41" height="15" preserveAspectRatio="none" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACkAAAAPCAYAAAB5lebdAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABGUlEQVRIie2TwWqEMBCG/2TXgCwe40lY8OhL6Ev4PEuex5fQl/AoCHsyeFIQdZ301FLFtVRLaWG/45BkvsnMAC9e/C3YN+N7MUcuL2VYFEWnqqr49XplAFCWpXFdl44kAYAwDEkpZbBDeCYZx/FJa22N42gRkQUAnPPxcrkckmyahjzPm7TW9El2jdX4TDKKonPf9xYRWZzzMwAQ0cO2bRJC7G5ZXdfGcRyybdtsdebZb2+2u+s6JqWkoigOzRQA+L7PtNa873vmOA5fFiGEMJ7nTUEQPJRSsyLOi7dMmqYTgCnP86NeH9xuN5ZlGZdS4n6/n9bODMPA2rZdXdif3uIt2LvsswNSSpMkCeGLdv8Wz/IeHqsX/543cmeARGeRY78AAAAASUVORK5CYII=" />

Should I do something special in my items in order to actually get SVG instead of bitmaps?

EDIT: This seems to be related, but disabling cache didn't help in my case with plain QGraphicsItems: QSvgGenerator converts QSvgGraphicsItem to image when generating Svg


Solution

  • It seems that setting QGraphicsDropShadowEffect for the items forces them as bitmaps (together with the effect graphics). The SVG will be vectorized if I don't set this effect.