tags:

views:

52

answers:

3

Ok well I'm trying implement something similar to the 'undo' function in many image drawing programs .. The problem I'm having is this: I'm trying to make a backup copy of a QImage object in a QVector (which stores upto 10 latest QImage copies for backup purposes), and then try to retrieve these backups in another function. The issue is that the backup is not being created properly (since when I try to recover a backuped image, nothing happens). I know the problem is somewhere in backing up part and not in the recovering part, since when I backup a new temporary image with a red background, it recovers perfectly ..

This is the backing up function code:

imageBackups.append(image);

where 'image' is the QImage object that I'm trying to backup ..

This is an alternate backing up (stores a red colored background image) - I used this just to see if this version of backing up works, which it does:

QImage newImage(QSize(100,100), QImage::Format_RGB32);
newImage.fill(qRgb(255, 0, 0));
imageBackups.append(newImage);

And here is the recovering code:

image =imageBackups.at(imageBackups.size()-1);
QPainter painter(&image);
painter.drawImage(QPoint(0,0),imageBackups.at(imageBackups.size()-1));

'image' is defined exactly like newImage above, except the size which is 800x400 in this case..

+1  A: 

The newImage is begin destructored when it goes out of scope, I'm guessing that the append is doing some sort of shallow copy where it only stores a reference to the image. QT does automatic reference counting with some objects, but I can't remember the details

Try creating the QImage with new and storing the pointer in imageBackups ( a better approach anyway).

Something like this ( note imageBackups now needs to be an array of QIMage* )

QImage pImage = new QImage(QSize(100,100), QImage::Format_RGB32);
pImage->fill(qRgb(255, 0, 0));
imageBackups.append(pImage);
Martin Beckett
For which code above are you saying I should use new ? When you write QImage newImage ( ... ); , aren't you creating a new QImage object ? Also, in the appending procedure, I don't see any place where new can be mentioned ..
Ahmad
Well you were right about the shallow copy thing .. If I changed the second backuping code to this:QImage newImage(QSize(100,100), QImage::Format_RGB32);newImage.fill(qRgb(255, 0, 0));imageBackups.append(newImage);newImage.~QImage();The recovery did NOT work ... However I got round this by using this:QImage newImage(QSize(100,100), QImage::Format_RGB32);newImage.fill(qRgb(255, 0, 0));imageBackups.append(newImage.copy(QRect()));newImage.~QImage();But the original problem still remains ...
Ahmad
A: 

It may has something to do with how you created image. If you use one of the constructors that takes a uchar * buffer (const or not), you have to make sure the buffer is valid through out the life of the QImage and its copies:

http://doc.trolltech.com/latest/qimage.html#QImage-5

If at the time of your restoring of the image from QVector, the buffer is deleted, the restored QImage will be using some stale memory location as it's buffer.

Stephen Chu
A: 

I think you might have something wrong with your recovery code. Perhaps it was just a typo when transferring from your actual code to the question, but you should look into it. Here is how I parse the recovery code.

// Get the image that is back one.
image =imageBackups.at(imageBackups.size()-1);
// Create a painter onto the backup image
QPainter painter(&image);
// Paint the backup image in the painter (onto the backup image).
painter.drawImage(QPoint(0,0),imageBackups.at(imageBackups.size()-1));

I can't see how this accomplishes anything, which might explain why nothing seems to happen when you are recovering.

Caleb Huitt - cjhuitt