Problems using QPixmap's loadFromData to load an image
-
wrote on 9 Mar 2012, 17:28 last edited by
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.
-
wrote on 9 Mar 2012, 17:39 last edited by
You may use "QDir::absoluteFilePath":http://qt-project.org/doc/qt-4.8/qdir.html#absoluteFilePath to check where the file should be.
-
wrote on 9 Mar 2012, 17:40 last edited by
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 -
wrote on 9 Mar 2012, 20:08 last edited by
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);
@ -
wrote on 9 Mar 2012, 21:01 last edited by
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();
@ -
wrote on 9 Mar 2012, 22:55 last edited by
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?
-
wrote on 10 Mar 2012, 12:08 last edited by
Hello
It sounds silly, but try to use "./folder/pic.png" or "/folder/pic.png" instead of "folder/pic.png". -
wrote on 10 Mar 2012, 20:36 last edited by
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.
-
wrote on 10 Mar 2012, 21:43 last edited by
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
9/9