March 9, 2012

Psycho_Path Psycho_Path
Lab Rat
19 posts

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:

  1. QString qs = "folder/pic.png"; // I've also tried dual backslash instead of the forward slash
  2. 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.

8 replies

March 9, 2012

koahnig koahnig
Mad Scientist
2114 posts

You may use QDir::absoluteFilePath [qt-project.org] to check where the file should be.

March 9, 2012

Wilk Wilk
Ant Farmer
120 posts

Hello
Maybe I miss something, but loadFromData method is used, when you have your picture loaded into memory. Try to use QPixmap::load [qt-project.org] method

March 9, 2012

Psycho_Path Psycho_Path
Lab Rat
19 posts

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

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

March 9, 2012

Volker Volker
Robot Herder
5428 posts

QPixamp has a constructor taking a QString with the pathname:

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

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

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

March 9, 2012

Psycho_Path Psycho_Path
Lab Rat
19 posts

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?

March 10, 2012

Wilk Wilk
Ant Farmer
120 posts

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

March 10, 2012

Volker Volker
Robot Herder
5428 posts

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:

  1. #include <QImageReader>
  2.  
  3. qDebug() << QImageReader::supportedImageFormats();

This should list PNG, amongst others.

March 10, 2012

Psycho_Path Psycho_Path
Lab Rat
19 posts

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

 
  ‹‹ Changing the name of the control and updating the code      Grainy application icon under Windows/VS2005 ››

You must log in to post a reply. Not a member yet? Register here!