如何更改当前工作目录?

时间:2021-04-28 07:09:36

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:

我正在开发一个程序,该程序从某个目录中获取文件并将其复制到Qt的工作目录以供我的应用程序读取。现在,我目前的路径是:

/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?

我的问题是,我将如何改变这条道路?

2 个解决方案

#1


4  

copies it to the working directory of Qt

将其复制到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.

在这种情况下,不确定“Qt”究竟是什么意思。如果它是安装库的位置,则应将该路径与文件名关联,然后进行处理,而不是将当前工作目录设置为公平。

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.

但是你为什么要改变工作目录呢?虽然您可能想用它来解决一个问题,但您可能会立即引入一整套其他问题。感觉就像XY问题。我认为你在实践中需要一个不同的解决方案,例如前面提到的。

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)

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.

将应用程序的当前工作目录设置为path。如果目录已成功更改,则返回true;否则返回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/*/change-cwd"
"/usr/lib"

#2


2  

QDir has a function, setCurrent, for that purpose.

为此,QDir有一个函数setCurrent。

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

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

更多内容见http://doc.qt.io/qt-4.8/qdir.html#setCurrent。

#1


4  

copies it to the working directory of Qt

将其复制到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.

在这种情况下,不确定“Qt”究竟是什么意思。如果它是安装库的位置,则应将该路径与文件名关联,然后进行处理,而不是将当前工作目录设置为公平。

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.

但是你为什么要改变工作目录呢?虽然您可能想用它来解决一个问题,但您可能会立即引入一整套其他问题。感觉就像XY问题。我认为你在实践中需要一个不同的解决方案,例如前面提到的。

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)

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.

将应用程序的当前工作目录设置为path。如果目录已成功更改,则返回true;否则返回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/*/change-cwd"
"/usr/lib"

#2


2  

QDir has a function, setCurrent, for that purpose.

为此,QDir有一个函数setCurrent。

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

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

更多内容见http://doc.qt.io/qt-4.8/qdir.html#setCurrent。