Qt 4.8
Public Functions | Static Public Functions | Protected Functions | Friends | List of all members
QTemporaryFile Class Reference

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

#include <qtemporaryfile.h>

Inheritance diagram for QTemporaryFile:
QFile QIODevice QObject

Public Functions

bool autoRemove () const
 Returns true if the QTemporaryFile is in auto remove mode. More...
 
virtual QAbstractFileEnginefileEngine () const
 
QString fileName () const
 Returns the complete unique filename backing the QTemporaryFile object. More...
 
QString fileTemplate () const
 Returns the set file template. More...
 
bool open ()
 A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, this allows easy access to the data in the file. More...
 
 QTemporaryFile ()
 Constructs a QTemporaryFile in QDir::tempPath(), using the file template "qt_temp.XXXXXX". More...
 
 QTemporaryFile (const QString &templateName)
 Constructs a QTemporaryFile with a template filename of templateName. More...
 
 QTemporaryFile (QObject *parent)
 Constructs a QTemporaryFile (with the given parent) in QDir::tempPath(), using the file template "qt_temp.XXXXXX". More...
 
 QTemporaryFile (const QString &templateName, QObject *parent)
 Constructs a QTemporaryFile with a template filename of templateName and the specified parent. More...
 
void setAutoRemove (bool b)
 Sets the QTemporaryFile into auto-remove mode if b is true. More...
 
void setFileTemplate (const QString &name)
 Sets the static portion of the file name to name. More...
 
 ~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. More...
 
- Public Functions inherited from QFile
bool atEnd () const
 Returns true if the end of the file has been reached; otherwise returns false. More...
 
virtual void close ()
 Calls QFile::flush() and closes the file. More...
 
bool copy (const QString &newName)
 Copies the file currently specified by fileName() to a file called newName. More...
 
FileError error () const
 Returns the file error status. More...
 
bool exists () const
 Returns true if the file specified by fileName() exists; otherwise returns false. More...
 
QString fileName () const
 Returns the name set by setFileName() or to the QFile constructors. More...
 
bool flush ()
 Flushes any buffered data to the file. More...
 
int handle () const
 Returns the file handle of the file. More...
 
bool isSequential () const
 Returns true if the file can only be manipulated sequentially; otherwise returns false. More...
 
bool link (const QString &newName)
 Creates a link named linkName that points to the file currently specified by fileName(). More...
 
ucharmap (qint64 offset, qint64 size, MemoryMapFlags flags=NoOptions)
 Maps size bytes of the file into memory starting at offset. More...
 
bool open (FILE *f, OpenMode flags)
 Opens the existing file handle fh in the given mode. More...
 
bool open (int fd, OpenMode flags)
 Opens the existing file descriptor fd in the given mode. More...
 
bool open (FILE *f, OpenMode ioFlags, FileHandleFlags handleFlags)
 Opens the existing file handle fh in the given mode. More...
 
bool open (int fd, OpenMode ioFlags, FileHandleFlags handleFlags)
 Opens the existing file descriptor fd in the given mode. More...
 
Permissions permissions () const
 Returns the complete OR-ed together combination of QFile::Permission for the file. More...
 
qint64 pos () const
 Reimplemented Function More...
 
 QFile ()
 
 QFile (const QString &name)
 Constructs a new file object to represent the file with the given name. More...
 
 QFile (QObject *parent)
 Constructs a new file object with the given parent. More...
 
 QFile (const QString &name, QObject *parent)
 Constructs a new file object with the given parent to represent the file with the specified name. More...
 
QString readLink () const
 Use symLinkTarget() instead. More...
 
bool remove ()
 Removes the file specified by fileName(). More...
 
bool rename (const QString &newName)
 Renames the file currently specified by fileName() to newName. More...
 
bool resize (qint64 sz)
 Sets the file size (in bytes) sz. More...
 
bool seek (qint64 offset)
 For random-access devices, this function sets the current position to pos, returning true on success, or false if an error occurred. More...
 
void setFileName (const QString &name)
 Sets the name of the file. More...
 
bool setPermissions (Permissions permissionSpec)
 Sets the permissions for the file to the permissions specified. More...
 
qint64 size () const
 Returns the size of the file. More...
 
QString symLinkTarget () const
 Returns the absolute path of the file or directory a symlink (or shortcut on Windows) points to, or a an empty string if the object isn't a symbolic link. More...
 
bool unmap (uchar *address)
 Unmaps the memory address. More...
 
void unsetError ()
 Sets the file's error to QFile::NoError. More...
 
 ~QFile ()
 Destroys the file object, closing it if necessary. More...
 
- Public Functions inherited from QIODevice
virtual qint64 bytesAvailable () const
 Returns the number of bytes that are available for reading. More...
 
virtual qint64 bytesToWrite () const
 For buffered devices, this function returns the number of bytes waiting to be written. More...
 
virtual bool canReadLine () const
 Returns true if a complete line of data can be read from the device; otherwise returns false. More...
 
QString errorString () const
 Returns a human-readable description of the last device error that occurred. More...
 
bool getChar (char *c)
 Reads one character from the device and stores it in c. More...
 
bool isOpen () const
 Returns true if the device is open; otherwise returns false. More...
 
bool isReadable () const
 Returns true if data can be read from the device; otherwise returns false. More...
 
bool isTextModeEnabled () const
 Returns true if the Text flag is enabled; otherwise returns false. More...
 
bool isWritable () const
 Returns true if data can be written to the device; otherwise returns false. More...
 
OpenMode openMode () const
 Returns the mode in which the device has been opened; i.e. More...
 
qint64 peek (char *data, qint64 maxlen)
 Reads at most maxSize bytes from the device into data, without side effects (i. More...
 
QByteArray peek (qint64 maxlen)
 Peeks at most maxSize bytes from the device, returning the data peeked as a QByteArray. More...
 
bool putChar (char c)
 Writes the character c to the device. More...
 
 QIODevice ()
 Constructs a QIODevice object. More...
 
 QIODevice (QObject *parent)
 Constructs a QIODevice object with the given parent. More...
 
qint64 read (char *data, qint64 maxlen)
 Reads at most maxSize bytes from the device into data, and returns the number of bytes read. More...
 
QByteArray read (qint64 maxlen)
 Reads at most maxSize bytes from the device, and returns the data read as a QByteArray. More...
 
QByteArray readAll ()
 Reads all available data from the device, and returns it as a QByteArray. More...
 
qint64 readLine (char *data, qint64 maxlen)
 This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read. More...
 
QByteArray readLine (qint64 maxlen=0)
 Reads a line from the device, but no more than maxSize characters, and returns the result as a QByteArray. More...
 
virtual bool reset ()
 Seeks to the start of input for random-access devices. More...
 
void setTextModeEnabled (bool enabled)
 If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is removed. More...
 
void ungetChar (char c)
 Puts the character c back into the device, and decrements the current position unless the position is 0. More...
 
virtual bool waitForBytesWritten (int msecs)
 For buffered devices, this function waits until a payload of buffered written data has been written to the device and the bytesWritten() signal has been emitted, or until msecs milliseconds have passed. More...
 
virtual bool waitForReadyRead (int msecs)
 Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. More...
 
qint64 write (const char *data, qint64 len)
 Writes at most maxSize bytes of data from data to the device. More...
 
qint64 write (const char *data)
 Writes data from a zero-terminated string of 8-bit characters to the device. More...
 
qint64 write (const QByteArray &data)
 Writes the content of byteArray to the device. More...
 
virtual ~QIODevice ()
 The destructor is virtual, and QIODevice is an abstract base class. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Static Public Functions

static QTemporaryFilecreateLocalFile (const QString &fileName)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Works on the given fileName rather than an existing QFile object. More...
 
static QTemporaryFilecreateLocalFile (QFile &file)
 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. More...
 
- Static Public Functions inherited from QFile
static bool copy (const QString &fileName, const QString &newName)
 Copies the file fileName to newName. More...
 
static QString decodeName (const QByteArray &localFileName)
 This does the reverse of QFile::encodeName() using localFileName. More...
 
static QString decodeName (const char *localFileName)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the Unicode version of the given localFileName. More...
 
static QByteArray encodeName (const QString &fileName)
 By default, this function converts fileName to the local 8-bit encoding determined by the user's locale. More...
 
static bool exists (const QString &fileName)
 Returns true if the file specified by fileName exists; otherwise returns false. More...
 
static bool link (const QString &oldname, const QString &newName)
 Creates a link named linkName that points to the file fileName. More...
 
static Permissions permissions (const QString &filename)
 Returns the complete OR-ed together combination of QFile::Permission for fileName. More...
 
static QString readLink (const QString &fileName)
 Use symLinkTarget() instead. More...
 
static bool remove (const QString &fileName)
 Removes the file specified by the fileName given. More...
 
static bool rename (const QString &oldName, const QString &newName)
 Renames the file oldName to newName. More...
 
static bool resize (const QString &filename, qint64 sz)
 Sets fileName to size (in bytes) sz. More...
 
static void setDecodingFunction (DecoderFn)
 Sets the function for decoding 8-bit file names. More...
 
static void setEncodingFunction (EncoderFn)
 Sets the function for encoding Unicode file names. More...
 
static bool setPermissions (const QString &filename, Permissions permissionSpec)
 Sets the permissions for fileName file to permissions. More...
 
static QString symLinkTarget (const QString &fileName)
 Returns the absolute path of the file or directory referred to by the symlink (or shortcut on Windows) specified by fileName, or returns an empty string if the fileName does not correspond to a symbolic link. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 

Protected Functions

bool open (OpenMode flags)
 Creates a unique file name for the temporary file, and opens it. More...
 
- Protected Functions inherited from QFile
 QFile (QFilePrivate &dd, QObject *parent=0)
 
qint64 readData (char *data, qint64 maxlen)
 Reimplemented Function More...
 
qint64 readLineData (char *data, qint64 maxlen)
 Reimplemented Function More...
 
qint64 writeData (const char *data, qint64 len)
 Reimplemented Function More...
 
- Protected Functions inherited from QIODevice
 QIODevice (QIODevicePrivate &dd, QObject *parent=0)
 
void setErrorString (const QString &errorString)
 Sets the human readable description of the last device error that occurred to str. More...
 
void setOpenMode (OpenMode openMode)
 Sets the OpenMode of the device to openMode. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 

Friends

class QFile
 

Additional Inherited Members

- Public Types inherited from QFile
typedef QString(* DecoderFn) (const QByteArray &localfileName)
 This is a typedef for a pointer to a function with the following signature: More...
 
typedef QByteArray(* EncoderFn) (const QString &fileName)
 This is a typedef for a pointer to a function with the following signature: More...
 
enum  FileError {
  NoError = 0, ReadError = 1, WriteError = 2, FatalError = 3,
  ResourceError = 4, OpenError = 5, AbortError = 6, TimeOutError = 7,
  UnspecifiedError = 8, RemoveError = 9, RenameError = 10, PositionError = 11,
  ResizeError = 12, PermissionsError = 13, CopyError = 14
}
 This enum describes the errors that may be returned by the error() function. More...
 
enum  FileHandleFlag { AutoCloseHandle = 0x0001, DontCloseHandle = 0 }
 This enum is used when opening a file to specify additional options which only apply to files and not to a generic QIODevice. More...
 
enum  MemoryMapFlags { NoOptions = 0 }
 This enum describes special options that may be used by the map() function. More...
 
enum  Permission {
  ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000, ReadUser = 0x0400,
  WriteUser = 0x0200, ExeUser = 0x0100, ReadGroup = 0x0040, WriteGroup = 0x0020,
  ExeGroup = 0x0010, ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001
}
 This enum is used by the permission() function to report the permissions and ownership of a file. More...
 
- Public Types inherited from QIODevice
enum  OpenModeFlag {
  NotOpen = 0x0000, ReadOnly = 0x0001, WriteOnly = 0x0002, ReadWrite = ReadOnly | WriteOnly,
  Append = 0x0004, Truncate = 0x0008, Text = 0x0010, Unbuffered = 0x0020
}
 This enum is used with open() to describe the mode in which a device is opened. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QIODevice
void aboutToClose ()
 This signal is emitted when the device is about to close. More...
 
void bytesWritten (qint64 bytes)
 This signal is emitted every time a payload of data has been written to the device. More...
 
void readChannelFinished ()
 This signal is emitted when the input (reading) stream is closed in this device. More...
 
void readyRead ()
 This signal is emitted once every time new data is available for reading from the device. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

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

Note
This class or function is reentrant.

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...
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 qt_temp 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(), QFile

Definition at line 62 of file qtemporaryfile.h.

Constructors and Destructors

◆ QTemporaryFile() [1/4]

QTemporaryFile::QTemporaryFile ( )

Constructs a QTemporaryFile in QDir::tempPath(), using the file template "qt_temp.XXXXXX".

The file is stored in the system's temporary directory.

See also
setFileTemplate(), QDir::tempPath()

Definition at line 530 of file qtemporaryfile.cpp.

Referenced by createLocalFile(), and QTemporaryFilePrivate::~QTemporaryFilePrivate().

531  : QFile(*new QTemporaryFilePrivate, 0)
532 {
534  d->templateName = QDir::tempPath() + QLatin1String("/qt_temp.XXXXXX");
535 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_D(Class)
Definition: qglobal.h:2482
static QString tempPath()
Returns the absolute path of the system&#39;s temporary directory.
Definition: qdir.cpp:1987
QFile()
Definition: qfile.cpp:417

◆ QTemporaryFile() [2/4]

QTemporaryFile::QTemporaryFile ( const QString templateName)
explicit

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(), fileTemplate()

Definition at line 551 of file qtemporaryfile.cpp.

552  : QFile(*new QTemporaryFilePrivate, 0)
553 {
555  d->templateName = templateName;
556 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
#define Q_D(Class)
Definition: qglobal.h:2482
QFile()
Definition: qfile.cpp:417

◆ QTemporaryFile() [3/4]

QTemporaryFile::QTemporaryFile ( QObject parent)
explicit

Constructs a QTemporaryFile (with the given parent) in QDir::tempPath(), using the file template "qt_temp.XXXXXX".

See also
setFileTemplate()

Definition at line 564 of file qtemporaryfile.cpp.

565  : QFile(*new QTemporaryFilePrivate, parent)
566 {
568  d->templateName = QDir::tempPath() + QLatin1String("/qt_temp.XXXXXX");
569 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_D(Class)
Definition: qglobal.h:2482
static QString tempPath()
Returns the absolute path of the system&#39;s temporary directory.
Definition: qdir.cpp:1987
QFile()
Definition: qfile.cpp:417

◆ QTemporaryFile() [4/4]

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(), fileTemplate()

Definition at line 586 of file qtemporaryfile.cpp.

587  : QFile(*new QTemporaryFilePrivate, parent)
588 {
590  d->templateName = templateName;
591 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
#define Q_D(Class)
Definition: qglobal.h:2482
QFile()
Definition: qfile.cpp:417

◆ ~QTemporaryFile()

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()

Definition at line 601 of file qtemporaryfile.cpp.

602 {
604  close();
605  if (!d->fileName.isEmpty() && d->autoRemove)
606  remove();
607 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void close()
Calls QFile::flush() and closes the file.
Definition: qfile.cpp:1680

Functions

◆ 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(), remove()

Definition at line 635 of file qtemporaryfile.cpp.

636 {
637  Q_D(const QTemporaryFile);
638  return d->autoRemove;
639 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ createLocalFile() [1/2]

QTemporaryFile * QTemporaryFile::createLocalFile ( const QString fileName)
inlinestatic

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Works on the given fileName rather than an existing QFile object.

Definition at line 88 of file qtemporaryfile.h.

89  { QFile file(fileName); return createLocalFile(file); }
static QTemporaryFile * createLocalFile(const QString &fileName)
This is an overloaded member function, provided for convenience. It differs from the above function o...
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65

◆ createLocalFile() [2/2]

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.

Definition at line 718 of file qtemporaryfile.cpp.

719 {
720  if (QAbstractFileEngine *engine = file.fileEngine()) {
722  return 0; //local already
723  //cache
724  bool wasOpen = file.isOpen();
725  qint64 old_off = 0;
726  if(wasOpen)
727  old_off = file.pos();
728  else
730  //dump data
731  QTemporaryFile *ret = new QTemporaryFile;
732  ret->open();
733  file.seek(0);
734  char buffer[1024];
735  while(true) {
736  qint64 len = file.read(buffer, 1024);
737  if(len < 1)
738  break;
739  ret->write(buffer, len);
740  }
741  ret->seek(0);
742  //restore
743  if(wasOpen)
744  file.seek(old_off);
745  else
746  file.close();
747  //done
748  return ret;
749  }
750  return 0;
751 }
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
The QTemporaryFile class is an I/O device that operates on temporary files.
virtual QAbstractFileEngine * fileEngine() const
Returns the QIOEngine for this QFile object.
Definition: qfile.cpp:1965
bool open()
A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, this allows easy access to the d...
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
The QAbstractFileEngine class provides an abstraction for accessing the filesystem.
qint64 pos() const
Reimplemented Function
Definition: qfile.cpp:1720
__int64 qint64
Definition: qglobal.h:942
QTemporaryFile()
Constructs a QTemporaryFile in QDir::tempPath(), using the file template "qt_temp.XXXXXX".
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342
virtual void close()
Calls QFile::flush() and closes the file.
Definition: qfile.cpp:1680
bool seek(qint64 offset)
For random-access devices, this function sets the current position to pos, returning true on success...
Definition: qfile.cpp:1784

◆ fileEngine()

QAbstractFileEngine * QTemporaryFile::fileEngine ( ) const
virtual
Warning
This function is not part of the public interface.

Reimplemented from QFile.

Definition at line 757 of file qtemporaryfile.cpp.

Referenced by fileName(), and open().

758 {
759  Q_D(const QTemporaryFile);
760  if(!d->fileEngine) {
761  if (d->fileName.isEmpty())
762  d->fileEngine = new QTemporaryFileEngine(d->templateName);
763  else
764  d->fileEngine = new QTemporaryFileEngine(d->fileName, false);
765  }
766  return d->fileEngine;
767 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ fileName()

QString QTemporaryFile::fileName ( ) const

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()

Definition at line 663 of file qtemporaryfile.cpp.

Referenced by registerFont(), and QSystemTrayIconPrivate::showMessage_sys().

664 {
665  Q_D(const QTemporaryFile);
666  if(d->fileName.isEmpty())
667  return QString();
669 }
double d
Definition: qnumeric_p.h:62
virtual QString fileName(FileName file=DefaultName) const
Return the file engine&#39;s current file name in the format specified by file.
The QTemporaryFile class is an I/O device that operates on temporary files.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
virtual QAbstractFileEngine * fileEngine() const

◆ fileTemplate()

QString QTemporaryFile::fileTemplate ( ) const

Returns the set file template.

The default file template will be called qt_temp and be placed in QDir::tempPath().

See also
setFileTemplate()

Definition at line 677 of file qtemporaryfile.cpp.

678 {
679  Q_D(const QTemporaryFile);
680  return d->templateName;
681 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ open() [1/2]

bool QTemporaryFile::open ( )
inline

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()

Definition at line 82 of file qtemporaryfile.h.

Referenced by createLocalFile(), QConfFile::isWritable(), open(), QNetworkDiskCache::prepare(), QPdf::ByteStream::prepareBuffer(), registerFont(), QSystemTrayIconPrivate::showMessage_sys(), and QNetworkDiskCachePrivate::storeItem().

82 { return open(QIODevice::ReadWrite); }
bool open()
A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, this allows easy access to the d...

◆ open() [2/2]

bool QTemporaryFile::open ( OpenMode  flags)
protectedvirtual

Creates a unique file name for the temporary file, and opens it.

Reimplemented Function

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

Reimplemented from QFile.

Definition at line 779 of file qtemporaryfile.cpp.

780 {
782  if (!d->fileName.isEmpty()) {
783  if (static_cast<QTemporaryFileEngine*>(fileEngine())->isReallyOpen()) {
784  setOpenMode(flags);
785  return true;
786  }
787  }
788 
789  if (QFile::open(flags)) {
790  d->fileName = d->fileEngine->fileName(QAbstractFileEngine::DefaultName);
791  return true;
792  }
793  return false;
794 }
double d
Definition: qnumeric_p.h:62
void setOpenMode(OpenMode openMode)
Sets the OpenMode of the device to openMode.
Definition: qiodevice.cpp:477
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
The QTemporaryFile class is an I/O device that operates on temporary files.
#define Q_D(Class)
Definition: qglobal.h:2482
virtual QAbstractFileEngine * fileEngine() const

◆ setAutoRemove()

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(), remove()

Definition at line 648 of file qtemporaryfile.cpp.

Referenced by QNetworkDiskCachePrivate::storeItem().

649 {
651  d->autoRemove = b;
652 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setFileTemplate()

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()

Definition at line 695 of file qtemporaryfile.cpp.

Referenced by QFile::copy().

696 {
698  d->templateName = name;
699  if (d->fileEngine)
700  static_cast<QTemporaryFileEngine*>(d->fileEngine)->setFileTemplate(name);
701 }
double d
Definition: qnumeric_p.h:62
The QTemporaryFile class is an I/O device that operates on temporary files.
#define Q_D(Class)
Definition: qglobal.h:2482
const char * name
void setFileTemplate(const QString &name)
Sets the static portion of the file name to name.

Friends and Related Functions

◆ QFile

friend class QFile
friend

Definition at line 98 of file qtemporaryfile.h.


The documentation for this class was generated from the following files: