Search code examples
qtqt4qimageqpixmapqhash

Constantly generating QPixmap* fails after a number of iterations


I am using the following code in order to generate QPixmap* pointers and then insert them into QHash<QString, QPixmap*> (I will show only the pointers generation code since this is the one that fails).

QPixmap* MyClass::loadImg(QString fileName)
{
    QImage qimage(fileName);
    if (qimage.isNull()) {
       qDebug() << "Cannot load image " <<  fileName;
    }

    QPixmap *image = new QPixmap(fileName);
    return image;
}

The problem that I have is the following: For the first about 200 calls the method works fine - it is being called on a loop that iterates through the image files of a directory. Then suddenly the QPixmap* starts returning QPixmap(null) for no apparent reason.QImage is also null when that happens. I have checked and made sure that the path is fine. Also, I have tried with various sets of images and the same always happens - it runs with no problems the ~200 calls and then starts generating nulls.

Any help would be appreciated.

Thank you.


Solution

  • Just don't create it on heap. QPixmap is implicitly shared.