Set the environment before. The Environment of the process is inherited to its children (here: the command you want to start with QProcess)
We're using this code to archive it (Works under Windows, Linux and OSX)
// header
enum EnvironmentSetOperationResult {
CouldSetEvironmentVariable = 0,
FailedToSetEnvironmentVariable = -1
};
// cpp
#include <cstdlib>
#ifdef Q_WS_WIN
// this is needed for the environment functions. Else the function will use unicode
// and will not work with char*
#undef UNICODE
#include <windows.h>
#endif
// [...]
void Environment::set(const QString& key, const QString& value) {
// attention!
//linux 0 == success, -1 == error
//windows 0 == error, !0 == success
int num_error = FailedToSetEnvironmentVariable;
#ifdef Q_WS_WIN
if(value.isNull()) {
num_error = SetEnvironmentVariableA((LPCSTR) key.toAscii().constData(), NULL);
} else {
num_error = SetEnvironmentVariableA((LPCSTR) key.toAscii().constData(), (LPCSTR) value.toAscii().constData());
}
num_error = (num_error == 0 ? FailedToSetEnvironmentVariable : CouldSetEvironmentVariable);
#else
const int overwrite_variable = 1;
num_error = setenv(key_ba.constData(), value_ba.constData(), overwrite_variable);
#endif
if (num_error == FailedToSetEnvironmentVariable) {
qDebug() << "Environment::set(): Could not set'" << key << "' to '" << value << "'";
}
}
// header
enum EnvironmentSetOperationResult {
CouldSetEvironmentVariable = 0,
FailedToSetEnvironmentVariable = -1
};
// cpp
#include <cstdlib>
#ifdef Q_WS_WIN
// this is needed for the environment functions. Else the function will use unicode
// and will not work with char*
#undef UNICODE
#include <windows.h>
#endif
// [...]
void Environment::set(const QString& key, const QString& value) {
// attention!
//linux 0 == success, -1 == error
//windows 0 == error, !0 == success
int num_error = FailedToSetEnvironmentVariable;
QByteArray key_ba = key.toLatin1();
QByteArray value_ba = value.toLatin1();
#ifdef Q_WS_WIN
if(value.isNull()) {
num_error = SetEnvironmentVariableA((LPCSTR) key.toAscii().constData(), NULL);
} else {
num_error = SetEnvironmentVariableA((LPCSTR) key.toAscii().constData(), (LPCSTR) value.toAscii().constData());
}
num_error = (num_error == 0 ? FailedToSetEnvironmentVariable : CouldSetEvironmentVariable);
#else
const int overwrite_variable = 1;
num_error = setenv(key_ba.constData(), value_ba.constData(), overwrite_variable);
#endif
if (num_error == FailedToSetEnvironmentVariable) {
qDebug() << "Environment::set(): Could not set'" << key << "' to '" << value << "'";
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks