|  | Home

# QtLockedFile Class Reference

The QtLockedFile class extends QFile with advisory locking functions. More...

     #include <QtLockedFile>

Inherits QFile.

- List of all members, including inherited members

## Public Types

enum | LockMode { ReadLock, WriteLock, NoLock }

## Public Functions

| QtLockedFile ()
| QtLockedFile ( const QString & name )
| ~QtLockedFile ()
bool | isLocked () const
bool | lock ( LockMode mode, bool block = true )
LockMode | lockMode () const
bool | open ( OpenMode mode )
bool | unlock ()

- 26 public functions inherited from QFile
- 33 public functions inherited from QIODevice
- 29 public functions inherited from QObject

### Additional Inherited Members

- 1 property inherited from QObject
- 1 public slot inherited from QObject
- 4 signals inherited from QIODevice
- 1 signal inherited from QObject
- 1 public type inherited from QObject
- 14 static public members inherited from QFile
- 4 static public members inherited from QObject
- 3 protected functions inherited from QFile
- 5 protected functions inherited from QIODevice
- 7 protected functions inherited from QObject
- 2 protected variables inherited from QObject

## Detailed Description

The QtLockedFile class extends QFile with advisory locking functions.

A file may be locked in read or write mode. Multiple instances of QtLockedFile, created in multiple processes running on the same machine, may have a file locked in read mode. Exactly one instance may have it locked in write mode. A read and a write lock cannot exist simultaneously on the same file.

The file locks are advisory. This means that nothing prevents another process from manipulating a locked file using QFile or file system functions offered by the OS. Serialization is only guaranteed if all processes that access the file use QLockedFile. Also, while holding a lock on a file, a process must not open the same file again (through any API), or locks can be unexpectedly lost.

The lock provided by an instance of QtLockedFile is released whenever the program terminates. This is true even when the program crashes and no destructors are called.

## Member Type Documentation

### enum QtLockedFile::LockMode

This enum describes the available lock modes.

Constant | Value | Description
--- | --- | ---
`QtLockedFile::ReadLock` | `1` | A read lock.
`QtLockedFile::WriteLock` | `2` | A write lock.
`QtLockedFile::NoLock` | `0` | Neither a read lock nor a write lock.

## Member Function Documentation

### QtLockedFile::QtLockedFile ()

Constructs an unlocked QtLockedFile object. This constructor behaves in the same way as QFile::QFile().

See also QFile::QFile().

### QtLockedFile::QtLockedFile ( const QString & name )

Constructs an unlocked QtLockedFile object with file name. This constructor behaves in the same way as QFile::QFile(const QString&).

See also QFile::QFile().

### QtLockedFile::~QtLockedFile ()

Destroys the QtLockedFile object. If any locks were held, they are released.

### bool QtLockedFile::isLocked () const

Returns true if this object has a in read or write lock; otherwise returns false.

See also lockMode().

### bool QtLockedFile::lock ( LockMode mode, bool block = true )

Obtains a lock of type mode. The file must be opened before it can be locked.

If block is true, this function will block until the lock is aquired. If block is false, this function returns false immediately if the lock cannot be aquired.

If this object already has a lock of type mode, this function returns true immediately. If this object has a lock of a different type than mode, the lock is first released and then a new lock is obtained.

This function returns true if, after it executes, the file is locked by this object, and false otherwise.

See also unlock(), isLocked(), and lockMode().

### LockMode QtLockedFile::lockMode () const

Returns the type of lock currently held by this object, or QtLockedFile::NoLock.

See also isLocked().

### bool QtLockedFile::open ( OpenMode mode )

Opens the file in OpenMode mode.

This is identical to QFile::open(), with the one exception that the Truncate mode flag is disallowed. Truncation would conflict with the advisory file locking, since the file would be modified before the write lock is obtained. If truncation is required, use resize(0) after obtaining the write lock.

Returns true if successful; otherwise false.

See also QFile::open() and QFile::resize().

### bool QtLockedFile::unlock ()

Releases a lock.

If the object has no lock, this function returns immediately.

This function returns true if, after it executes, the file is not locked by this object, and false otherwise.

See also lock(), isLocked(), and lockMode().