CacheKey method of QImage returns not really a plain hash of image. Does anyone know, what the upper 32 bits mean? And can I really ignore them when comparing two images? (Compare only the lower 32 bits?)
For illustration only, this code reading the same image multiple times (yet storing them in different QImage objects):
printf("%llx\n",QImage("image.png").cacheKey());
printf("%llx\n",QImage("image.png").cacheKey());
printf("%llx\n",QImage("image.png").cacheKey());
returns this?
144300000002
144400000002
144500000002
or this? (seems like the upper 32 bits depend on current memory position)
140800000002
140900000002
140a00000002
No, you can't use any part of the
cacheKey
to compare image content, it only exists to make sure an image hasn't changed since the lastcacheKey
value was taken. It identifies the buffer and changes whenever any non-const function of theQImage
is called.But as the name of the
cacheKey
property implies, you can use it as a key for aQCache<qint64, QByteArray>
where you would store the actual hash of the image that you would only recalculate withQCryptographicHash
only if the image was changed (= only if it isn't already in the cache).Also
QImage
doesn't use a cache likeQPixmap
for reading an image, so reading 3 times the same image file will allocate 3 distinct buffers, each time with a differentcacheKey
. To avoid calculating the hashes each time, you should take a look at the source code of theQPixmap::load
function.