通过Qt程序添加环境变量,qputenv()函数、qgetenv()函数

这篇博客介绍了在Windows系统中遇到环境变量Path被删除导致程序启动问题的解决方案,主要利用Qt的qgetenv()和qputenv()函数进行环境变量的读取和设置。通过qgetenv()获取系统Path,然后使用qputenv()添加Python路径到Path,确保程序能正确运行。尽管添加的环境变量是临时的,只对当前程序生效,但能有效解决程序稳定性问题。
摘要由CSDN通过智能技术生成

最近在进行一个项目的时候遇到一个问题,windows某些的情况下会不定时的删除自己的系统环境变量Path,这会导致程序莫名其妙的启动不成功,虽然可以靠在程序启动失败的时候手工添加系统环境变量来弥补,但是还是对程序的稳定性造成了影响,所以想通过Qt程序添加系统环境变量,经过翻阅网上相关资料,找到了qgetenv,以及qputenv两个函数。

1.qgetenv()函数

关于qgetenv()函数,官方文档是这么说的:

Returns the value of the environment variable with name varName as a QByteArray. If no variable by that name is found in the environment, this function returns a default-constructed QByteArray.

The Qt environment manipulation functions are thread-safe, but this requires that the C library equivalent functions like getenv and putenv are not directly called.

To convert the data to a QString use QString::fromLocal8Bit().

Note: on desktop Windows, qgetenv() may produce data loss if the original string contains Unicode characters not representable in the ANSI encoding. Use qEnvironmentVariable() instead. On Unix systems, this function is lossless.

大概意思是:返回名称为 varName 的环境变量的值作为 一个数组。如果在环境中找不到具有该名称的变量,则此函数将返回默认构造的数组。 注意:在 Windows 上,如果原始字符串包含无法在 ANSI 编码中表示的 Unicode 字符,则 qgetenv() 可能会产生数据丢失。请改用 qEnvironmentVariable() 。在Unix系统上,这个功能是无损的。

下面我们尝试获取本机的系统环境变量

QString originalPath = qgetenv("PATH");
QStringList pathList1 = originalPath.split(";");
qDebug()<<"当前程序运行环境变量Path列表为:";
for(int i=0 ;i<pathList1.size();i++)
{
    qDebug()<<"环境变量"<<i<<"为:"<<pathList1.at(i);
}

运行结果如下:

 可以看到函数返回了计算机当前状态所有的环境变量,这其中不只有我们在计算机设置Path中看到的,还有一些是一些程序所附带的环境变量。

2.qputenv()

关于qputenv()官方文档是这么说的:

This function sets the value of the environment variable named varName. It will create the variable if it does not exist. It returns 0 if the variable could not be set.

Calling qputenv with an empty value removes the environment variable on Windows, and makes it set (but empty) on Unix. Prefer using qunsetenv() for fully portable behavior.

Note: qputenv() was introduced because putenv() from the standard C library was deprecated in VC2005 (and later versions). qputenv() uses the replacement function in VC, and calls the standard C library's implementation on all other platforms.

大体意思是:此函数设置名为 varName 的环境变量的值。如果变量不存在,它将创建该变量。如果无法设置变量,则返回 0。使用空值调用 qputenv 会删除 Windows 上的环境变量,但是在 Unix 上可以设置(但为空)。

下面我们就可以通过这个函数添加环境变量

    //获取环境变量初始值    
    QString originalPath = qgetenv("PATH");
    QString pythonPath = "C:\\Python3.7\\Scripts";
    if(!originalPath.contains("C:\\Python3.7\\Scripts"))
    {
        qDebug()<<"当前程序不存在python程序运行所需Path环境变量";
        
        //添加环境变量
        QString path = pythonPath + ";" + qgetenv("Path");
        bool putenvFlag = qputenv("path", path.toStdString().c_str());
        if(putenvFlag)
        {
            qDebug()<<"Path环境变量"<<pythonPath<<"添加成功";
        }
    }

    QString currentPath = qgetenv("Path");
    QStringList pathList = currentPath.split(";");
    qDebug()<<"当前程序运行环境变量Path列表为:";
    for(int i=0 ;i<pathList.size();i++)
    {
        qDebug()<<"环境变量"<<i<<"为:"<<pathList.at(i);
    }
    qDebug()<<"所有程序运行环境变量Path加载完成";

执行结果如下:

可以看到C:\\Python3.7\\Scrripts这个变量已经被添加到环境变量中了,需要注意的是,通过qputenv()添加的系统环境变量不能在系统设置界面直接被查看到,但是经过测试这个环境变量确实存在(否则程序会崩),并且需要注意一点,这个环境变量是跟随这个程序而存在的,这个程序退出,这个环境变量就不存在了,所以也可以说是专门为这个程序添加的环境变量,换句话说,这个环境变量是个临时的。


 

  • 8
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值