Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QTemporaryFile Class

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

 #include <QTemporaryFile>

Inherits: QFile.

Note: All functions in this class are reentrant.

Public Types

flags OpenMode
enum OpenModeFlag { NotOpen, ReadOnly, WriteOnly, ReadWrite, ..., Unbuffered }

Public Functions

QIODevice()
QIODevice(QObject * parent)
QTemporaryFile()
QTemporaryFile(const QString & templateName)
QTemporaryFile(QObject * parent)
QTemporaryFile(const QString & templateName, QObject * parent)
virtual ~QIODevice()
~QTemporaryFile()
virtual bool atEnd() const
bool autoRemove() const
virtual qint64 bytesAvailable() const
virtual qint64 bytesToWrite() const
virtual bool canReadLine() const
virtual void close()
QString errorString() const
QString fileTemplate() const
bool getChar(char * c)
bool isOpen() const
bool isReadable() const
virtual bool isSequential() const
bool isTextModeEnabled() const
bool isWritable() const
bool open()
OpenMode openMode() const
qint64 peek(char * data, qint64 maxSize)
QByteArray peek(qint64 maxSize)
virtual qint64 pos() const
bool putChar(char c)
qint64 read(char * data, qint64 maxSize)
QByteArray read(qint64 maxSize)
QByteArray readAll()
qint64 readLine(char * data, qint64 maxSize)
QByteArray readLine(qint64 maxSize = 0)
virtual bool reset()
virtual bool seek(qint64 pos)
void setAutoRemove(bool b)
void setFileTemplate(const QString & name)
void setTextModeEnabled(bool enabled)
virtual qint64 size() const
void ungetChar(char c)
virtual bool waitForBytesWritten(int msecs)
virtual bool waitForReadyRead(int msecs)
qint64 write(const char * data, qint64 maxSize)
qint64 write(const char * data)
qint64 write(const QByteArray & byteArray)

Reimplemented Public Functions

virtual QString fileName() const
  • 15 public functions inherited from QFile
  • 16 public functions inherited from QFileDevice
  • 31 public functions inherited from QObject

Signals

void aboutToClose()
void bytesWritten(qint64 bytes)
void readChannelFinished()
void readyRead()

Static Public Members

QTemporaryFile * createLocalFile(QFile & file)
QTemporaryFile * createLocalFile(const QString & fileName)
  • 12 static public members inherited from QFile
  • 11 static public members inherited from QObject

Protected Functions

virtual qint64 readData(char * data, qint64 maxSize) = 0
virtual qint64 readLineData(char * data, qint64 maxSize)
void setErrorString(const QString & str)
void setOpenMode(OpenMode openMode)
virtual qint64 writeData(const char * data, qint64 maxSize) = 0

Reimplemented Protected Functions

virtual bool open(OpenMode flags)
  • 3 protected functions inherited from QFileDevice
  • 9 protected functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject

Detailed Description

The 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's constructor.

Example:

     // 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.

See also QDir::tempPath() and QFile.

Member Function Documentation

QTemporaryFile::QTemporaryFile()

Constructs a 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().

See also setFileTemplate() and QDir::tempPath().

QTemporaryFile::QTemporaryFile(const QString & templateName)

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

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

If 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.

See also open() and fileTemplate().

QTemporaryFile::QTemporaryFile(QObject * parent)

Constructs a 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().

See also setFileTemplate().

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

Constructs a 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.

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

If 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.

See also open() and 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.

See also autoRemove().

bool QTemporaryFile::autoRemove() const

Returns true if the 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.

See also setAutoRemove() and remove().

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

If file is not on a local disk, a temporary file is created on a local disk, file is copied into the temporary local file, and a pointer to the temporary local file is returned. If file is already on a local disk, a copy is not created and 0 is returned.

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

This is an overloaded function.

Works on the given fileName rather than an existing QFile object.

QString QTemporaryFile::fileName() const [virtual]

Reimplemented from 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.

See also 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().

See also 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.

See also fileName().

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

Reimplemented from 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)

Sets the QTemporaryFile into auto-remove mode if b is true.

Auto-remove is on by default.

See also autoRemove() and remove().

void QTemporaryFile::setFileTemplate(const QString & name)

Sets the static portion of the file name to name. If the file template ends in 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.

If 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.

See also fileTemplate().

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web