QPixmap copy question
-
Let's say I have something like a QPixmap, which loads a large png file, which serves as a sprite sheet. Then I want to separate it to different QPixmaps and paint them in custom QGraphicsItem. Should I allocate the QPixmaps which contain the copy of the original QPixmap sprite sheet on the heap and delete them after painting, or I can do it on the stack? Example:
class CustomQGraphicsItem : public QGraphicsItem { CustomQGraphicsItem() { original = new QPixmap; original = original.load('image.png'); } QPixmap *original; paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) { QRect rect1(0, 0, 10, 20); QRect rect2(0, 20, 10, 20); QPixmap cropped1 = original.copy(rect1); QPixmap cropped2 = original.copy(rect2); painter.drawPixmap(0, 0, cropped1); painter.drawPixmap(0, 20, cropped2); } ~CustomQGraphicsItem() { delete original; } };
Done this way, do I have to delete anything concerning the deep copy, or it's sufficient for cropped1 and cropped2 to go out of the scope of the paint function, for all the memory to be freed, and just delete original, when the object is destroyed?
P.S. I'm writing this on a PC without IDE, so there might be some errors. I haven't written the boundingRect. -
@Hristo-Konstantinov
QPixmap
s are maintained by Qt as shared data. You do not need to keep your pixmap in scope after assigning/drawing it, so stack should be fine, with no need to delete.