tags:

views:

46

answers:

2

I don't see this in the documentation anywhere, so it probably doesn't exist, but just in case:

I have a function that accepts as a parameter a vector of QImages. Each QImage was loaded from disk at some point and has not been edited--it is just read from. Ideally what I'd like to do is loop over all the QImages and output a list of their file paths to an XML file.

Unfortunately, I'm not seeing in the documentation any way to get at the original file path that the image was loaded from. So my question is, is it possible, given only a QImage, to figure out what file path the QImage was originally loaded from or not?

Something along the lines of:

QString QImage::getOriginalFilepath();

I know this is probably a futile question, but its always worth a shot to ask, I suppose.

(I'm using Qt 4.7, by the way.)

+2  A: 

I looked through the code, and that information doesn't appear to be saved anywhere. QImage::load (and the constructor) uses QImageReader(fileName,format).read() and then copies the result to itself. The QImageReader is set to delete the device representation (the opened file object) as soon as it's finished reading.

So, in summary, it appears that this is not possible.

jkerian
+3  A: 

Also for me it seems that QImage doesn't provide an interface to get its path. I also think that this is not a missing feature, because there's no need to tie a QImage to a specific path. In the majority of all use cases the QImage has no corresponding physical file.

So the best way in your situation would be to subclass QImage and add this feature:

class MyImage : public QImage {
   QString path_;
public:
   MyImage(const QString& path); 
   QString path(); // getter 
};

I omit the implementation details.

Wolfgang Plaschg
QImages are not QObjects, so the macro is wrong. I would also use a struct Foobar { QImage image; QString path; }; instead, or somesuch, instead of needless subclassing.
Frank
Thanks for the hint.
Wolfgang Plaschg
I think subclassing here is absolutely useful because @quadelirus wants to use all features of a QImage and not just a subset of it (in this case a has-a relationship would be better). I can see no benefit of using a struct, and it has the downside that you always have to write `foobar.image.xy()`.
Wolfgang Plaschg