web-dev-qa-db-fra.com

Obtenir le répertoire de travail actuel dans une application Qt

J'écris un programme en C++ en utilisant la bibliothèque Qt. Il y a un lien symbolique dans mon répertoire home bin vers l'exécutable. Je voudrais que le répertoire de travail actuel de mon programme soit le répertoire dans lequel je me trouve avec mon terminal (c'est-à-dire le résultat de la commande pwd.). J'ai vu la fonction QDir::currentPath(), mais elle renvoie le répertoire où se trouve le binaire.

Comment trouver mon répertoire de travail actuel?

67
Geoffroy

Merci RedX et Kaz pour vos réponses. Je ne comprends pas pourquoi par moi cela donne le chemin de l'exe. J'ai trouvé un autre moyen de le faire:

QString pwd("");
char * PWD;
PWD = getenv ("PWD");
pwd.append(PWD);
cout << "Working directory : " << pwd << flush;

C'est moins élégant qu'une seule ligne ... mais ça marche pour moi.

3
Geoffroy

Je viens de tester et QDir::currentPath() renvoie le chemin à partir duquel j'ai appelé mon exécutable.

Et un lien symbolique n’existe pas. Si vous exécutez un exe à partir de ce chemin, vous l'exécutez effectivement à partir du chemin indiqué par le lien symbolique.

90
RedX

Avez-vous essayé QCoreApplication :: applicationDirPath ()

qDebug() << "App path : " << qApp->applicationDirPath();
52
KaZ

Pour ajouter à la réponse KaZ, chaque fois que je fais une application QML, j'ai tendance à l'ajouter au c ++ principal.

#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();
}

Utilisation en QML

....
........
............
Text{
text:"This is the applications path: " + appPath
+ "\nThis is the users home directory: " + homePath
+ "\nThis is the Desktop path: " desktopPath;
}
9
joseph

J'exécute Qt 5.5 sous Windows et le constructeur par défaut de QDir semble choisir le répertoire de travail actuel, pas le répertoire de l'application.

Je ne sais pas si le getenv PWD fonctionnera sur plusieurs plates-formes et je pense qu'il est défini sur le répertoire de travail actuel lorsque le shell a lancé l'application et n'inclut pas les modifications apportées au répertoire de travail effectuées par l'application elle-même le PO voit ce comportement).

J'ai donc pensé ajouter d'autres moyens qui devraient vous donner le répertoire de travail actuel (pas l'emplacement binaire de l'application):

// 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;
3
Mark Walker