Important: Please read the Qt Code of Conduct -

Looking for help with optimalization of QGraphicsItem subclass

  • Hi guys. I have this QGraphicsSvgItem subclass ("header":, "source": It was working quite well up until I tried to add some QGraphicsEffects (QGraphicsDropShadowEffect to be precise) to objects of that class... what I noticed (1 minute after the application even loaded) is that it was not usable at all... resizing the window was out of question, and so was anything else, so I dumped the idea, because I thought the reason for that was the QGraphicsSvgItem itself. But I was not sure so I created a testcase where I tried to find the culprit... what I found out was that the MySvgItem::size() is the method causing the problems (probably because boundingRect() is calculated out of size()), because when I did not set the size, it worked quite fine in my testcase with 625 items, but when I set the size to any value, the lagfest was back. The problem is that I use the method quite extensively and as I'm writing this I still have no solution to make the size() work faster.

    Here is the code I suspect from causing the overhead (the whole source code is linked above)

    @void MySvgItem::setSize(const QSizeF& size)
    if (size_m != size) {
    size_m = size;

    QSizeF MySvgItem::size() const
    qreal width = size_m.width();
    qreal height = size_m.height();

    if (size_m.width() < 0) {
        width = (renderer()->boundsOnElement(elementId()).size().width());
    if (size_m.height() < 0) {
        height = (renderer()->boundsOnElement(elementId()).size().width());
    return QSizeF(width, height);


    QRectF MySvgItem::boundingRect()
    return QRectF(QPointF(0.0, 0.0), size());

    "Here": is the compile-ready testcase (with svg and everything) I was using to test the stuff (the setSize() lines are now commented out). I would be glad for any help or other input. Thanks :)

  • So I rewrote some parts of the project so that it no longer relies on the setSize(..) (which I found to be useful not only because of the added performance but also because it's gives more free space to the designer of the app (more things can be set directly in the svg file)).

    Yet, there is one remaining problem and that is that when I use QSvgRenderer::render(QPainter* painter, const QString& elementId, const QRectF& bounds = QRectF()) and the bounds rectangle is bigger than QSvgRenderer::boundsOnElement(const QString& elementId) then it's not rendered correctly (only part is rendered).

Log in to reply