0

I have problem with storing a path to file in Windows in a QString. I'm using Qt with C++.

QString resourcePath = ":/images/frog.bmp";
if( ! QFile::exists(resourcePath) )
{
    qDebug("*** Error - Resource path not found : %s",   resourcePath.data());
}

This code results with this:

*** Error - Resource path not found : :

So I can see that resourcePath.data()) contains just ":". I assumed that the problem is with slashes, so I tried changing "/" with "\" but the result is the same.

But if I write:

QString resourcePath = "C:\\Users\\Boris\\Desktop\\Frogger3\\images\\frog.bmp";

everything works just fine. What am I missing? Is there a reason why colon cant be the first sign in QString? How should I write path to the file in the same folder as the code?

Thanks in advance!

speedyTeh
  • 247
  • 1
  • 8
  • 22
  • 2
    With the `%s` format specifier, `qDebug` expects a null terminated ascii string but `QString::data()` returns a pointer to the UTF-16 representation of the string. You should use something like `qDebug("... %s", resourcePath.toLatin1().data());` instead. – alexisdm Jul 31 '13 at 00:34
  • If you set resourcePath to the full path, there's no chance it's altered between due to whatever reason, exists() takes a const reference. As alexisdm points out, it might be a problem with your qdebug() line. (I'd suggest to use `qPrintable(resourcePath)` though) – Frank Osterfeld Jul 31 '13 at 11:26

1 Answers1

2

The style of resource path you are using is implying that the file frog.bmp is in a resource file. So either you need to resolve the path of the bmp file at run-time, or you need to add a resource file to your project.

If you use the UI designer the concept of resource files is handled automatically, but if you want to access resources through code there are a few things you need to do.

First create a resource file. In visual studios (with the visual studios add-in) there is a wizard to do this. Essentially it is just an xml file with the extension .qrc looking something like this:

<RCC>
    <qresource prefix="/images">
        <file>frogger.bmp</file>
    </qresource>
</RCC>

Now this file has to be processed during the build. If you have used .ui files, it is similar. There is a tool called "rcc.exe" that takes the qrc file as an input and generates a .cpp file that needs to be compiled and linked with your project.

If you are using visual studios and have the Qt Visual Studios Plugin, this should all be handled for you when you add the qrc file to the project.

If you are using QMake then your pri file should contain a "RESOURCES" section where you need to list your qrc file something like:

RESOURCES += yourqrcfile.qrc

Now, once that is done. You can access your resources in code. Your call to QFile::exists should resolve the file name.

In the case where you put your resources in a static or shared library, you will need to add the following line to your class to ensure that the resource file is loaded.

Q_INIT_RESOURCE(yourqrcfile);  // do not include the extension, just the name of the file.

Here are a few links that explain things in more detail:

Creating a resource file in Qt Creator

Explaining how resource files work

Liz
  • 8,780
  • 2
  • 36
  • 40
  • Thank you very much! But unfortunately this didn't solve my problem... i still get *** Error - Resource path not found : : – speedyTeh Jul 31 '13 at 00:36
  • Two things... In case you reproduced my resource file exactly, in your case you should change line 2 to ". I changed my answer - sorry about that. Also, I saw it mentioned the other day that a resource path must have "qrc" before the ":/images/frogger.bmp", making it "qrc:/images/frogger.bmp". I have never had to do that to make it work, but you could try that as well. – Liz Aug 01 '13 at 02:02