Set Environment Variables for startDetached() QPro

2019-01-29 12:35发布

In Qt4, there is QProcess::setProcessEnvironment() for setting Env variables for the newly spawn process.

However, QProcess::startDetached() is a static member function, so setProcessEnvironment() doesn't apply. How does one set Env variables for a detached process in Qt?

2条回答
Viruses.
2楼-- · 2019-01-29 13:01

It is a known old bug: http://bugreports.qt-project.org/browse/QTBUG-2284. You need to overload startDetached function to support your own environment. Take a look at Qt sources to see how to do that: http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/io?h=5.5 (qprocess* files).

查看更多
女痞
3楼-- · 2019-01-29 13:07

Using Qt5.5 now, Run into this problem.

Under Win7, Used code below, Set environment in father process, It seems that sub process inherit the environment. Not for sure, but it worked in my case.

Hope there is better solutions

QString oldPath = qgetenv( "Path" );
QByteArray newPath = ( QCoreApplication::applicationDirPath() + ";" + oldPath ).toLocal8Bit();
bool bSet = qputenv("Path", newPath);
if ( !bSet )
{
    qDebug()<<"Failed";
}
查看更多
登录 后发表回答