QTemporaryFile 类

QTemporaryFile class is an I/O device that operates on temporary files. 更多...

头: #include <QTemporaryFile>
qmake: QT += core
继承: QFile

注意: 此类的所有函数 可重入 .

公共函数

QTemporaryFile ()
QTemporaryFile (const QString & templateName )
QTemporaryFile (QObject * parent )
QTemporaryFile (const QString & templateName , QObject * parent )
~QTemporaryFile ()
bool autoRemove () const
QString fileTemplate () const
bool open ()
void setAutoRemove (bool b )
void setFileTemplate (const QString & name )

重实现公共函数

virtual QString fileName () const

静态公共成员

QTemporaryFile * createNativeFile (QFile & file )
QTemporaryFile * createNativeFile (const QString & fileName )
  • 12 static public members inherited from QFile
  • 11 静态公共成员继承自 QObject

重实现保护函数

virtual bool open (OpenMode flags )

额外继承成员

详细描述

QTemporaryFile class is an I/O device that operates on temporary files.

QTemporaryFile is used to create unique temporary files safely. The file itself is created by calling open (). The name of the temporary file is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing file), and the file will subsequently be removed upon destruction of the QTemporaryFile object. This is an important technique that avoids data corruption for applications that store data in temporary files. The file name is either auto-generated, or created based on a template, which is passed to QTemporaryFile 的构造函数。

范例:

    // Within a function/method...
    QTemporaryFile file;
    if (file.open()) {
        // file.fileName() returns the unique file name
    }
    // The QTemporaryFile destructor removes the temporary file
    // as it goes out of scope.
					

Reopening a QTemporaryFile after calling close () is safe. For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile .

The file name of the temporary file can be found by calling fileName (). Note that this is only defined after the file is first opened; the function returns an empty string before this.

A temporary file will have some static part of the name and some part that is calculated to be unique. The default filename will be determined from QCoreApplication::applicationName () (otherwise qt_temp ) and will be placed into the temporary path as returned by QDir::tempPath (). If you specify your own filename, a relative file path will not be placed in the temporary directory by default, but be relative to the current working directory.

Specified filenames can contain the following template XXXXXX (six upper case "X" characters), which will be replaced by the auto-generated portion of the filename. Note that the template is case sensitive. If the template is not present in the filename, QTemporaryFile appends the generated part to the filename given.

另请参阅 QDir::tempPath () 和 QFile .

成员函数文档编制

QTemporaryFile:: QTemporaryFile ()

构造 QTemporaryFile using as file template the application name returned by QCoreApplication::applicationName () (otherwise qt_temp ) followed by ".XXXXXX". The file is stored in the system's temporary directory, QDir::tempPath ().

另请参阅 setFileTemplate () 和 QDir::tempPath ().

QTemporaryFile:: QTemporaryFile (const QString & templateName )

构造 QTemporaryFile with a template filename of templateName . Upon opening the temporary file this will be used to create a unique filename.

templateName does not contain XXXXXX it will automatically be appended and used as the dynamic portion of the filename.

templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath () to construct templateName if you want use the system's temporary directory.

另请参阅 open () 和 fileTemplate ().

QTemporaryFile:: QTemporaryFile ( QObject * parent )

构造 QTemporaryFile (with the given parent ) using as file template the application name returned by QCoreApplication::applicationName () (otherwise qt_temp ) followed by ".XXXXXX". The file is stored in the system's temporary directory, QDir::tempPath ().

另请参阅 setFileTemplate ().

QTemporaryFile:: QTemporaryFile (const QString & templateName , QObject * parent )

构造 QTemporaryFile with a template filename of templateName and the specified parent . Upon opening the temporary file this will be used to create a unique filename.

templateName does not contain XXXXXX it will automatically be appended and used as the dynamic portion of the filename.

templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath () to construct templateName if you want use the system's temporary directory.

另请参阅 open () 和 fileTemplate ().

QTemporaryFile:: ~QTemporaryFile ()

Destroys the temporary file object, the file is automatically closed if necessary and if in auto remove mode it will automatically delete the file.

另请参阅 autoRemove ().

bool QTemporaryFile:: autoRemove () const

返回 true QTemporaryFile is in auto remove mode. Auto-remove mode will automatically delete the filename from disk upon destruction. This makes it very easy to create your QTemporaryFile object on the stack, fill it with data, read from it, and finally on function return it will automatically clean up after itself.

Auto-remove is on by default.

另请参阅 setAutoRemove () 和 remove ().

[static] QTemporaryFile *QTemporaryFile:: createNativeFile ( QFile & file )

file is not already a native file, then a QTemporaryFile is created in QDir::tempPath (), the contents of file is copied into it, and a pointer to the temporary file is returned. Does nothing and returns 0 if file is already a native file.

例如:

QFile f(":/resources/file.txt");
QTemporaryFile::createNativeFile(f); // Returns a pointer to a temporary file
QFile f("/users/qt/file.txt");
QTemporaryFile::createNativeFile(f); // Returns 0
					

另请参阅 QFileInfo::isNativePath ().

[static] QTemporaryFile *QTemporaryFile:: createNativeFile (const QString & fileName )

这是重载函数。

Works on the given fileName rather than an existing QFile 对象。

[virtual] QString QTemporaryFile:: fileName () const

重实现自 QFileDevice::fileName ().

Returns the complete unique filename backing the QTemporaryFile object. This string is null before the QTemporaryFile is opened, afterwards it will contain the fileTemplate () plus additional characters to make it unique.

另请参阅 fileTemplate ().

QString QTemporaryFile:: fileTemplate () const

Returns the set file template. The default file template will be called qcoreappname.XXXXXX and be placed in QDir::tempPath ().

另请参阅 setFileTemplate ().

bool QTemporaryFile:: open ()

A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, this allows easy access to the data in the file. This function will return true upon success and will set the fileName () to the unique filename used.

另请参阅 fileName ().

[virtual protected] bool QTemporaryFile:: open ( OpenMode flags )

重实现自 QIODevice::open ().

Creates a unique file name for the temporary file, and opens it. You can get the unique name later by calling fileName (). The file is guaranteed to have been created by this function (i.e., it has never existed before).

void QTemporaryFile:: setAutoRemove ( bool b )

设置 QTemporaryFile into auto-remove mode if b 为 true。

Auto-remove is on by default.

另请参阅 autoRemove () 和 remove ().

void QTemporaryFile:: setFileTemplate (const QString & name )

Sets the static portion of the file name to name . If the file template contains XXXXXX that will automatically be replaced with the unique part of the filename, otherwise a filename will be determined automatically based on the static portion specified.

name contains a relative file path, the path will be relative to the current working directory. You can use QDir::tempPath () to construct name if you want use the system's temporary directory.

另请参阅 fileTemplate ().