如何在Qt5中使用Windows环境变量
How to use Windows environment variables in Qt5
我正在做一个项目,我想从C:/Windows/media
访问一个声音文件,但为了保持它更通用,我想使用用户系统中的一些环境变量。
此代码目前有效
soundURL = QUrl::fromUserInput(soundFilename,
QStringLiteral("C:/Windows/media"),
QUrl::AssumeLocalFile);
我尝试了以下代码,不起作用
soundURL = QUrl::fromUserInput(soundFilename,
QStringLiteral((%%WINDIR%%)+"/media"),
QUrl::AssumeLocalFile);
如何利用 %WINDIR% 使路径更简单、更通用?
Qt5公开了几个函数来检索存储在环境变量中的值,即qgetenv和qEnvironmentVariable。
由于您似乎以Windows为目标,因此使用QString qEnvironmentVariable(const char *varName)
QString winDirPath = qEnvironmentVariable("WINDIR");
if (!winDirPath.isNull()) {
// the environment variable WINDIR exists and has been retrieved
} else {
// the environment variable does not exists in this system
}
string path(getenv("WINDIR"));
将 %WINDIR% 放在 std::string 中。我希望你可以对Qt类型做同样的事情。
您可能最好使用Qt标准路径 http://doc.qt.io/qt-5/qstandardpaths.html。弄乱%WINDIR%有点危险。
相关文章:
- 枚举环境变量的惯用C++14/C++17方法
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 定义C++新的环境变量并在 bat 文件中使用它
- docker 构建失败:无法设置环境变量
- 如何在不传递命令行参数的情况下在 c++ 中设置环境变量
- C++环境变量
- 在编译时为运行时设置环境变量
- 如何使用可视化代码和平台IO将环境变量注入CPP文件?
- 访问 PlatformIO 中的外部环境变量
- WSL 看不到环境变量
- 如何在提升构建中设置环境变量以进行提升单元测试框架?
- 注册 TBB 的环境变量
- CMake 看不到SDL2_PATH环境变量 (Windows)
- C++ - 我需要一个宏或环境变量来检测我何时在 Windows 上运行 bazel
- 如何在Qt5中使用Windows环境变量
- 如何在安装MinGW (Windows 32位)时设置环境变量PATH
- 如何在MSYS上访问windows环境变量
- 在Windows 7下运行MinGW gcc编译器,不设置环境变量
- 如何完全清除所有的用户环境变量在Windows c++
- Win32释放环境变量导致Windows断点