QProcessEnvironment 類

QProcessEnvironment 類保持可以被傳遞給程序的環境變量。 更多...

頭: #include <QProcessEnvironment>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

此類 相等可比較 .

注意: 此類的所有函數 可重入 .

公共類型

(從 6.3 起) enum Initialization { InheritFromParent }

公共函數

QProcessEnvironment ()
(從 6.3 起) QProcessEnvironment (QProcessEnvironment::Initialization)
QProcessEnvironment (const QProcessEnvironment & other )
~QProcessEnvironment ()
void clear ()
bool contains (const QString & name ) const
(從 6.3 起) bool inheritsFromParent () const
void insert (const QString & name , const QString & value )
void insert (const QProcessEnvironment & e )
bool isEmpty () const
QStringList keys () const
void remove (const QString & name )
void swap (QProcessEnvironment & other )
QStringList toStringList () const
QString value (const QString & name , const QString & defaultValue = QString()) const
QProcessEnvironment & operator= (const QProcessEnvironment & other )

靜態公共成員

QProcessEnvironment systemEnvironment ()
bool operator!= (const QProcessEnvironment & lhs , const QProcessEnvironment & rhs )
bool operator== (const QProcessEnvironment & lhs , const QProcessEnvironment & rhs )

詳細描述

A process's environment is composed of a set of key=value pairs known as environment variables. The QProcessEnvironment class wraps that concept and allows easy manipulation of those variables. It's meant to be used along with QProcess , to set the environment for child processes. It cannot be used to change the current process's environment.

The environment of the calling process can be obtained using QProcessEnvironment::systemEnvironment ().

On Unix systems, the variable names are case-sensitive. Note that the Unix environment allows both variable names and contents to contain arbitrary binary data (except for the NUL character). QProcessEnvironment will preserve such variables, but does not support manipulating variables whose names or values cannot be encoded by the current locale settings (see QString::toLocal8Bit ).

On Windows, the variable names are case-insensitive, but case-preserving. QProcessEnvironment behaves accordingly.

另請參閱 QProcess , QProcess::systemEnvironment (),和 QProcess::setProcessEnvironment ().

成員類型文檔編製

[since 6.3] enum QProcessEnvironment:: 初始化

此枚舉包含用於消除構造函數歧義的令牌。

常量 描述
QProcessEnvironment::InheritFromParent 0 A QProcessEnvironment will be created that, when set on a QProcess , causes it to inherit variables from its parent.

該枚舉在 Qt 6.3 引入。

成員函數文檔編製

QProcessEnvironment:: QProcessEnvironment ()

Creates a new QProcessEnvironment object. This constructor creates an empty environment. If set on a QProcess , this will cause the current environment variables to be removed (except for PATH and SystemRoot on Windows).

[noexcept, since 6.3] QProcessEnvironment:: QProcessEnvironment ( QProcessEnvironment::Initialization )

Creates an object that when set on QProcess will cause it to be executed with environment variables inherited from its parent process.

注意: The created object does not store any environment variables by itself, it just indicates to QProcess to arrange for inheriting the environment at the time when the new process is started. Adding any environment variables to the created object will disable inheritance of the environment and result in an environment containing only the added environment variables.

If a modified version of the parent environment is wanted, start with the return value of systemEnvironment() and modify that (but note that changes to the parent process's environment after that is created won't be reflected in the modified environment).

該函數在 Qt 6.3 引入。

另請參閱 inheritsFromParent () 和 systemEnvironment ().

QProcessEnvironment:: QProcessEnvironment (const QProcessEnvironment & other )

Creates a QProcessEnvironment object that is a copy of other .

[noexcept] QProcessEnvironment:: ~QProcessEnvironment ()

Frees the resources associated with this QProcessEnvironment 對象。

void QProcessEnvironment:: clear ()

Removes all key=value pairs from this QProcessEnvironment object, making it empty.

If the environment was constructed using QProcessEnvironment::InheritFromParent it remains unchanged.

另請參閱 isEmpty () 和 systemEnvironment ().

bool QProcessEnvironment:: contains (const QString & name ) const

返迴 true 若環境變量名 name 被找到在此 QProcessEnvironment 對象。

另請參閱 insert () 和 value ().

[since 6.3] bool QProcessEnvironment:: inheritsFromParent () const

返迴 true 若此 QProcessEnvironment 的構造是使用 QProcessEnvironment::InheritFromParent .

該函數在 Qt 6.3 引入。

另請參閱 isEmpty ().

void QProcessEnvironment:: insert (const QString & name , const QString & value )

插入環境變量按名稱 name 和內容 value 到此 QProcessEnvironment 對象。若變量已經存在,以新值替換它。

在大多數係統,插入沒有內容的變量對應用程序與根本沒有設置變量的效果相同。不管怎樣,為保證不存在不兼容,要移除變量請使用 remove () 函數。

另請參閱 contains (), remove (),和 value ().

void QProcessEnvironment:: insert (const QProcessEnvironment & e )

插入內容為 e 在此 QProcessEnvironment 對象。在此對象中的變量還存在於 e 會被覆寫。

這是重載函數。

bool QProcessEnvironment:: isEmpty () const

返迴 true 若此 QProcessEnvironment 對象為空:沒有設置 "鍵=值" 對。

此方法也返迴 true 若對象的構造是使用 QProcessEnvironment::InheritFromParent .

另請參閱 clear (), systemEnvironment (), insert (),和 inheritsFromParent ().

QStringList QProcessEnvironment:: keys () const

返迴包含所有變量名稱的列錶在此 QProcessEnvironment 對象。

返迴列錶為空,若對象的構造是使用 QProcessEnvironment::InheritFromParent .

void QProcessEnvironment:: remove (const QString & name )

移除環境變量標識通過 name 從此 QProcessEnvironment 對象。若變量之前不存在,什麼都不發生。

另請參閱 contains (), insert (),和 value ().

[noexcept] void QProcessEnvironment:: swap ( QProcessEnvironment & other )

交換此進程環境實例與 other 。此操作很快且從不失敗。

[static] QProcessEnvironment QProcessEnvironment:: systemEnvironment ()

The systemEnvironment function returns the environment of the calling process.

它被返迴作為 QProcessEnvironment . This function does not cache the system environment. Therefore, it's possible to obtain an updated version of the environment if low-level C library functions like setenv or putenv 有被調用。

However, note that repeated calls to this function will recreate the QProcessEnvironment object, which is a non-trivial operation.

另請參閱 QProcess::systemEnvironment ().

QStringList QProcessEnvironment:: toStringList () const

轉換此 QProcessEnvironment object into a list of strings, one for each environment variable that is set. The environment variable's name and its value are separated by an equal character ('=').

The QStringList contents returned by this function are suitable for presentation. Use with the QProcess::setEnvironment function is not recommended due to potential encoding problems under Unix, and worse performance.

另請參閱 systemEnvironment (), QProcess::systemEnvironment (),和 QProcess::setProcessEnvironment ().

QString QProcessEnvironment:: value (const QString & name , const QString & defaultValue = QString()) const

搜索此 QProcessEnvironment object for a variable identified by name and returns its value. If the variable is not found in this object, then defaultValue 被返迴取而代之。

另請參閱 contains (), insert (),和 remove ().

QProcessEnvironment &QProcessEnvironment:: operator= (const QProcessEnvironment & other )

拷貝內容從 other QProcessEnvironment 對象到此對象。

相關非成員

[noexcept] bool operator!= (const QProcessEnvironment & lhs , const QProcessEnvironment & rhs )

返迴 true if the process environment objects lhs and rhs are different.

另請參閱 operator== ().

[noexcept] bool operator== (const QProcessEnvironment & lhs , const QProcessEnvironment & rhs )

返迴 true if the process environment objects lhs and rhs 相等。

Two QProcessEnvironment objects are considered equal if they have the same set of key=value pairs. The comparison of keys is done case-sensitive on platforms where the environment is case-sensitive.

另請參閱 operator!= () 和 contains ().