CopperSpice API  2.0.0
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 &fileName)
 
 QTemporaryFile (const QString &fileName, 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 &fileName)
 
- 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)
 
QFileDevice::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 (QFileDevice::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
 
QDateTime fileTime (QFileDevice::FileTimeType type) 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
 
bool setFileTime (const QDateTime &newTime, QFileDevice::FileTimeType type)
 
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 QFileDevice::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, QFileDevice::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  FileTimeType
 
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 file name. 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 (tmpFile.open()) {
// call fileName() to return the file name
}

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

A temporary file will have a file name based on the value of QCoreApplication::applicationName() and an auto generated part. If the application name was not set the base file will be cs_temp.

For more information about the auto generated part refer to setFileTemplate().

Constructors

By default, the file is stored in the system temporary directory which can be obtained by calling QDir::tempPath().

If the constructor which accepts a file name is called and no path is provided, the file will be placed in the current working directory. If a path is supplied the file will be located specified directory.

See also
QDir::tempPath(), QFile

Constructor & Destructor Documentation

QTemporaryFile::QTemporaryFile ( )

Constructs a QTemporaryFile based on the template name. When this temporary file is opened the template name will be used to create a unique file name.

The template name is based on the application name, which is returned by calling QCoreApplication::applicationName(). If the application name is empty the template name is defaulted to cs_temp.

The file will be stored in the system's temporary directory. This location can be obtained by calling QDir::tempPath().

See also
open()
QTemporaryFile::QTemporaryFile ( const QString fileName)
explicit

Constructs a QTemporaryFile based on the given fileName. The name of the temporary file will be generated based on the template name. This name is a combination of a path and a file name. The file name is the portion of the name after the last directory path separator.

If the template name is a relative path, the path will be relative to the current working directory.

If fileName contains "XXXXXX" (six X characters in a row) the X's will be replaced by random letters to create a unique file name. If fileName does not contain "XXXXXX" then six random letters will be appended as the file extension.

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

Constructs a QTemporaryFile with the given parent. When the temporary file is opened the template name will be used to create a unique file name.

The template name is based on the application name, which is returned by calling QCoreApplication::applicationName(). If the application name is empty the template name is defaulted to cs_temp.

The file will be stored in the system's temporary directory. This location can be obtained by calling QDir::tempPath().

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

Constructs a QTemporaryFile with using fileName and parent. When the temporary file is opened the template name will be used to create a unique file name.

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

Destroys the temporary file object. The file is automatically closed if necessary and when AutoRemove mode is enabled the file will be 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 file 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 true by default.

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

Opens the existing file specified by fileName. Then calls the other version of createNativeFile() which accepts a QFile.

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 file name. The return value will be empty before the open() method is called.

See also
fileTemplate()

Reimplemented from QFile::fileName()

QString QTemporaryFile::fileTemplate ( ) const

Returns the current template file name.

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

A QTemporaryFile is opened in QIODevice::ReadWrite mode which allows reading and writing to the given file. This method will return true upon success. Once the file is open, calling the method fileName() will return the unique temporary file name.

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

Creates a unique file name for this QTemporaryFile and opens it using the given flags. The unique file name is obtained by calling fileName(). The file is guaranteed to be a new file.

Since this overloaded method is protected, it can only called from an inherited class.

Reimplemented from QFile::open()

void QTemporaryFile::setAutoRemove ( bool  b)

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

See also
autoRemove(), remove()
void QTemporaryFile::setFileTemplate ( const QString fileName)

Sets the internal template name to fileName. The name of the temporary file will be generated based on the template name. This name is a combination of a path and a file name. The file name is the portion of the name after the last directory path separator.

If the template name is a relative path, the path will be relative to the current working directory.

If fileName contains "XXXXXX" (six X characters in a row) the X's will be replaced by random letters to create a unique file name. If fileName does not contain "XXXXXX" then six random letters will be appended as the file extension.

To create a temporary file with no extension, the six X's must be included in the fileName as shown below.

tmpFile.setFileTemplate(QDir::tempPath() + "myFileName_XXXXXX");
See also
fileTemplate()