Ich schreibe ein Programm in C++ mit der Qt-Bibliothek. In meinem Home-Bin-Verzeichnis befindet sich eine symbolische Verknüpfung zur ausführbaren Datei. Ich möchte, dass das aktuelle Arbeitsverzeichnis meines Programms das Verzeichnis ist, in dem ich mich mit meinem Terminal befinde (dh das Ergebnis des Befehls pwd
). Ich habe die Funktion QDir::currentPath()
gesehen, aber sie gibt das Verzeichnis zurück, in dem sich die Binärdatei befindet.
Wie finde ich mein aktuelles Arbeitsverzeichnis?
Vielen Dank, dass Sie RedX und Kaz für Ihre Antworten. Ich verstehe nicht, warum es mir den Weg der Exe gibt. Ich habe einen anderen Weg gefunden:
QString pwd("");
char * PWD;
PWD = getenv ("PWD");
pwd.append(PWD);
cout << "Working directory : " << pwd << flush;
Es ist weniger elegant als eine einzelne Linie ... aber es funktioniert bei mir.
Gerade getestet und QDir::currentPath()
gibt den Pfad zurück, von dem aus ich meine ausführbare Datei aufgerufen habe.
Und ein Symlink existiert nicht. Wenn Sie eine Exe von diesem Pfad ausführen, führen Sie sie effektiv von dem Pfad aus, auf den der Symlink verweist.
Haben Sie versucht QCoreApplication :: applicationDirPath ()
qDebug() << "App path : " << qApp->applicationDirPath();
Wenn ich eine QML-Anwendung erstelle, tendiere ich dazu, dies zum Haupt-C++ hinzuzufügen
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QStandardPaths>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
// get the applications dir path and expose it to QML
QUrl appPath(QString("%1").arg(app.applicationDirPath()));
engine.rootContext()->setContextProperty("appPath", appPath);
// Get the QStandardPaths home location and expose it to QML
QUrl userPath;
const QStringList usersLocation = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
if (usersLocation.isEmpty())
userPath = appPath.resolved(QUrl("/home/"));
else
userPath = QString("%1").arg(usersLocation.first());
engine.rootContext()->setContextProperty("userPath", userPath);
QUrl imagePath;
const QStringList picturesLocation = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
if (picturesLocation.isEmpty())
imagePath = appPath.resolved(QUrl("images"));
else
imagePath = QString("%1").arg(picturesLocation.first());
engine.rootContext()->setContextProperty("imagePath", imagePath);
QUrl videoPath;
const QStringList moviesLocation = QStandardPaths::standardLocations(QStandardPaths::MoviesLocation);
if (moviesLocation.isEmpty())
videoPath = appPath.resolved(QUrl("./"));
else
videoPath = QString("%1").arg(moviesLocation.first());
engine.rootContext()->setContextProperty("videoPath", videoPath);
QUrl homePath;
const QStringList homesLocation = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
if (homesLocation.isEmpty())
homePath = appPath.resolved(QUrl("/"));
else
homePath = QString("%1").arg(homesLocation.first());
engine.rootContext()->setContextProperty("homePath", homePath);
QUrl desktopPath;
const QStringList desktopsLocation = QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
if (desktopsLocation.isEmpty())
desktopPath = appPath.resolved(QUrl("/"));
else
desktopPath = QString("%1").arg(desktopsLocation.first());
engine.rootContext()->setContextProperty("desktopPath", desktopPath);
QUrl docPath;
const QStringList docsLocation = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
if (docsLocation.isEmpty())
docPath = appPath.resolved(QUrl("/"));
else
docPath = QString("%1").arg(docsLocation.first());
engine.rootContext()->setContextProperty("docPath", docPath);
QUrl tempPath;
const QStringList tempsLocation = QStandardPaths::standardLocations(QStandardPaths::TempLocation);
if (tempsLocation.isEmpty())
tempPath = appPath.resolved(QUrl("/"));
else
tempPath = QString("%1").arg(tempsLocation.first());
engine.rootContext()->setContextProperty("tempPath", tempPath);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
Verwendung in QML
....
........
............
Text{
text:"This is the applications path: " + appPath
+ "\nThis is the users home directory: " + homePath
+ "\nThis is the Desktop path: " desktopPath;
}
Ich führe Qt 5.5 unter Windows aus und der Standardkonstruktor von QDir scheint das aktuelle Arbeitsverzeichnis zu übernehmen, nicht das Anwendungsverzeichnis.
Ich bin nicht sicher, ob das getenv-PWD plattformübergreifend funktioniert, und ich denke, es wird auf das aktuelle Arbeitsverzeichnis gesetzt, als die Shell die Anwendung startete, und enthält keine von der App selbst vorgenommenen Änderungen des Arbeitsverzeichnisses (was möglicherweise der Grund ist) das OP sieht dieses Verhalten).
Daher habe ich mir überlegt, einige andere Möglichkeiten hinzuzufügen, mit denen Sie das aktuelle Arbeitsverzeichnis (nicht den Binärspeicherort der Anwendung) ermitteln können:
// using where a relative filename will end up
QFileInfo fi("temp");
cout << fi.absolutePath() << endl;
// explicitly using the relative name of the current working directory
QDir dir(".");
cout << dir.absolutePath() << endl;