Extract QProcess environment variables

I want to run a script environment in QProcess, and then read the environment (like a QStringList) to run other scripts with this environment.

If I run env script and read the environment, I always get an empty QStringList. Is there a way to read the QProcess environment?

I also tried to start the script environment first and run the actual script in the same QProcess object, but that didn't help either.

+3
source share
4 answers

If you can rewrite the script that installs the environment in C ++, you can create the environment yourself and install it using the voidQProcess::setProcessEnvironment ( const QProcessEnvironment & environment ) method, as in the example given in the documentation method:

 QProcess process;
 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
 env.insert("TMPDIR", "C:\\MyApp\\temp"); // Add an environment variable
 env.insert("PATH", env.value("Path") + ";C:\\Bin");
 process.setProcessEnvironment(env);
 process.start("myapp");

UPDATE

, cmd.exe :

#include <QtCore/QCoreApplication>
#include <QtCore/QProcess>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QProcess* proc = new QProcess();
    proc->start("cmd.exe /c \"call env.bat && script.bat\"");

    return app.exec();
}

env.bat

set abc=test

script.bat

echo %abc% > a.txt

a.txt

test 
+3

setEnvironment QProcess, QStringList . QProcess . ,

QStringList env(QProcess::systemEnvironment());

.

+1

, QProcess, ( ). .

QProcessEnvironment env = QProcessEnvironment::systemEnvironment();

qDebug() << "All variables";

QString env_variable;
QStringList paths_list = env.toStringList();

foreach( env_variable, paths_list )
    qDebug() << env_variable;
0

; vcvarsall.bat msvc, ():

  • .cmd script , , ;
  • ... cmd script ""
    call ...\vc\vcvarsall.bat
    call may\be\another.cmd
    echo {5c131c2a-405b-478a-8279-9dff52c31537}
    set
    
  • script QProcess readAllStandardOutput(), .
  • , , , , - , ,
  • ... = QProcessEnvironment ( ) .
0

Source: https://habr.com/ru/post/1792875/


All Articles