Frage

I am working on a Qt project and I want the working directories to be dynamically set for the program will be run on different systems.

I have enclosed the code for your reference.

QProcess Home;
Home.start("echo",QStringList() << "$HOME");
Home.waitForFinished(-1);
qDebug() << Home.readAllStandardOutput();

But the qDebug() prints "$HOME" and not the actual home path. Why does this happen? Is There any other way of doing this?

War es hilfreich?

Lösung

You can use std::getenv to retrieve the home path set in the processes environment.

#include <cstdlib>

const char *homePath = std::getenv("HOME");
if(homePath != NULL)
{
    QProcess Home;
    Home.start("echo",QStringList() << homePath);
    Home.waitForFinished(-1);
    qDebug() << Home.readAllStandardOutput();
}

Andere Tipps

Here is another way to do it.

QStringList QProcess::systemEnvironment () [static]

http://qt-project.org/doc/qt-4.8/qprocess.html#systemEnvironment

 QStringList environment = QProcess::systemEnvironment();
  // environment = {"PATH=/usr/bin:/usr/local/bin",
  //                "USER=greg", "HOME=/home/greg"}

Hope that helps.

QByteArray qgetenv ( const char * varName ) is the function provided by QT library to fetch any environment variable on all platforms.

getenv() seems to be deprecated on Windows VS2005 onward more info here

Especially for home path you should use QDir::homePath()

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top