QTemporaryDir 類創建用於臨時使用的唯一目錄。 更多...
| 頭: | #include <QTemporaryDir> |
| qmake: | QT += core |
注意: 此類的所有函數 可重入 .
| QTemporaryDir (const QString & templatePath ) | |
| QTemporaryDir () | |
| ~QTemporaryDir () | |
| bool | autoRemove () const |
| QString | errorString () const |
| QString | filePath (const QString & fileName ) const |
| bool | isValid () const |
| QString | path () const |
| bool | remove () |
| void | setAutoRemove (bool b ) |
QTemporaryDir 用於安全創建唯一臨時目錄。目錄本身是由構造函數創建的。臨時目錄名保證唯一(即:保證不覆寫現有目錄),且隨後會移除目錄,當 QTemporaryDir 對象銷毀時。目錄名要麼是自動生成的,要麼是基於模闆 (被傳遞給 QTemporaryDir 構造函數) 創建的。
範例:
// Within a function/method...
QTemporaryDir dir;
if (dir.isValid()) {
// dir.path() returns the unique directory path
}
// The QTemporaryDir destructor removes the temporary directory
// as it goes out of scope.
測試可以創建臨時目錄非常重要,是使用 isValid ()。不要使用 exists() , since a default-constructed QDir 錶示當前存在目錄。
臨時目錄路徑的查找可以通過調用 path ().
A temporary directory will have some static part of the name and some part that is calculated to be unique. The default path will be determined from
QCoreApplication::applicationName
() (否則
qt_temp
) and will be placed into the temporary path as returned by
QDir::tempPath
(). If you specify your own path, a relative path will not be placed in the temporary directory by default, but be relative to the current working directory. In all cases, a random string will be appended to the path in order to make it unique.
另請參閱 QDir::tempPath (), QDir ,和 QTemporaryFile .
構造 QTemporaryDir 采用模闆化的 templatePath .
若 templatePath is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath () to construct templatePath if you want use the system's temporary directory.
若 templatePath ends with XXXXXX it will be used as the dynamic portion of the directory name, otherwise it will be appended. Unlike QTemporaryFile , XXXXXX in the middle of the template string is not supported.
另請參閱 QDir::tempPath ().
Constructs a QTemporaryDir using as template the application name returned by
QCoreApplication::applicationName
() (否則
qt_temp
). The directory is stored in the system's temporary directory,
QDir::tempPath
().
另請參閱 QDir::tempPath ().
Destroys the temporary directory object. If auto remove mode was set, it will automatically delete the directory including all its contents.
另請參閱 autoRemove ().
返迴
true
若
QTemporaryDir
is in auto remove mode. Auto-remove mode will automatically delete the directory from disk upon destruction. This makes it very easy to create your
QTemporaryDir
object on the stack, fill it with files, do something with the files, and finally on function return it will automatically clean up after itself.
默認情況下,開啓自動移除。
另請參閱 setAutoRemove () 和 remove ().
若
isValid
() 返迴
false
, this function returns the error string that explains why the creation of the temporary directory failed. Otherwise, this function return an empty string.
該函數在 Qt 5.6 引入。
Returns the path name of a file in the temporary directory. Does not check if the file actually exists in the directory. Redundant multiple separators or "." and ".." directories in fileName are not removed (see QDir::cleanPath ())。絕對路徑不允許。
該函數在 Qt 5.9 引入。
返迴
true
若
QTemporaryDir
創建成功。
Returns the path to the temporary directory. Empty if the QTemporaryDir could not be created.
移除臨時目錄,包括其下所有內容。
返迴
true
若移除成功。
設置 QTemporaryDir 成自動移除模式若 b 為 true。
默認情況下,開啓自動移除。
另請參閱 autoRemove () 和 remove ().