![]() |
Home · All Classes · Main Classes · Grouped Classes · Modules · Functions | ![]() |
Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying of data. The only important effect is that the classes listed here can be passed around as arguments efficiently, even though they may seem heavyweight.
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 takes place behind the scenes; the programmer does not need to worry about it. Even in multithreaded applications, implicit sharing takes place, as explained in Threads and Implicit Sharing.
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 total control of its internal data. In any member functions that modify its data, it automatically detaches before modifying the data.
The QPen class, which uses implicit sharing, detaches from the shared data in all member functions that change the internal data.
Code fragment:
void QPen::setStyle(Qt::PenStyle style) { detach(); // detach from common data d->style = style; // set the style member } void QPen::detach() { if (d->ref != 1) { ... // perform a deep copy } }
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:
QPixmap p1, p2; p1.load("image.bmp"); p2 = p1; // p1 and p2 share data QPainter paint; 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: Do not copy an implicitly shared container (QMap, QVector, etc.) while you are iterating over it using an non-const STL-style iterator.
QBitArray | The QBitArray class provides an array of bits. |
---|---|
QBitmap | The QBitmap class provides monochrome (1-bit depth) pixmaps. |
QBrush | The QBrush class defines the fill pattern of shapes drawn by QPainter. |
QByteArray | The QByteArray class provides an array of bytes. |
QCache | The QCache class is a template class that provides a cache. |
QCursor | The QCursor class provides a mouse cursor with an arbitrary shape. |
QDir | The QDir class provides access to directory structures and their contents. |
QFileInfo | The QFileInfo class provides system-independent file information. |
QFont | The QFont class specifies a font used for drawing text. |
QFontInfo | The QFontInfo class provides general information about fonts. |
QFontMetrics | The QFontMetrics class provides font metrics information. |
QFontMetricsF | The QFontMetricsF class provides font metrics information. |
QGLColormap | The QGLColormap class is used for installing custom colormaps into QGLWidgets. |
QHash | The QHash class is a template class that provides a hash-table-based dictionary. |
QIcon | The QIcon class provides scalable icons in different modes and states. |
QImage | The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device. |
QKeySequence | The QKeySequence class encapsulates a key sequence as used by shortcuts. |
QLinkedList | The QLinkedList class is a template class that provides linked lists. |
QList | The QList class is a template class that provides lists. |
QLocale | The QLocale class converts between numbers and their string representations in various languages. |
QMap | The QMap class is a template class that provides a skip-list-based dictionary. |
QMultiHash | The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes. |
QMultiMap | The QMultiMap class is a convenience QMap subclass that provides multi-valued maps. |
QPalette | The QPalette class contains color groups for each widget state. |
QPen | The QPen class defines how a QPainter should draw lines and outlines of shapes. |
QPicture | The QPicture class is a paint device that records and replays QPainter commands. |
QPixmap | The QPixmap class is an off-screen image representation that can be used as a paint device. |
QPolygon | The QPolygon class provides a vector of points using integer precision. |
QPolygonF | The QPolygonF class provides a vector of points using floating point precision. |
QQueue | The QQueue class is a generic container that provides a queue. |
QRegExp | The QRegExp class provides pattern matching using regular expressions. |
QRegion | The QRegion class specifies a clip region for a painter. |
QSet | The QSet class is a template class that provides a hash-table-based set. |
QSqlField | The QSqlField class manipulates the fields in SQL database tables and views. |
QSqlQuery | The QSqlQuery class provides a means of executing and manipulating SQL statements. |
QSqlRecord | The QSqlRecord class encapsulates a database record. |
QStack | The QStack class is a template class that provides a stack. |
QString | The QString class provides a Unicode character string. |
QStringList | The QStringList class provides a list of strings. |
QTextCursor | The QTextCursor class offers an API to access and modify QTextDocuments. |
QTextDocumentFragment | The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument. |
QTextFormat | The QTextFormat class provides formatting information for a QTextDocument. |
QUrl | The QUrl class provides a convenient interface for working with URLs. |
QVariant | The QVariant class acts like a union for the most common Qt data types. |
QVector | The QVector class is a template class that provides a dynamic array. |
QX11Info | The QX11Info class provides information about the X display configuration. |
Copyright © 2007 Trolltech | Trademarks | Qt 4.3.0beta |