Drawing directly with QPainter and drawing QPixmap give different results



  • There's a simple class based on QQuickPaintedItem:

    class PaintedItem : public QQuickPaintedItem
    {
        Q_OBJECT
    public:
        PaintedItem();
        void paint(QPainter *painter) override;
    };
    
    // ...
    
    PaintedItem::PaintedItem()
    {
        setRenderTarget(QQuickPaintedItem::FramebufferObject);
    }
    
    void PaintedItem::paint(QPainter *painter)
    {
        painter->drawRect(0, 0, 150, 150);
    
        QPixmap* m_pixmap = new QPixmap(width(), height());
        m_pixmap->fill(QColor("transparent"));
    
        QPainter painter2(m_pixmap);
        painter2.setPen(QColor("red"));
        painter2.drawRect(0, 0, 150, 150);
    
        painter->drawPixmap(0, 0, *m_pixmap);
    }
    

    The paint() function just does two things: draw a rectangle directly with QPainter and draw the QPixmap containing the same rectangle. But if I set the render target as FramebufferObject in the constructor, those rectangles doesn't match for some reason. If I comment this string, everything's OK.

    With FramebufferObject
    alt text
    Without FramebufferObject
    alt text

    Could you please explain me why does it happen and how to deal with it?



  • @mdma2
    Can you post the full code of this short example, so that it can be tried out immediately using simple copy/paste...