0

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

With FramebufferObject

Without FramebufferObject

Without FramebufferObject

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

Nikita
  • 337
  • 1
  • 3
  • 12
  • Which of the two rectangles is different? The first one that's drawn directly with `painter->drawRect(0, 0, 150, 150)` or the one drawn via the pixmap? Also, you've got a memory leak as you allocate a new `QPixmap` every time `PaintedItem::paint` is called but never delete it -- just create it on the stack. – G.M. Aug 21 '18 at 13:50
  • @G.M. Yup, I know about memory leak, it's just a simplifed class for testing. I mean that pixmap's rectangle and directly-drawn rectangle have to be fully intersected by each other supposedly, but for some reason rectangles don't match and there's kind of a 1 px space between them. – Nikita Aug 21 '18 at 13:58
  • Sure, but which of the two rectangles drawn is *causing* the mismatch? If you comment out the line `painter->drawRect(0, 0, 150, 150)` do you get the desired result? Likewise if you comment out the line `painter->drawPixmap(0, 0, *m_pixmap)`? That is, which of the two rectangles appears to be offset? – G.M. Aug 21 '18 at 14:06
  • @G.M. I'm not sure which rectangle is right, because the `PaintedItem` and the `QPixmap` have the same size and have to paint the same rectangle, but an offset appears anyway. And the most strange thing is that without render target rectangles don't have any difference. – Nikita Aug 21 '18 at 15:28
  • @G.M. I guess the pixmap's rectangle is bigger than it has to be (red rectangle) – Nikita Aug 21 '18 at 15:37

1 Answers1

0

Most likely, drawing on the QQuickPaintedItem is not clipped, and you're drawing a bigger rectangle than you think you are. For historical reasons, the rectangles are one pixel wider than the size you entered. So your rectangle is 151x151 and doesn't fit into the QPixmap.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Well, then why rectangles have the same size when I don't have `FramebufferObject` flag set? And, as far as I know, QRect has a correct width, it has only `bottom()` and `right()` function deviated. – Nikita Aug 21 '18 at 15:30