我对为拖动事件创建的像素图有疑问。对于派生的 QGraphicsRectItem 的拖动事件,我从该项目创建了一个半透明的像素图。
在调试版本中一切看起来都很好。
但在发布版本中,拖动像素图有一些周期性和随机性的人工制品
代码如下:
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;
}
这会不会是一种内存问题?
最佳答案
QPixmap 使用未初始化的数据进行初始化。在 Debug 中,这通常设置为固定模式,但在 Release 中它是垃圾。
使用前应先用透明色填充像素图。
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.
(来自Qt Docs)
关于c++ - QT - 拖动时的像素图人工制品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45982065/