10

I'm writing a program in C++ using the Qt library. I would like to get current working directory of my program. I found the QDir::currentPath() and QCoreApplication::applicationDirPath() function but they give back the directory where the application executable is. I don't wanna get the directory that contains the application executable.

I have uploaded the image which describes the path that I want to get.

enter image description here

Does anyone has any solutions?

Thank you!

Tan Viet
  • 1,983
  • 6
  • 25
  • 36
  • 2
    don't see an executable in the path/folder. – user1810087 Jul 15 '13 at 04:19
  • 1
    what do you mean by I want the executable but not the binary ? – A. H. Jul 15 '13 at 04:21
  • `QDir::currentPath()` *is* the current application directory. It is *not* the directory containing the executable, unless those two directories happen to be the same. Which is what happens in your case because Creator launches the executable that way. – Nikos C. Jul 15 '13 at 04:31

3 Answers3

13

Your application cannot know where the project is, because this is a qt-creator option. If you want to run your binary in the project path you have to setup your working directory inside qt-creator to the correct path. Then QDir::currentPath() will return the path you want to have.

goto: projects->[environment]->run->working directory

user1810087
  • 5,146
  • 1
  • 41
  • 76
2

Checkout QCoreApplication::applicationDirPath(), it tries to figure out the directory the executable is in, rather than checking the current working directory

ted
  • 4,791
  • 5
  • 38
  • 84
1

The source code directory is available via qmake variables and can "defined" for access within C++ source files:

# Demo.pro:
# set PROJECT_PATH to the "project path" with a trailing slash
DEFINES += PROJECT_PATH=\"\\\"$${_PRO_FILE_PWD_}/\\\"\"

// mainwindow.cpp
qDebug() << "Able to find main.cpp?..." 
         << QFile::exists(PROJECT_PATH "main.cpp"));

Output:

Able to find main.cpp?... true
pixelgrease
  • 1,940
  • 23
  • 26