user1914692
user1914692

Reputation: 3073

QT: fail to find the file using a relative path

(1) Say I have a file callingfile.cpp

It needs to read another file newyearfile.frag in its child directory "shaders".

This means it is like:

project:
  /prog1
  /prog1/callingfile.cpp
  /prog1/shaders/newyearfile.frag    (this file is added in Project.qrc)

Now in callingfile.cpp, I want to access newyearfile.frag

I tried:

shaders/newyearfile.frag

It doesn't work.

Then I tried:

./shaders/newyearfile.frag

It doesn't work

If I use

:/prog1/shaders/newyearfile.frag

Then it works.

But how come the relative paths cannot work?

(2) But there is another case in the project file, it can use upper level of relative path.

project:
  /prog2
  /prog2/callme.h   (this file is in the Project.pro)
  /prog2/source/iwillcall.cpp

In iwillcall.cpp it uses:

../callme.h

and it works.

(3) My guess: Do you think the case (1), the file in .qrc override relative path?

Upvotes: 0

Views: 1217

Answers (1)

Luca Carlon
Luca Carlon

Reputation: 9996

Relative paths are relative to the working directory (which is not necessarily the directory of your executable). You can also load it from the Qt resources if you prefer, but I see you probably already did.

With QDir::currentPath you can get the path of the working directory.

Upvotes: 2

Related Questions