Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] Render QGraphicsScene to Pixmap - bits and pieces missing



  • I am using QGraphicsScene's render method to render the contents of a scene to a pixmap. Mostly it works, but for some of my objects, the resulting output has bits and pieces missing.

    I can step through my paint code, and everything is painted as it should be. It just never appears within the pixmap.

    When painting the same objects in a normal QGraphicsScene attached to a QGraphicsView, the same paint code runs and results in correctly painted objects.

    Here is the core code that created the pixmap:

    @
    QRectF targetRect = QRectF(QPointF(), QSizeF(getTargetSizeFromCurrentSizeMode()));

    QPixmap scenePixmap(targetRect.size().toSize());
    scenePixmap.fill(getBackgroundFillColor());
    {
        QPainter painter(&scenePixmap);
        m_pSourceGraphicsScene->render(&painter);
    }
    
    return scenePixmap;@
    

    Because I use this method to create a preview tooltip image, I create a QGraphicsScene on the stack, fill in all the QGraphicsItems I need, and render the scene, then throw it away. The items are all there and have correct data set.

    The interesting part is that it's not like whole items are missing - in many cases it looks as if items are only partially painted. E.g. I have a rectangle, which is simply painted using

    @ painter.drawRect(boundingRect());@

    Manual inspection reveals that the boundingRect at this time is 358x162 pixels in size, but is drawn as if it was about 50x50 pixels.
    Another item uses the very same class and very same code to paint a rectangle of 800x400 pixels - and actually get the expected result.

    Do you have any suggestions what I might need to watch out for, what I could try or check? Thanks!

    Update:
    Assigning the scene to a GraphicsView, and using grabWidget to get the pixmap yields the same result.
    Also, further inspection reveals that the QPainter's clipRect is not to blame for the rectangle (mentioned above) being drawn too small.
    I keep wondering...



  • Solution: It seems that Items do not get drawn if they have a cachingMode set (DeviceCoordinateCaching, in my case). This looks like a bug to me, but will require further investigation


Log in to reply