<QtEnvironmentVariables> Proxy Page
函数
| QString | qEnvironmentVariable (const char * varName , const QString & defaultValue ) | 
| QString | qEnvironmentVariable (const char * varName ) | 
| int | qEnvironmentVariableIntValue (const char * varName , bool * ok = nullptr) | 
| bool | qEnvironmentVariableIsEmpty (const char * varName ) | 
| bool | qEnvironmentVariableIsSet (const char * varName ) | 
| QByteArray | qgetenv (const char * varName ) | 
| bool | qputenv (const char * varName , QByteArrayView value ) | 
| bool | qunsetenv (const char * varName ) | 
函数文档编制
QString qEnvironmentVariable (const char * varName )
QString qEnvironmentVariable (const char * varName , const QString & defaultValue )
These functions return the value of the environment variable, varName , as a QString . If no variable varName is found in the environment and defaultValue is provided, defaultValue is returned. Otherwise QString() is returned.
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.
The following table describes how to choose between qgetenv () 和 qEnvironmentVariable ():
| Condition | Recommendation | 
|---|---|
| Variable contains file paths or user text | qEnvironmentVariable () | 
| Windows-specific code | qEnvironmentVariable () | 
| Unix-specific code, destination variable is not QString and/or is used to interface with non-Qt APIs | qgetenv () | 
| Destination variable is a QString | qEnvironmentVariable () | 
| Destination variable is a QByteArray or std::string | qgetenv () | 
注意: on Unix systems, this function may produce data loss if the original string contains arbitrary binary data that cannot be decoded by the locale codec. Use qgetenv () instead for that case. On Windows, this function is lossless.
注意: the variable name varName must contain only US-ASCII characters.
另请参阅 qputenv (), qgetenv (), qEnvironmentVariableIsSet (),和 qEnvironmentVariableIsEmpty ().
QString qEnvironmentVariable (const char * varName , const QString & defaultValue )
QString qEnvironmentVariable (const char * varName )
					
[noexcept]
					
					
						int
					
					
						qEnvironmentVariableIntValue
					
					(const
					
						char
					
					*
					
						varName
					
					,
					
						bool
					
					*
					
						ok
					
					= nullptr)
					
				
				
					Returns the numerical value of the environment variable
					
						varName
					
					。若
					
						ok
					
					is not null, sets
					
*ok
					
					to
					
true
					
					or
					
false
					
					depending on the success of the conversion.
				
相当于
    qgetenv(varName).toInt(ok, 0)
				
				except that it's much faster, and can't throw exceptions.
					
						注意:
					
					there's a limit on the length of the value, which is sufficient for all valid values of int, not counting leading zeroes or spaces. Values that are too long will either be truncated or this function will set
					
						ok
					
					to
					
false
					
					.
				
另请参阅 qgetenv (), qEnvironmentVariable (),和 qEnvironmentVariableIsSet ().
					
[noexcept]
					
					
						bool
					
					
						qEnvironmentVariableIsEmpty
					
					(const
					
						char
					
					*
					
						varName
					
					)
					
				
				Returns whether the environment variable varName is empty.
相当于
    qgetenv(varName).isEmpty()
				
				except that it's potentially much faster, and can't throw exceptions.
另请参阅 qgetenv (), qEnvironmentVariable (),和 qEnvironmentVariableIsSet ().
					
[noexcept]
					
					
						bool
					
					
						qEnvironmentVariableIsSet
					
					(const
					
						char
					
					*
					
						varName
					
					)
					
				
				Returns whether the environment variable varName 有设置。
相当于
    !qgetenv(varName).isNull()
				
				except that it's potentially much faster, and can't throw exceptions.
另请参阅 qgetenv (), qEnvironmentVariable (),和 qEnvironmentVariableIsEmpty ().
QByteArray qgetenv (const char * varName )
Returns the value of the environment variable with name varName 作为 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 使用 QString::fromLocal8Bit ().
注意: 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.
注意: 此函数是 thread-safe .
另请参阅 qputenv (), qEnvironmentVariable (), qEnvironmentVariableIsSet (),和 qEnvironmentVariableIsEmpty ().
bool qputenv (const char * varName , QByteArrayView value )
此函数设置 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.
注意: 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.
注意: In Qt versions prior to 6.5, the value argument was QByteArray , not QByteArrayView .
另请参阅 qgetenv () 和 qEnvironmentVariable ().
bool qunsetenv (const char * varName )
This function deletes the variable varName from the environment.
					返回
					
true
					
					当成功时。
				
另请参阅 qputenv (), qgetenv (),和 qEnvironmentVariable ().
