Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Implicit Sharing#

Reference counting for fast copying.

Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e., copy-on-write.

Overview#

A shared class consists of a pointer to a shared data block that contains a reference count and the data.

When a shared object is created, it sets the reference count to 1. The reference count is incremented whenever a new object references the shared data, and decremented when the object dereferences the shared data. The shared data is deleted when the reference count becomes zero.

When dealing with shared objects, there are two ways of copying an object. We usually speak about deep and shallow copies. A deep copy implies duplicating an object. A shallow copy is a reference copy, i.e. just a pointer to a shared data block. Making a deep copy can be expensive in terms of memory and CPU. Making a shallow copy is very fast, because it only involves setting a pointer and incrementing the reference count.

Object assignment (with operator=()) for implicitly shared objects is implemented using shallow copies.

The benefit of sharing is that a program does not need to duplicate data unnecessarily, which results in lower memory use and less copying of data. Objects can easily be assigned, sent as function arguments, and returned from functions.

Implicit sharing mostly takes place behind the scenes; the programmer rarely needs to worry about it. However, Qt’s container iterators have different behavior than those from the STL. Read Implicit sharing iterator problem .

In multithreaded applications, implicit sharing takes place, as explained in Threads and Implicitly Shared Classes.

When implementing your own implicitly shared classes, use the QSharedData and QSharedDataPointer classes.

Implicit Sharing in Detail#

Implicit sharing automatically detaches the object from a shared block if the object is about to change and the reference count is greater than one. (This is often called copy-on-write or value semantics.)

An implicitly shared class has control of its internal data. In any member functions that modify its data, it automatically detaches before modifying the data. Notice, however, the special case with container iterators; see Implicit sharing iterator problem .

The QPen class, which uses implicit sharing, detaches from the shared data in all member functions that change the internal data.

Code fragment:

def setStyle(self, style):

    detach() # detach from common data
    d.style = style # set the style member

def detach(self):

    if d.ref != 1:
        ... // perform a deep copy

List of Classes#

The classes listed below automatically detach from common data if an object is about to be changed. The programmer will not even notice that the objects are shared. Thus you should treat separate instances of them as separate objects. They will always behave as separate objects but with the added benefit of sharing data whenever possible. For this reason, you can pass instances of these classes as arguments to functions by value without concern for the copying overhead.

Example:

p1, = QPixmap()
p1.load("image.bmp")
p2 = p1 # p1 and p2 share data
paint = QPainter()
paint.begin(p2) # cuts p2 loose from p1
paint.drawText(0,50, "Hi")
paint.end()

In this example, p1 and p2 share data until QPainter::begin() is called for p2, because painting a pixmap will modify it.

Warning

Be careful with copying an implicitly shared container ( QMap , QList , etc.) while you use STL-style iterator . See Implicit sharing iterator problem .

QDebug

The QDebug class provides an output stream for debugging information.

PySide6.QtCore.QDir

The QDir class provides access to directory structures and their contents.

PySide6.QtCore.QFileInfo

The QFileInfo class provides an OS-independent API to retrieve information about file system entries.

PySide6.QtCore.QProcessEnvironment

The QProcessEnvironment class holds the environment variables that can be passed to a program.

PySide6.QtCore.QStorageInfo

Provides information about currently mounted storage and drives.

PySide6.QtCore.QUrl

The QUrl class provides a convenient interface for working with URLs.

PySide6.QtCore.QUrlQuery

The QUrlQuery class provides a way to manipulate a key-value pairs in a URL’s query.

PySide6.QtCore.QPersistentModelIndex

The QPersistentModelIndex class is used to locate data in a data model.

PySide6.QtCore.QVariant

The QVariant class acts like a union for the most common Qt data types.

PySide6.QtCore.QMimeType

The QMimeType class describes types of file or data, represented by a MIME type string.

PySide6.QtCore.QJsonArray

The QJsonArray class encapsulates a JSON array.

PySide6.QtCore.QJsonDocument

The QJsonDocument class provides a way to read and write JSON documents.

PySide6.QtCore.QJsonObject

The QJsonObject class encapsulates a JSON object.

PySide6.QtCore.QJsonParseError

The QJsonParseError class is used to report errors during JSON parsing.

PySide6.QtCore.QJsonValue

The QJsonValue class encapsulates a value in JSON.

PySide6.QtCore.QByteArray

The QByteArray class provides an array of bytes.

QByteArrayList

The QByteArrayList class provides a list of byte arrays.

PySide6.QtCore.QByteArrayView

The QByteArrayView class provides a view on an array of bytes with a read-only subset of the QByteArray API.

PySide6.QtCore.QCollator

The QCollator class compares strings according to a localized collation algorithm.

PySide6.QtCore.QCollatorSortKey

The QCollatorSortKey class can be used to speed up string collation.

PySide6.QtCore.QLocale

The QLocale class converts between numbers and their string representations in various languages.

PySide6.QtCore.QRegularExpression

The QRegularExpression class provides pattern matching using regular expressions.

PySide6.QtCore.QRegularExpressionMatch

The QRegularExpressionMatch class provides the results of a matching a QRegularExpression against a string.

PySide6.QtCore.QRegularExpressionMatchIterator

The QRegularExpressionMatchIterator class provides an iterator on the results of a global match of a QRegularExpression object against a string.

PySide6.QtCore.QString

The QString class provides a Unicode character string.

PySide6.QtCore.QStringList

The QStringList class provides a list of strings.

PySide6.QtCore.QTextBoundaryFinder

The QTextBoundaryFinder class provides a way of finding Unicode text boundaries in a string.

PySide6.QtCore.QDateTime

The QDateTime class provides date and time functions.

PySide6.QtCore.QBitArray

The QBitArray class provides an array of bits.

QCache

The QCache class is a template class that provides a cache.

PySide6.QtCore.QCommandLineOption

The QCommandLineOption class defines a possible command-line option.

QContiguousCache

The QContiguousCache class is a template class that provides a contiguous cache.

QHash

The QHash class is a template class that provides a hash-table-based dictionary.

QMultiHash

The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes.

QList

The QList class is a template class that provides a dynamic array.

QMap

The QMap class is a template class that provides an associative array.

QMultiMap

The QMultiMap class is a template class that provides an associative array with multiple equivalent keys.

QQueue

The QQueue class is a generic container that provides a queue.

QSet

The QSet class is a template class that provides a hash-table-based set.

QStack

The QStack class is a template class that provides a stack.