Qt Image won't load?

Started by
5 comments, last by Aardvajk 11 years, 1 month ago

I am trying to load an image in qt with the following code:


this->image = new QImage("c://Users//Jared//Pictures//cs_162_final//led.jpg");
	if(image->isNull())
	{
		throw image;
	}

I am not sure what is wrong as the path seems to be correct and this is how I've seen images loaded in examples. Any ideas?

J.W.
Advertisement

The path is wrong. You don't what to use two forward slashes (//). You want to use one forward slash (/). Like this:


"c:/Users/Jared/Pictures/cs_162_final/led.jpg"

Sometimes, on Windows, people will use backslashes instead of forward slashes (since that's what the Windows OS uses), but forward slashes work just fine on Windows too (plus backslashes don't work on other operating systems). Backslashes have to be escaped, however, because backslashes form escape sequences. Thus, if you did use backslashes, then you would have two of them in a row and it would be like:


"c:\\Users\\Jared\\Pictures\\cs_162_final\\led.jpg"

But I'd still go for the single forward slash version.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

forward slashes don't work either. Double checked the path again and it seems to be right. Hmm this is very confusing

J.W.

forward slashes don't work either. Double checked the path again and it seems to be right. Hmm this is very confusing

I'd suggest (just for debugging purposes) opening the file first and then trying to create the image. That way you can know if it's opening the file that's failing, or if it's corrupt (or if Qt just can't handle it). Something like:
QString path = "c:/Users/Jared/Pictures/cs_162_final/led.jpg";
{
    std::ifstream file(path.toStdString(), std::ios_base::binary);
    if (!file)
    {
        throw std::runtime_error("Could not open the file... either a bad path or your OS is blocking you (permission settings, maybe?)");
    }
}

this->image = new QImage(path);
if(image->isNull())
{
    // Also, you're probably leaking memory here since this->image was allocated but isn't being freed
    // (unless you catch the exception somewhere and delete this->image)
    throw std::runtime_error("The file exists, but it couldn't be read and decoded into a proper QImage!");
}
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

I believe the jpeg implementation is in a dll. I compile Qt myself and I have to be careful to update the image plugins when I do so or images wont load.

Yes it is failing when I actually try and create the image itself. Where can I go to download this dll?

J.W.

You should already have them in the plugins/imageformats folder in your Qt installation.

There's a page here with some notes on getting it working:

http://stackoverflow.com/questions/6724606/include-the-jpeg-plugin-in-my-application

This topic is closed to new replies.

Advertisement