QGraphicsItem paint keep old content

  • Hello,
    I do face a problem, well not really a problem, as this do behave correctly, with QGraphicsItem::paint() QPainter.

    Problem is that when QGraphicsItem::paint() method is invoked QPainter will clear item content.
    Work around for this is using QPixmap as "cache" but that do take memory, especially on HDPI embedded system.

    I tried various blending options, but every one of them do erase previous content.

    My question, is there a way to force QPainter in QGraphicsItem::paint() to not erase previous content, so when paint is issues with specified rectangle any call to i.e. QPainter::drawPath() will only add new content.

    What I want to achieve: drawing by moue (like in Paint) without need to use cache of any sort minimizing memory footprint and increasing drawing speed by using smallest possible clipping rect.

    Thanks for any suggestions.

  • Lifetime Qt Champion


    Can you share the code you are using for the painting ?

  • It's simply call to draw 2 last elements from QPainterPath, like:

    void Item::paint(QPainter *painter,
                        const QStyleOptionGraphicsItem *option,
                        QWidget *widget)
        const QPointF& p1 = path.elementAt( path.elementCount()-2);
        const QPointF& p2 = path.elementAt( path.elementCount()-1);
        painter->drawLine( p1, p2);

    painter path to increase in mouseMove by lineTo( event->pos());
    Omitted checks etc.

  • Lifetime Qt Champion

    The thing is, you are currently going against the recommendation of the paint function which state that your item should redraw itself the exact same way.

  • Not exactly, I do call update() after new line is added to QPainterPath.
    There is also similar issue with macOS and transparency, but in that case it's other way around old content is not cleared. I do think that this could be achieved and I don't see any problem with letting developer to clear theirs item's content manually. The problem is I don't want to go as deep as patching Qt (probably view or scene classes) due to deployment problems (I do need to deploy to 3-4 OS'es so it will be pain to maintain).

    AFAIKT there is no relation between item content (human visible content) and content that is mentioned in documentation and only important parts are boundingRect() and shape() with do determine "content" size from scene point of view, rest is irrelevant and if painting is done inside item no artifact should occur.

  • Lifetime Qt Champion

    Which version of Qt are you using by the way ?

  • Qt 5.7 msvc.

Log in to reply