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

QFileDevice Class

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

 #include <QFileDevice>

Inherits: QIODevice.

Inherited by: QFile.

Note: All functions in this class are reentrant.

This class was introduced in Qt 5.0.

Public Types

enum FileError { NoError, ReadError, WriteError, FatalError, ..., CopyError }
enum FileHandleFlag { AutoCloseHandle, DontCloseHandle }
flags FileHandleFlags
enum MemoryMapFlags { NoOptions }
flags OpenMode
enum OpenModeFlag { NotOpen, ReadOnly, WriteOnly, ReadWrite, ..., Unbuffered }
enum Permission { ReadOwner, WriteOwner, ExeOwner, ReadUser, ..., ExeOther }
flags Permissions

Public Functions

QIODevice()
QIODevice(QObject * parent)
~QFileDevice()
virtual ~QIODevice()
virtual bool atEnd() const
virtual qint64 bytesAvailable() const
virtual qint64 bytesToWrite() const
virtual bool canReadLine() const
virtual void close()
FileError error() const
QString errorString() const
virtual QString fileName() const
bool flush()
bool getChar(char * c)
int handle() const
bool isOpen() const
bool isReadable() const
virtual bool isSequential() const
bool isTextModeEnabled() const
bool isWritable() const
uchar * map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions)
virtual bool open(OpenMode mode)
OpenMode openMode() const
qint64 peek(char * data, qint64 maxSize)
QByteArray peek(qint64 maxSize)
virtual Permissions permissions() const
virtual qint64 pos() const
bool putChar(char c)
qint64 read(char * data, qint64 maxSize)
QByteArray read(qint64 maxSize)
QByteArray readAll()
qint64 readLine(char * data, qint64 maxSize)
QByteArray readLine(qint64 maxSize = 0)
virtual bool reset()
virtual bool resize(qint64 sz)
virtual bool seek(qint64 pos)
virtual bool setPermissions(Permissions permissions)
void setTextModeEnabled(bool enabled)
virtual qint64 size() const
void ungetChar(char c)
bool unmap(uchar * address)
void unsetError()
virtual bool waitForBytesWritten(int msecs)
virtual bool waitForReadyRead(int msecs)
qint64 write(const char * data, qint64 maxSize)
qint64 write(const char * data)
qint64 write(const QByteArray & byteArray)

Reimplemented Public Functions

virtual bool atEnd() const
virtual void close()
virtual bool isSequential() const
virtual qint64 pos() const
virtual bool seek(qint64 pos)
virtual qint64 size() const
  • 31 public functions inherited from QObject

Signals

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

Protected Functions

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

Reimplemented Protected Functions

virtual qint64 readData(char * data, qint64 len)
virtual qint64 readLineData(char * data, qint64 maxlen)
virtual qint64 writeData(const char * data, qint64 len)
  • 9 protected functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 11 static public members inherited from QObject

Detailed Description

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

QFileDevice is the base class for I/O devices that can read and write text and binary files and 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 and QTemporaryFile.

Member Type Documentation

enum QFileDevice::FileError

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

ConstantValueDescription
QFileDevice::NoError0No error occurred.
QFileDevice::ReadError1An error occurred when reading from the file.
QFileDevice::WriteError2An error occurred when writing to the file.
QFileDevice::FatalError3A fatal error occurred.
QFileDevice::ResourceError4 
QFileDevice::OpenError5The file could not be opened.
QFileDevice::AbortError6The operation was aborted.
QFileDevice::TimeOutError7A timeout occurred.
QFileDevice::UnspecifiedError8An unspecified error occurred.
QFileDevice::RemoveError9The file could not be removed.
QFileDevice::RenameError10The file could not be renamed.
QFileDevice::PositionError11The position in the file could not be changed.
QFileDevice::ResizeError12The file could not be resized.
QFileDevice::PermissionsError13The file could not be accessed.
QFileDevice::CopyError14The file could not be copied.

enum QFileDevice::FileHandleFlag
flags QFileDevice::FileHandleFlags

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

ConstantValueDescription
QFileDevice::AutoCloseHandle0x0001The 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.
QFileDevice::DontCloseHandle0If not explicitly closed, the underlying file handle is left open when the QFile object is destroyed.

The FileHandleFlags type is a typedef for QFlags<FileHandleFlag>. It stores an OR combination of FileHandleFlag values.

enum QFileDevice::MemoryMapFlags

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

ConstantValueDescription
QFileDevice::NoOptions0No options.

This enum was introduced or modified in Qt 4.4.

enum QFileDevice::Permission
flags QFileDevice::Permissions

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.

ConstantValueDescription
QFileDevice::ReadOwner0x4000The file is readable by the owner of the file.
QFileDevice::WriteOwner0x2000The file is writable by the owner of the file.
QFileDevice::ExeOwner0x1000The file is executable by the owner of the file.
QFileDevice::ReadUser0x0400The file is readable by the user.
QFileDevice::WriteUser0x0200The file is writable by the user.
QFileDevice::ExeUser0x0100The file is executable by the user.
QFileDevice::ReadGroup0x0040The file is readable by the group.
QFileDevice::WriteGroup0x0020The file is writable by the group.
QFileDevice::ExeGroup0x0010The file is executable by the group.
QFileDevice::ReadOther0x0004The file is readable by anyone.
QFileDevice::WriteOther0x0002The file is writable by anyone.
QFileDevice::ExeOther0x0001The 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 that Qt does not by default check for permissions on NTFS file systems, as this may decrease the performance of file handling considerably. It is possible to force permission checking on NTFS by including the following code in your source:

 extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;

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

 qt_ntfs_permission_lookup++; // turn checking on
 qt_ntfs_permission_lookup--; // turn it off again

The Permissions type is a typedef for QFlags<Permission>. It stores an OR combination of Permission values.

Member Function Documentation

QFileDevice::~QFileDevice()

Destroys the file device, closing it if necessary.

bool QFileDevice::atEnd() const [virtual]

Reimplemented from QIODevice::atEnd().

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.

void QFileDevice::close() [virtual]

Reimplemented from QIODevice::close().

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

See also QIODevice::close().

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

QString QFileDevice::fileName() const [virtual]

Returns the name of the file. The default implementation in QFileDevice returns QString().

bool QFileDevice::flush()

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

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.

This function is not supported on Windows CE.

See also QSocketNotifier.

bool QFileDevice::isSequential() const [virtual]

Reimplemented from QIODevice::isSequential().

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

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.

Any mapping options can be passed through flags.

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

Note: On Windows CE 5.0 the file will be closed before mapping occurs.

See also unmap().

Permissions QFileDevice::permissions() const [virtual]

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

See also setPermissions().

qint64 QFileDevice::pos() const [virtual]

Reimplemented from QIODevice::pos().

qint64 QFileDevice::readData(char * data, qint64 len) [virtual protected]

Reimplemented from QIODevice::readData().

qint64 QFileDevice::readLineData(char * data, qint64 maxlen) [virtual protected]

Reimplemented from QIODevice::readLineData().

bool QFileDevice::resize(qint64 sz) [virtual]

Sets the file size (in bytes) sz. Returns true if the file 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.

See also size().

bool QFileDevice::seek(qint64 pos) [virtual]

Reimplemented from QIODevice::seek().

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() shall not immediately extend the file. If a write is performed at this position, then the file shall 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.

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.

See also permissions().

qint64 QFileDevice::size() const [virtual]

Reimplemented from QIODevice::size().

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

bool QFileDevice::unmap(uchar * address)

Unmaps the memory address.

Returns true if the unmap succeeds; false otherwise.

See also map().

void QFileDevice::unsetError()

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

See also error().

qint64 QFileDevice::writeData(const char * data, qint64 len) [virtual protected]

Reimplemented from QIODevice::writeData().

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

Hébergement Web