Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Loading textures from Qt resource  (Read 4757 times)

0 Members and 1 Guest are viewing this topic.

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Loading textures from Qt resource
« on: March 21, 2015, 07:45:38 pm »
Hi... I am trying to load a texture from a .qrc file which contains my texture file. I tried to google for it but I got no relevant search result so I'm posting it here. Here's what I did.

QResource Image(":/test.bmp");

sf::Texture Tex;

Tex.loadFromMemory(Image.data(), Image.size(), sf::IntRect());

The texture is not being loaded and fails to show. I checked and found that loadFromMemory() takes const void* for data, and std::size_t for size and the QResource's data() returns a const unsigned char* as return value and size() returns long long int as return value. So I tried the following two methods.

Tex.loadFromMemory((const void*)Image.data(), (std::size_t)Image.size(), sf::IntRect());

Tex.loadFromMemory(static_cast<const void*>(Image.data()), static_cast<std::size_t>(Image.size()), sf::IntRect());

Still the same result. The image does not load. Here's the application output.
Starting C:\Users\WDR\Documents\Qt\test\debug\test.exe...
Failed to load image from memory, no data provided
C:\Users\WDR\Documents\Qt\test\debug\test.exe exited with code 0

Please tell me the right way to do this. Thank you.  :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading textures from Qt resource
« Reply #1 on: March 21, 2015, 08:02:54 pm »
Quote
Failed to load image from memory, no data provided
Have you read that? Is your image empty?

And don't specify the third argument, the default parameter exists for a reason ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Loading textures from Qt resource
« Reply #2 on: March 21, 2015, 08:43:23 pm »
The error means that Image.data() is null and/or Image.size() is zero. In other words, this problem has nothing to do with SFML, make the Qt part work first ;)
Laurent Gomila - SFML developer

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Loading textures from Qt resource
« Reply #3 on: March 21, 2015, 08:43:53 pm »
Quote
Failed to load image from memory, no data provided
Have you read that? Is your image empty?

And don't specify the third argument, the default parameter exists for a reason ;)

I don't understand what you mean by "Have you read that?" Are you asking, have I read that sentence or have I read the image into memory? The image file is perfect, not corrupted in any way. And that file does exist in the .qrc file. I don't know why it isn't loading.

P.S. : I initially did not have the third argument in. But I later put it in because I thought people might criticize me for not putting it in. I read the docs and saw it was default, but I was being on the safe side anyway.  ;D

EDIT: @Laurent I went according to the Qt Docs and did exactly what it mentioned to do for loading a file into QResource from the .qrc file. The file in the .qrc works fine, and is perfectly usable by the QWidget. Everything else also works fine. I don't know why this is occuring. I assumed it was the loadFromMemory(). Now, I know I didn't do anything wrong there. Thanks for clearing that up.
« Last Edit: March 21, 2015, 08:55:03 pm by WDR »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading textures from Qt resource
« Reply #4 on: March 21, 2015, 08:49:14 pm »
I don't understand what you mean by "Have you read that?"
I meant the error message, which says that there's something wrong with the passed data. You should store both the data pointer and the size into variables and check their values with a debugger.

But I later put it in because I thought people might criticize me for not putting it in. I read the docs and saw it was default, but I was being on the safe side anyway.  ;D
The whole point of default parameters is to relieve users from specifying them. You're not any safer at all :P
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Loading textures from Qt resource
« Reply #5 on: March 21, 2015, 08:58:08 pm »
I meant the error message, which says that there's something wrong with the passed data. You should store both the data pointer and the size into variables and check their values with a debugger.

OK, I'll try that... I'll see what comes up and update this post accordingly. Thanks.

EDIT: **FIXED** I ran it through the debugger, and it showed zeros. The QResource is returning empty. I tested on another project and it worked. Apparently, this project was corrupted. Now it works perfectly. Thank you guys for the help. :)
« Last Edit: March 21, 2015, 10:03:13 pm by WDR »