6

I am working on a program that takes a file from a certain directory and copies it to the working directory of Qt to be read by my application. Right now, my current path is:

/Users/softwareDev/Desktop/User1/build-viewer-Desktop_Qt_5_4_0_clang_64bit-Debug/viewer.app/Conents/MacOS/viewer

To get this, I used:

qDebug() << QDir::current().path();

and confirmed this directory with:

qDebug() << QCoreApplication::applicationDirPath();

My question is, how would I go about changing this path?

AAEM
  • 1,837
  • 2
  • 18
  • 26
abuv
  • 169
  • 1
  • 1
  • 13
  • 1
    [Here](http://stackoverflow.com/questions/27500588/difference-in-relative-file-path-debug-mode-and-release-mode-of-qt-creator). From an answer I've accepted, though I asked a different question. – Tay2510 Dec 24 '14 at 03:05
  • @Tay2510, your link is fantastic, thank you for the help, it worked when I needed to run the application on the Mac. However, is there a way to make this work when porting to an iOS device? – abuv Dec 24 '14 at 03:18
  • Hmm...I have no experience in developing mobile devices and that link is all I can think about when talking to changing working directory. Perhaps you can specify your needs for iOS in the title and the question? – Tay2510 Dec 24 '14 at 03:31

2 Answers2

9

copies it to the working directory of Qt

Not sure what exactly you mean by "Qt" in this context. If it is where the library is installed, you should associate that path with the file name then to be processed rather than setting the current working directory to be fair.

But why do you want to change the working directory at all? While you may want to solve one problem with it, you might instantly introduce a whole set of others. It feels like the XY problem. I think you will need a different solution in practice, like for instance the aforementioned.

If you still insist on changing the current working directory or whatever reason, you can use this static method:

bool QDir::​setCurrent(const QString & path)

Sets the application's current working directory to path. Returns true if the directory was successfully changed; otherwise returns false.

Therefore, you would be issuing something like this:

main.cpp

#include <QDir>
#include <QDebug>

int main()
{
    qDebug() << QDir::currentPath();
    if (!QDir::setCurrent(QStringLiteral("/usr/lib")))
        qDebug() << "Could not change the current working directory";
    qDebug() << QDir::currentPath();
    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

"/tmp/stackoverflow/change-cwd"
"/usr/lib"
Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • You are right, I fell into the XY problem. My issue was resolved with @Tay2510. What I needed to do was change the build directory specified in Projects->Run Settings->Working Directory. – abuv Dec 26 '14 at 21:42
  • An excellent answer, which is not just a link, but is a great explanation and still has a working example. Congratulations! (Many answers here on the site are just links or have just typed "try this") – Protomen Nov 24 '16 at 17:02
2

QDir has a function, setCurrent, for that purpose.

bool QDir::setCurrent ( const QString & path ) [static]

More at http://doc.qt.io/qt-4.8/qdir.html#setCurrent.

jesterjunk
  • 2,342
  • 22
  • 18
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • This is wrong documentation as the OP uses 5.4, not ancient 4.8. It is probably not an issue in this case, but it is a bad practice. – László Papp Dec 25 '14 at 07:44