Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Qt Image won't load?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 jdub   Members   -  Reputation: 302

Like
0Likes
Like

Posted 16 March 2013 - 06:23 PM

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.

Sponsor:

#2 Cornstalks   Moderator*   -  Reputation: 5410

Like
2Likes
Like

Posted 16 March 2013 - 06:34 PM

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.


[ 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 ]

#3 jdub   Members   -  Reputation: 302

Like
0Likes
Like

Posted 16 March 2013 - 07:22 PM

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


J.W.

#4 Cornstalks   Moderator*   -  Reputation: 5410

Like
0Likes
Like

Posted 16 March 2013 - 09:42 PM

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!");
}

Edited by Cornstalks, 16 March 2013 - 09:45 PM.

[ 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 ]

#5 BlueHabu   Members   -  Reputation: 106

Like
0Likes
Like

Posted 16 March 2013 - 10:53 PM

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.



#6 jdub   Members   -  Reputation: 302

Like
0Likes
Like

Posted 16 March 2013 - 11:52 PM

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


J.W.

#7 Aardvajk   Members   -  Reputation: 2408

Like
0Likes
Like

Posted 18 March 2013 - 07:45 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS