RobRobRob
RobRobRob

Reputation: 67

QT - pixmap artefacts on drag

I have an issue with pixmaps created for drag events. For drag events of my derived QGraphicsRectItem I create a semi-transparent pixmap from that item.

In the debug build everything looks fine.

enter image description here

But in the release build the drag pixmap has some periodic and random artefacts

enter image description here

here is the code:

QPixmap MyGraphicsRectItem::toPixmap() const
{
   QRect r = boundingRect().toRect();
   QPixmap pixmap(r.width(), r.height());
   QColor dragColor(color);
   dragColor.setAlphaF(0.5);

   QPainter painter;
   painter.begin(&pixmap);

   painter.fillRect(pixmap.rect(), dragColor);
   painter.setPen(Qt::white);

   QFont font("SegoeUI");
   font.setBold(true);

   painter.setFont(font);
   painter.drawText(pixmap.rect(), QString(" ") + textItem->toPlainText());

   if (pixItem != nullptr) {
       painter.setOpacity(0.5);
       painter.drawPixmap(pixItem->pos(), pixItem->pixmap());
   }

   painter.end();

   return pixmap;
}

Could that be a kind of memory issue?

Upvotes: 0

Views: 281

Answers (1)

king_nak
king_nak

Reputation: 11513

The QPixmap is initialized with uninitialized data. In Debug, this is often set to a fixed pattern, but in Release it is garbage.

You should fill the pixmap with transparent color before using it.

QPixmap::QPixmap(int width, int height)

Constructs a pixmap with the given width and height. If either width or height is zero, a null pixmap is constructed.

Warning: This will create a QPixmap with uninitialized data. Call fill() to fill the pixmap with an appropriate color before drawing onto it with QPainter.

(From Qt Docs)

Upvotes: 1

Related Questions