PDA

View Full Version : getting absolute file path of Qt resources



dvmorris
17th April 2007, 16:43
I have some files in a .qrc file like this:



<file>objs/cube.obj</file>


and I want to resolve the absolute path to that file into a char * array.

I tried all sorts of combinations of QDir,QFile, and QFileInfo with no luck, does anyone have any ideas?



//filename = ":/objs/cube.obj" here
QFile file(fileName);
file.open(QIODevice::ReadOnly);
QFileInfo info(file);
QByteArray ba = info.absoluteFilePath().toLatin1();//file.fileName().toLatin1();//
const char *filename = ba.data();
QMessageBox::about(this, tr("test"),tr("filename = %1").arg(filename) );
//this outputs "filename = :/objs/cube.obj"

marcel
17th April 2007, 17:00
At run time, the resources specified in the qrc are embedded in the binary file...
When you distribute your app( binary ), you will not also distribute the resources.

marcel
17th April 2007, 17:04
Also, if you want to a set of resources dynamically, check QResource.

Also:


The resource files listed in the .qrc file are files that are part of the application's source tree. Paths are relative to the directory where the .qrc file is located. Resource data can either be compiled into the binary and thus accessed immediately in application code, or a binary resource can be created and at a later point in application code registered with the resource system.


regards

dvmorris
17th April 2007, 23:40
so I guess i misunderstood how this works. Just to clarify some more, I basically have some .obj files I want to embed in the app when i compile it, so i put it in the qrc file that has all my icons in it as well. The icons load up fine when i send ":/icon.png" to a QIcon or QPixmap constructor, but if I have a string called ":/cube.obj" and i load it like this:



//filename = ":/cube.obj" at this point
ifstream file;
file.open(filename);


that won't work, so I am guessing that I need to change all the places I use ifstream over to QFile's. Would that make it work?

wysota
18th April 2007, 00:32
Would that make it work?

Yes, it would.

dvmorris
18th April 2007, 02:52
QFile file(":/objs/cube.obj");
file.open(QIODevice::ReadOnly);
QMessageBox::about(this, tr("asdf"),tr("%1").arg(file.exists()));


file.exists() is outputting 0 in the above code. What could I be doing wrong? my .qrc file has this line in it:



<file alias="cube.obj">objs/cube.obj</file>

dvmorris
18th April 2007, 02:56
ok nevermind, when i try ":/cube.obj" , the alias, it returns 1.