CopperSpice API  1.9.1
QTemporaryFile Class Reference

I/O device which operates on temporary files. More...

Inheritance diagram for QTemporaryFile:
QFile QFileDevice QIODevice QObject

Public Methods

 QTemporaryFile ()
 
 QTemporaryFile (const QString &tempPath)
 
 QTemporaryFile (const QString &tempPath, QObject *parent)
 
 QTemporaryFile (QObject *parent)
 
 ~QTemporaryFile ()
 
bool autoRemove () const
 
QString fileName () const override
 
QString fileTemplate () const
 
bool open ()
 
void setAutoRemove (bool b)
 
void setFileTemplate (const QString &name)
 
- Public Methods inherited from QFile
 QFile ()
 
 QFile (const QString &name)
 
 QFile (const QString &name, QObject *parent)
 
 QFile (QObject *parent)
 
 ~QFile ()
 
bool copy (const QString &newName)
 
bool exists () const
 
bool link (const QString &newName)
 
bool open (FILE *fHandle, OpenMode mode, FileHandleFlags handleFlags=DontCloseHandle)
 
bool open (int fd, OpenMode mode, FileHandleFlags handleFlags=DontCloseHandle)
 
Permissions permissions () const override
 
QString readLink () const
 
bool remove ()
 
bool rename (const QString &newName)
 
bool resize (qint64 sz) override
 
void setFileName (const QString &name)
 
bool setPermissions (Permissions permissions) override
 
qint64 size () const override
 
QString symLinkTarget () const
 
- Public Methods inherited from QFileDevice
 ~QFileDevice ()
 
bool atEnd () const override
 
void close () override
 
FileError error () const
 
bool flush ()
 
int handle () const
 
bool isSequential () const override
 
ucharmap (qint64 offset, qint64 size, MemoryMapFlags flags=NoOptions)
 
qint64 pos () const override
 
bool seek (qint64 pos) override
 
qint64 size () const override
 
bool unmap (uchar *address)
 
void unsetError ()
 
- Public Methods inherited from QIODevice
 QIODevice ()
 
 QIODevice (QObject *parent)
 
virtual ~QIODevice ()
 
virtual qint64 bytesAvailable () const
 
virtual qint64 bytesToWrite () const
 
virtual bool canReadLine () const
 
QString errorString () const
 
bool getChar (char *c)
 
bool isOpen () const
 
bool isReadable () const
 
bool isTextModeEnabled () const
 
bool isWritable () const
 
OpenMode openMode () const
 
qint64 peek (char *data, qint64 maxSize)
 
QByteArray peek (qint64 maxSize)
 
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 ()
 
void setTextModeEnabled (bool enabled)
 
void ungetChar (char c)
 
virtual bool waitForBytesWritten (int msecs)
 
virtual bool waitForReadyRead (int msecs)
 
qint64 write (const char *data)
 
qint64 write (const char *data, qint64 maxSize)
 
qint64 write (const QByteArray &data)
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Static Public Methods

static QTemporaryFile * createNativeFile (const QString &fileName)
 
static QTemporaryFile * createNativeFile (QFile &file)
 
- Static Public Methods inherited from QFile
static bool copy (const QString &oldName, const QString &newName)
 
static QString decodeName (const char *localFileName)
 
static QString decodeName (const QByteArray &localFileName)
 
static QByteArray encodeName (const QString &fileName)
 
static bool exists (const QString &fileName)
 
static bool link (const QString &oldName, const QString &newName)
 
static Permissions permissions (const QString &fileName)
 
static QString readLink (const QString &fileName)
 
static bool remove (const QString &fileName)
 
static bool rename (const QString &oldName, const QString &newName)
 
static bool resize (const QString &filename, qint64 sz)
 
static void setDecodingFunction (DecoderFn function)
 
static void setEncodingFunction (EncoderFn function)
 
static bool setPermissions (const QString &fileName, Permissions permissions)
 
static QString symLinkTarget (const QString &fileName)
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Protected Methods

bool open (OpenMode flags) override
 
- Protected Methods inherited from QFileDevice
qint64 readData (char *data, qint64 maxSize) override
 
qint64 readLineData (char *data, qint64 maxSize) override
 
qint64 writeData (const char *data, qint64 maxSize) override
 
- Protected Methods inherited from QIODevice
void setErrorString (const QString &errorString)
 
void setOpenMode (OpenMode openMode)
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Friends

class QFile
 

Additional Inherited Members

- Public Typedefs inherited from QFile
using DecoderFn = QString (*)(const QByteArray &localfileName)
 
using EncoderFn = QByteArray (*)(const QString &fileName)
 
- Public Typedefs inherited from QFileDevice
using FileHandleFlags = QFlags< FileHandleFlag >
 
using Permissions = QFlags< Permission >
 
- Public Typedefs inherited from QIODevice
using OpenMode = QFlags< OpenModeFlag >
 
- Public Types inherited from QFileDevice
enum  FileError
 
enum  FileHandleFlag
 
enum  MemoryMapFlags
 
enum  Permission
 
- Public Types inherited from QIODevice
enum  OpenModeFlag
 
- Public Signals inherited from QIODevice
void aboutToClose ()
 
void bytesWritten (qint64 bytes)
 
void readChannelFinished ()
 
void readyRead ()
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 
- Public Slots inherited from QObject
void deleteLater ()
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The QTemporaryFile class is an I/O device which operates on temporary files. The class is used to create unique temporary files. The file is not actually created until the open() method is called. The file must be opened before retrieving the filename. By default, the file will be removed when the QTemporaryFile object is destroyed.

The name of the temporary file is guaranteed to be unique and will not overwrite an existing file. The desired file name can be passed to the constructor or one will be auto generated.

if (file.open()) {
// call file.fileName() to return the file name
}

Reopening a QTemporaryFile after calling QFileDevice::close() is supported, as long as the QTemporaryFile object itself is not destroyed.

A temporary file will have a filename with a fixed part determined from QCoreApplication::applicationName() and an auto generated part. If the application name was not set the base file will be cs_temp. The auto generated part consists of six upper case X characters and will be appended to the file name. These X characters will be replaced by a random sequence of letters when the file is opened.

As an alternative, a filename can be specified and passed directly to the constructor. You can include the X's or the open() method will add them for you.

The file is stored in the system temporary directory, QDir::tempPath(). If you specify your own filename a relative file path will not be placed in the temporary directory. It will be relative to the current working directory.

See also
QDir::tempPath(), QFile

Constructor & Destructor Documentation

QTemporaryFile::QTemporaryFile ( )

Constructs a QTemporaryFile based on the application name which is returned by QCoreApplication::applicationName() followed by ".XXXXXX". If the application name is not set the file will be cs_temp.XXXXXX.

The file is stored in the system temporary directory, QDir::tempPath().

See also
setFileTemplate(), QDir::tempPath()
QTemporaryFile::QTemporaryFile ( const QString tempPath)
explicit

Constructs a QTemporaryFile with a filename of tempPath. When open() is called the tempPath will be used to create a unique filename. If the tempPath does not contain XXXXXX these characters it will be appended.

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

See also
open(), fileTemplate()
QTemporaryFile::QTemporaryFile ( QObject parent)
explicit

Constructs a QTemporaryFile with the given parent. The file will be stored in the system temporary directory, defined by QDir::tempPath().

The file name is based on the application name which is returned by QCoreApplication::applicationName() followed by ".XXXXXX". If the application name is not set the file will be cs_temp.XXXXXX.

See also
setFileTemplate()
QTemporaryFile::QTemporaryFile ( const QString tempPath,
QObject parent 
)

Constructs a QTemporaryFile with a filename of tempPath and the specified parent. When open() is called the tempPath will be used to create a unique filename. If the tempPath does not contain XXXXXX characters they will be appended.

If tempPath is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath() to construct tempPath to use the system temporary directory.

See also
open(), fileTemplate()
QTemporaryFile::~QTemporaryFile ( )

Destroys the temporary file object. The file is automatically closed if necessary and when AutoRemove mode is enabled the file will be automatically deleted

See also
autoRemove()

Method Documentation

bool QTemporaryFile::autoRemove ( ) const

Returns true if the QTemporaryFile is in AutoRemove mode. AutoRemove 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.

AutoRemove is on by default.

See also
setAutoRemove(), remove()
QTemporaryFile * QTemporaryFile::createNativeFile ( const QString fileName)
inlinestatic

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

QTemporaryFile * QTemporaryFile::createNativeFile ( 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 nullptr is returned.

QFile f(":/resources/file.txt");
QTemporaryFile::createNativeFile(f); // Returns a pointer to a temporary file
QFile f("/users/fred/file.txt");
QTemporaryFile::createNativeFile(f); // Returns nullptr
QString QTemporaryFile::fileName ( ) const
overridevirtual

Returns the complete filename which was opened by the QTemporaryFile object. This string will be empty before the open() methods is called.

See also
fileTemplate()

Reimplemented from QFile::fileName()

QString QTemporaryFile::fileTemplate ( ) const

Returns the current template file name. The default base file name is cs_temp.XXXXXX and is located in QDir::tempPath().

See also
setFileTemplate()
bool QTemporaryFile::open ( )
inline

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

See also
fileName()
bool QTemporaryFile::open ( OpenMode  flags)
overrideprotectedvirtual

Creates a unique file name for the temporary file and opens it. The unique name can be retrieved by calling fileName(). The file is guaranteed to be a new file.

Reimplemented from QFile::open()

void QTemporaryFile::setAutoRemove ( bool  b)

Sets the QTemporaryFile into AutoRemove mode if b true. AutoRemove is on by default.

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