PDA

View Full Version : Trouble with relative paths for files



N3wb
29th December 2010, 23:22
I have a lot of things like QIcons that I need to specify a path->filename for, but I don't want to do an absolute path because the project's directory will not always be constant.

I discovered the QCoreApplication::applicationDirPath() function, which gives the path to the executable file, but my images are a directory above. How can I move up a level? I tried stuff like:


QString temp = QCoreApplication::applicationDirPath() + "../icon.png";
setWindowIcon( QIcon(temp) );

But I haven't had any luck so far.

Is there an easy way to use relative paths for files?

squidge
29th December 2010, 23:30
See QDir and QDir::cdUp

Basically, you can give QCoreApplication::applicationDirPath() to the constructor of QDir, then perform cdUp function on that object and retrieve new directory. If it's not possible to go further up (eg. if the application is in the root directory), the cdUp method will fail.

totem
30th December 2010, 11:46
Also, you could output the result of your string concatenation (or go in debug mode) to be sure of what you're doing.
My guess is that you missed a "/" after application dir path



QString temp = QCoreApplication::applicationDirPath() + "/" + ".." + "/icon.png";
qDebug() << temp ;