简体   繁体   中英

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.

在此处输入图片说明

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

在此处输入图片说明

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?

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM