Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problems using QPixmap's loadFromData to load an image



  • Trying to use the loadFromData to load an image that's in a sub-folder where the code looks like this:

    @
    QPixmap qp;
    QString qs = "folder/pic.png"; // I've also tried dual backslash instead of the forward slash
    qp.loadFromData(qs.toAscii());
    @

    I have the folder both in the project's folder as well as in the debug folder where the debug .exe is (not sure where I'm supposed to have it) and this function always returns false.

    Any help would be much appreciated, thanks.



  • You may use "QDir::absoluteFilePath":http://qt-project.org/doc/qt-4.8/qdir.html#absoluteFilePath to check where the file should be.



  • Hello
    Maybe I miss something, but loadFromData method is used, when you have your picture loaded into memory. Try to use "QPixmap::load":http://qt-project.org/doc/qt-4.8/qpixmap.html#load method



  • Tried using the load function and that doesn't seem to be solving my problem either:

    @
    QPixmap qp;
    QString qs = "folder/pic.png"; // I've also tried dual backslash instead of the forward slash
    qp.load(qs);
    @



  • QPixamp has a constructor taking a QString with the pathname:

    @
    QPixampa qp("foler/pic.png");
    @

    For your path problem. Make a QFile of the path an check if that absolute path exists:

    @
    QFile qpFile("folder/pic.png");
    QFileInfo qpFileInfo(qpFile);
    qDebug() << qpFileInfo. absoluteFilePath();
    @



  • Yes, the file path exists. Tried to run the same code with using the file name in the constructor and it still doesn't work. Is there some flag I should be setting in the .load() call?



  • Hello
    It sounds silly, but try to use "./folder/pic.png" or "/folder/pic.png" instead of "folder/pic.png".



  • Did you try with an full, absolute path in the constructor? Just to make sure, it's not a "file not found" issue.

    If that doesn't work either, check if the imageformats support PNG. Add in your main method:

    @
    #include <QImageReader>

    qDebug() << QImageReader::supportedImageFormats();
    @

    This should list PNG, amongst others.



  • Figured out that my problem was the folder was in the wrong folder for the code to recognize it. In any case I'm now having the problem where the image wont draw via a qpainter.drawPixmap() function. Going to start a new thread


Log in to reply