QtBase  v6.3.1
Public Types | Public Member Functions | Protected Member Functions | List of all members
QFileDevice Class Reference

The QFileDevice class provides an interface for reading from and writing to open files. More...

#include <qfiledevice.h>

Inheritance diagram for QFileDevice:
Inheritance graph
[legend]
Collaboration diagram for QFileDevice:
Collaboration graph
[legend]

Public Types

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
}
 
enum  FileTime { FileAccessTime , FileBirthTime , FileMetadataChangeTime , FileModificationTime }
 
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
}
 
enum  FileHandleFlag { AutoCloseHandle = 0x0001 , DontCloseHandle = 0 }
 
enum  MemoryMapFlag { NoOptions = 0 , MapPrivateOption = 0x0001 }
 
- Public Types inherited from QIODeviceBase
enum  OpenModeFlag {
  NotOpen = 0x0000 , ReadOnly = 0x0001 , WriteOnly = 0x0002 , ReadWrite = ReadOnly | WriteOnly ,
  Append = 0x0004 , Truncate = 0x0008 , Text = 0x0010 , Unbuffered = 0x0020 ,
  NewOnly = 0x0040 , ExistingOnly = 0x0080
}
 

Public Member Functions

 ~QFileDevice ()
 
FileError error () const
 
void unsetError ()
 
void close () override
 
bool isSequential () const override
 
int handle () const
 
virtual QString fileName () const
 
qint64 pos () const override
 
bool seek (qint64 offset) override
 
bool atEnd () const override
 
bool flush ()
 
qint64 size () const override
 
virtual bool resize (qint64 sz)
 
virtual Permissions permissions () const
 
virtual bool setPermissions (Permissions permissionSpec)
 
ucharmap (qint64 offset, qint64 size, MemoryMapFlags flags=NoOptions)
 
bool unmap (uchar *address)
 
QDateTime fileTime (QFileDevice::FileTime time) const
 
bool setFileTime (const QDateTime &newDate, QFileDevice::FileTime fileTime)
 
- Public Member Functions inherited from QIODevice
 QIODevice ()
 
 QIODevice (QObject *parent)
 
virtual ~QIODevice ()
 
QIODeviceBase::OpenMode openMode () const
 
void setTextModeEnabled (bool enabled)
 
bool isTextModeEnabled () const
 
bool isOpen () const
 
bool isReadable () const
 
bool isWritable () const
 
int readChannelCount () const
 
int writeChannelCount () const
 
int currentReadChannel () const
 
void setCurrentReadChannel (int channel)
 
int currentWriteChannel () const
 
void setCurrentWriteChannel (int channel)
 
virtual bool open (QIODeviceBase::OpenMode mode)
 
virtual bool reset ()
 
virtual qint64 bytesAvailable () const
 
virtual qint64 bytesToWrite () const
 
qint64 read (char *data, qint64 maxlen)
 
QByteArray read (qint64 maxlen)
 
QByteArray readAll ()
 
qint64 readLine (char *data, qint64 maxlen)
 
QByteArray readLine (qint64 maxlen=0)
 
virtual bool canReadLine () const
 
void startTransaction ()
 
void commitTransaction ()
 
void rollbackTransaction ()
 
bool isTransactionStarted () const
 
qint64 write (const char *data, qint64 len)
 
qint64 write (const char *data)
 
qint64 write (const QByteArray &data)
 
qint64 peek (char *data, qint64 maxlen)
 
QByteArray peek (qint64 maxlen)
 
qint64 skip (qint64 maxSize)
 
virtual bool waitForReadyRead (int msecs)
 
virtual bool waitForBytesWritten (int msecs)
 
void ungetChar (char c)
 
bool putChar (char c)
 
bool getChar (char *c)
 
QString errorString () const
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 
virtual ~QObject ()
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const
 
void setObjectName (const QString &name)
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 
bool isWindowType () const
 
bool signalsBlocked () const noexcept
 
bool blockSignals (bool b) noexcept
 
QThreadthread () const
 
void moveToThread (QThread *thread)
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 
template<typename T >
T findChild (const QString &aName=QString(), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (const QString &aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<typename T >
QList< TfindChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
const QObjectListchildren () const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 
void dumpObjectInfo () const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 
bool inherits (const char *classname) const
 

Protected Member Functions

 QFileDevice ()
 
 QFileDevice (QObject *parent)
 
 QFileDevice (QFileDevicePrivate &dd, QObject *parent=nullptr)
 
qint64 readData (char *data, qint64 maxlen) override
 
qint64 writeData (const char *data, qint64 len) override
 
qint64 readLineData (char *data, qint64 maxlen) override
 
- Protected Member Functions inherited from QIODevice
 QIODevice (QIODevicePrivate &dd, QObject *parent=nullptr)
 
virtual qint64 skipData (qint64 maxSize)
 
void setOpenMode (QIODeviceBase::OpenMode openMode)
 
void setErrorString (const QString &errorString)
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Member Functions inherited from QIODeviceBase
 ~QIODeviceBase ()=default
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 
- Signals inherited from QIODevice
void readyRead ()
 
void channelReadyRead (int channel)
 
void bytesWritten (qint64 bytes)
 
void channelBytesWritten (int channel, qint64 bytes)
 
void aboutToClose ()
 
void readChannelFinished ()
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
static QMetaObject::Connection ::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 slot)
 
template<typename Func1 , typename Func2 >
static std::enable_if< QtPrivate::FunctionPointer< Func2 >::ArgumentCount==-1 &&!std::is_convertible_v< Func2, const char * >, QMetaObject::Connection >::type connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Properties inherited from QObject
QString objectName
 the name of this object More...
 

Detailed Description

The QFileDevice class provides an interface for reading from and writing to open files.

\inmodule QtCore

Since
5.0

\reentrant

QFileDevice is the base class for I/O devices that can read and write text and binary files and \l{The Qt Resource System}{resources}. QFile offers the main functionality, QFileDevice serves as a base class for sharing functionality with other file devices such as QTemporaryFile, by providing all the operations that can be done on files that have been opened by QFile or QTemporaryFile.

See also
QFile, QTemporaryFile

Definition at line 51 of file qfiledevice.h.

Member Enumeration Documentation

◆ FileError

This enum describes the errors that may be returned by the error() function.

\value NoError No error occurred. \value ReadError An error occurred when reading from the file. \value WriteError An error occurred when writing to the file. \value FatalError A fatal error occurred. \value ResourceError Out of resources (e.g., too many open files, out of memory, etc.) \value OpenError The file could not be opened. \value AbortError The operation was aborted. \value TimeOutError A timeout occurred. \value UnspecifiedError An unspecified error occurred. \value RemoveError The file could not be removed. \value RenameError The file could not be renamed. \value PositionError The position in the file could not be changed. \value ResizeError The file could not be resized. \value PermissionsError The file could not be accessed. \value CopyError The file could not be copied.

Enumerator
NoError 
ReadError 
WriteError 
FatalError 
ResourceError 
OpenError 
AbortError 
TimeOutError 
UnspecifiedError 
RemoveError 
RenameError 
PositionError 
ResizeError 
PermissionsError 
CopyError 

Definition at line 59 of file qfiledevice.h.

◆ FileHandleFlag

This enum is used when opening a file to specify additional options which only apply to files and not to a generic QIODevice.

\value AutoCloseHandle The file handle passed into open() should be closed by close(), the default behavior is that close just flushes the file and the application is responsible for closing the file handle. When opening a file by name, this flag is ignored as Qt always owns the file handle and must close it. \value DontCloseHandle If not explicitly closed, the underlying file handle is left open when the QFile object is destroyed.

Enumerator
AutoCloseHandle 
DontCloseHandle 

Definition at line 92 of file qfiledevice.h.

◆ FileTime

Since
5.10

This enum is used by the fileTime() and setFileTime() functions.

\value FileAccessTime When the file was most recently accessed (e.g. read or written to). \value FileBirthTime When the file was created (may not be not supported on UNIX). \value FileMetadataChangeTime When the file's metadata was last changed. \value FileModificationTime When the file was most recently modified.

See also
setFileTime(), fileTime(), QFileInfo::fileTime()
Enumerator
FileAccessTime 
FileBirthTime 
FileMetadataChangeTime 
FileModificationTime 

Definition at line 77 of file qfiledevice.h.

◆ MemoryMapFlag

Since
4.4

This enum describes special options that may be used by the map() function.

\value NoOptions No options. \value MapPrivateOption The mapped memory will be private, so any modifications will not be visible to other processes and will not be written to disk. Any such modifications will be lost when the memory is unmapped. It is unspecified whether modifications made to the file made after the mapping is created will be visible through the mapped memory. This enum value was introduced in Qt 5.4.

Enumerator
NoOptions 
MapPrivateOption 

Definition at line 121 of file qfiledevice.h.

◆ Permission

This enum is used by the permission() function to report the permissions and ownership of a file. The values may be OR-ed together to test multiple permissions and ownership values.

\value ReadOwner The file is readable by the owner of the file. \value WriteOwner The file is writable by the owner of the file. \value ExeOwner The file is executable by the owner of the file. \value ReadUser The file is readable by the user. \value WriteUser The file is writable by the user. \value ExeUser The file is executable by the user. \value ReadGroup The file is readable by the group. \value WriteGroup The file is writable by the group. \value ExeGroup The file is executable by the group. \value ReadOther The file is readable by anyone. \value WriteOther The file is writable by anyone. \value ExeOther The file is executable by anyone.

Warning
Because of differences in the platforms supported by Qt, the semantics of ReadUser, WriteUser and ExeUser are platform-dependent: On Unix, the rights of the owner of the file are returned and on Windows the rights of the current user are returned. This behavior might change in a future Qt version.
Note
On NTFS file systems, ownership and permissions checking is disabled by default for performance reasons. To enable it, include the following line:

Permission checking is then turned on and off by incrementing and decrementing qt_ntfs_permission_lookup by 1.

Enumerator
ReadOwner 
WriteOwner 
ExeOwner 
ReadUser 
WriteUser 
ExeUser 
ReadGroup 
WriteGroup 
ExeGroup 
ReadOther 
WriteOther 
ExeOther 

Definition at line 84 of file qfiledevice.h.

Constructor & Destructor Documentation

◆ ~QFileDevice()

QFileDevice::~QFileDevice ( )

Destroys the file device, closing it if necessary.

Definition at line 224 of file qfiledevice.cpp.

Here is the call graph for this function:

◆ QFileDevice() [1/3]

QFileDevice::QFileDevice ( )
protected

Definition at line 201 of file qfiledevice.cpp.

◆ QFileDevice() [2/3]

QFileDevice::QFileDevice ( QObject parent)
explicitprotected

Definition at line 208 of file qfiledevice.cpp.

◆ QFileDevice() [3/3]

QFileDevice::QFileDevice ( QFileDevicePrivate dd,
QObject parent = nullptr 
)
protected

Definition at line 215 of file qfiledevice.cpp.

Member Function Documentation

◆ atEnd()

bool QFileDevice::atEnd ( ) const
overridevirtual

Returns true if the end of the file has been reached; otherwise returns false.

For regular empty files on Unix (e.g. those in /proc), this function returns true, since the file system reports that the size of such a file is 0. Therefore, you should not depend on atEnd() when reading data from such a file, but rather call read() until no more data can be read.

Reimplemented from QIODevice.

Definition at line 353 of file qfiledevice.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ close()

void QFileDevice::close ( )
overridevirtual

Calls QFileDevice::flush() and closes the file. Errors from flush are ignored.

See also
QIODevice::close()

Reimplemented from QIODevice.

Definition at line 314 of file qfiledevice.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ error()

QFileDevice::FileError QFileDevice::error ( ) const

Returns the file error status.

The I/O device status returns an error code. For example, if open() returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.

See also
unsetError()

Definition at line 574 of file qfiledevice.cpp.

Here is the caller graph for this function:

◆ fileName()

QString QFileDevice::fileName ( ) const
virtual

Returns the name of the file. The default implementation in QFileDevice returns a null string.

Reimplemented in QTemporaryFile, QSaveFile, and QFile.

Definition at line 268 of file qfiledevice.cpp.

◆ fileTime()

QDateTime QFileDevice::fileTime ( QFileDevice::FileTime  time) const
Since
5.10 Returns the file time specified by time. If the time cannot be determined return QDateTime() (an invalid date time).
See also
setFileTime(), FileTime, QDateTime::isValid()

Definition at line 771 of file qfiledevice.cpp.

◆ flush()

bool QFileDevice::flush ( )

Flushes any buffered data to the file. Returns true if successful; otherwise returns false.

Definition at line 277 of file qfiledevice.cpp.

Here is the caller graph for this function:

◆ handle()

int QFileDevice::handle ( ) const

Returns the file handle of the file.

This is a small positive integer, suitable for use with C library functions such as fdopen() and fcntl(). On systems that use file descriptors for sockets (i.e. Unix systems, but not Windows) the handle can be used with QSocketNotifier as well.

If the file is not open, or there is an error, handle() returns -1.

See also
QSocketNotifier

Definition at line 255 of file qfiledevice.cpp.

Here is the call graph for this function:

◆ isSequential()

bool QFileDevice::isSequential ( ) const
overridevirtual

Returns true if the file can only be manipulated sequentially; otherwise returns false.

Most files support random-access, but some special files may not.

See also
QIODevice::isSequential()

Reimplemented from QIODevice.

Reimplemented in QSequentialFile.

Definition at line 237 of file qfiledevice.cpp.

Here is the caller graph for this function:

◆ map()

uchar * QFileDevice::map ( qint64  offset,
qint64  size,
MemoryMapFlags  flags = NoOptions 
)

Maps size bytes of the file into memory starting at offset. A file should be open for a map to succeed but the file does not need to stay open after the memory has been mapped. When the QFile is destroyed or a new file is opened with this object, any maps that have not been unmapped will automatically be unmapped.

The mapping will have the same open mode as the file (read and/or write), except when using MapPrivateOption, in which case it is always possible to write to the mapped memory.

Any mapping options can be passed through flags.

Returns a pointer to the memory or \nullptr if there is an error.

See also
unmap()

Definition at line 702 of file qfiledevice.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ permissions()

QFile::Permissions QFileDevice::permissions ( ) const
virtual

Returns the complete OR-ed together combination of QFile::Permission for the file.

See also
setPermissions()

Reimplemented in QFile.

Definition at line 641 of file qfiledevice.cpp.

Here is the caller graph for this function:

◆ pos()

qint64 QFileDevice::pos ( ) const
overridevirtual

\reimp

Reimplemented from QIODevice.

Definition at line 339 of file qfiledevice.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readData()

qint64 QFileDevice::readData ( char *  data,
qint64  len 
)
overrideprotectedvirtual

\reimp

Implements QIODevice.

Definition at line 449 of file qfiledevice.cpp.

Here is the call graph for this function:

◆ readLineData()

qint64 QFileDevice::readLineData ( char *  data,
qint64  maxlen 
)
overrideprotectedvirtual

\reimp

Reimplemented from QIODevice.

Definition at line 423 of file qfiledevice.cpp.

Here is the call graph for this function:

◆ resize()

bool QFileDevice::resize ( qint64  sz)
virtual

Sets the file size (in bytes) sz. Returns true if the resize succeeds; false otherwise. If sz is larger than the file currently is, the new bytes will be set to 0; if sz is smaller, the file is simply truncated.

Warning
This function can fail if the file doesn't exist.
See also
size()

Reimplemented in QFile.

Definition at line 617 of file qfiledevice.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ seek()

bool QFileDevice::seek ( qint64  pos)
overridevirtual

For random-access devices, this function sets the current position to pos, returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false.

Seeking beyond the end of a file: If the position is beyond the end of a file, then seek() will not immediately extend the file. If a write is performed at this position, then the file will be extended. The content of the file between the previous end of file and the newly written data is UNDEFINED and varies between platforms and file systems.

Reimplemented from QIODevice.

Definition at line 398 of file qfiledevice.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setFileTime()

bool QFileDevice::setFileTime ( const QDateTime newDate,
QFileDevice::FileTime  fileTime 
)
Since
5.10 Sets the file time specified by fileTime to newDate, returning true if successful; otherwise returns false.
Note
The file must be open to use this function.
See also
fileTime(), FileTime

Definition at line 790 of file qfiledevice.cpp.

◆ setPermissions()

bool QFileDevice::setPermissions ( Permissions  permissions)
virtual

Sets the permissions for the file to the permissions specified. Returns true if successful, or false if the permissions cannot be modified.

Warning
This function does not manipulate ACLs, which may limit its effectiveness.
See also
permissions()

Reimplemented in QFile.

Definition at line 658 of file qfiledevice.cpp.

Here is the caller graph for this function:

◆ size()

qint64 QFileDevice::size ( ) const
overridevirtual

Returns the size of the file.

For regular empty files on Unix (e.g. those in /proc), this function returns 0; the contents of such a file are generated on demand in response to you calling read().

Reimplemented from QIODevice.

Definition at line 598 of file qfiledevice.cpp.

Here is the caller graph for this function:

◆ unmap()

bool QFileDevice::unmap ( uchar address)

Unmaps the memory address.

Returns true if the unmap succeeds; false otherwise.

See also
map()

Definition at line 723 of file qfiledevice.cpp.

Here is the call graph for this function:

◆ unsetError()

void QFileDevice::unsetError ( )

Sets the file's error to QFileDevice::NoError.

See also
error()

Definition at line 585 of file qfiledevice.cpp.

Here is the caller graph for this function:

◆ writeData()

qint64 QFileDevice::writeData ( const char *  data,
qint64  len 
)
overrideprotectedvirtual

\reimp

Implements QIODevice.

Reimplemented in QSaveFile.

Definition at line 534 of file qfiledevice.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

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