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 .
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 隐式共享迭代器问题 .
In multithreaded applications, implicit sharing takes place, as explained in Threads and Implicitly Shared Classes.
When implementing your own implicitly shared classes, use the
QSharedDataandQSharedDataPointer类。
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 隐式共享迭代器问题 .
QPenclass, which uses implicit sharing, detaches from the shared data in all member functions that change the internal data.代码片段:
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.
范例:
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();在此范例中,
p1andp2共享数据直到begin()is called forp2,因为描绘像素图会修改它。警告
Be careful with copying an implicitly shared container (
QMap,QVector, etc.) while you use STL 样式迭代器 。见 隐式共享迭代器问题 .
QDebugThe QDebug class provides an output stream for debugging information.
The QDir class provides access to directory structures and their contents.
QFileInfo 类提供与系统无关的文件信息。
QProcessEnvironment 类保持可以被传递给程序的环境变量。
提供有关当前挂载的存储和驱动器的信息。
QUrl 类提供操控 URL 的方便接口。
The QUrlQuery class provides a way to manipulate a key-value pairs in a URL’s query.
The QPersistentModelIndex class is used to locate data in a data model.
PySide2.QtCore.QVariantQVariant 类充当用于最常见 Qt 数据类型的 Union (并集)。
QMimeType 类描述由 MIME 类型字符串表示的文件或数据的类型。
QJsonArray 类封装 JSON 数组。
QJsonDocument 类提供读写 JSON 文档的办法。
PySide2.QtCore.QJsonObjectQJsonObject 类封装 JSON 对象。
QJsonParseError 类被用于在 JSON 剖析期间报告错误。
The QJsonValue class encapsulates a value in JSON.
QByteArray 类提供字节数组。
QByteArrayListQByteArrayList 类提供字节数组列表。
QCollator 类根据本地整理算法比较字符串。
The QCollatorSortKey class can be used to speed up string collation.
QLocale 类在各种语言的数字及其字符串表示之间转换。
QRegExp 类提供以正则表达式进行模式匹配。
QRegularExpression 类提供以正则表达式进行模式匹配。
The QRegularExpressionMatch class provides the results of a matching a QRegularExpression against a string.
The QRegularExpressionMatchIterator class provides an iterator on the results of a global match of a QRegularExpression object against a string.
PySide2.QtCore.QStringQString 类提供 Unicode 字符串。
The QStringBuilder class is a template class that provides a facility to build up QStrings and QByteArrays from smaller chunks.
PySide2.QtCore.QStringListQStringList 类提供字符串列表。
QTextBoundaryFinder 类提供在字符串中查找 Unicode 文本边界的办法。
QDateTime 类提供日期和时间功能。
The QBitArray class provides an array of bits.
QCacheQCache 类是提供缓存的模板类。
QCommandLineOption 类定义可能的命令行选项。
QContiguousCacheQContiguousCache 类是提供连续缓存的模板类。
QHashQHash 类是提供基于哈希表的字典的模板类。
QMultiHashThe QMultiHash class is a convenience QHash subclass that provides multi-valued hashes.
QLinkedListQLinkedList 类是提供链接列表的模板类。
QListQList 类是提供列表的模板类。
QMapQMap 类是提供基于红-黑-树的字典的模板类。
QMultiMapThe QMultiMap class is a convenience QMap subclass that provides multi-valued maps.
QQueueQQueue 类是提供队列的通用容器。
QSetQSet 类是提供基于哈希表的集的模板类。
QStackThe QStack class is a template class that provides a stack.
QVectorQVector 类是提供动态数组的模板类。
QBitmap 类提供单色 (1 位深度) 像素图。
QIcon 类提供在不同模式和状态下的可缩放图标。
QImage 类提供独立于硬件的图像表示 (允许直接访问像素数据,且可以被用作描绘设备)。
The QPicture class is a paint device that records and replays QPainter commands.
QPixmap 类是可以用作描绘设备的离屏图像表示。
QCursor 类提供具有任意形状的鼠标光标。
QKeySequence 类封装作为快捷键使用的键序列。
QPalette 类包含用于各 Widget 状态的颜色组。
QOpenGLDebugMessage 类包裹 OpenGL 调试消息。
The QBrush class defines the fill pattern of shapes drawn by QPainter.
The QGradient class is used in combination with QBrush to specify gradient fills.
QPainterPath 类提供用于描绘操作的容器,使图形形状能够被构造和重用。
The QPen class defines how a QPainter should draw lines and outlines of shapes.
The QPolygon class provides a vector of points using integer precision.
The QPolygonF class provides a vector of points using floating point precision.
QRegion 类为描绘器指定裁剪区域。
QFont 类指定用于绘制文本的字体查询。
The QFontInfo class provides general information about fonts.
QFontMetrics 类提供字体规格信息。
QFontMetricsF 类提供字体规格信息。
QGlyphRunQGlyphRun 类提供对字体内部字形的直接访问。
QRawFont 类 提供访问字体的单物理实例。
The QStaticText class enables optimized drawing of text when the text and its layout is updated rarely.
QTextCursor 类提供能访问和修改 QTextDocument 的 API。
The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument.
The QTextFormat class provides formatting information for a QTextDocument.
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument.
The QTextListFormat class provides formatting information for lists in a QTextDocument.
The QTextFrameFormat class provides formatting information for frames in a QTextDocument.
The QTextTableFormat class provides formatting information for tables in a QTextDocument.
The QTextImageFormat class provides formatting information for images in a QTextDocument.
The QTextTableCellFormat class provides formatting information for table cells in a QTextDocument.
The QNetworkCacheMetaData class provides cache information.
QHttp2ConfigurationThe QHttp2Configuration class controls HTTP/2 parameters and settings.
The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message.
The QNetworkCookie class holds one network cookie.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
The QNetworkConfiguration class provides an abstraction of one or more access point configurations.
QDnsDomainNameRecord 类存储关于域名记录的信息。
QDnsHostAddressRecord 类存储有关主机地址记录的信息。
QDnsMailExchangeRecord 类存储有关 DNS MX 记录的信息。
QDnsServiceRecord 类存储有关 DNS SRV 记录的信息。
QDnsTextRecord 类存储有关 DNS TXT 记录的信息。
QHostAddress 类提供 IP 地址。
QNetworkAddressEntry 类存储由网络接口支持的一个 IP 地址,及其关联的 Netmask (网络掩码) 和广播地址。
The QNetworkInterface class provides a listing of the host’s IP addresses and network interfaces.
QNetworkProxy 类提供网络层代理。
The QNetworkProxyQuery class is used to query the proxy settings for a socket.
The QSslCertificate class provides a convenient API for an X509 certificate.
The QSslCertificateExtension class provides an API for accessing the extensions of an X509 certificate.
The QSslCipher class represents an SSL cryptographic cipher.
QSslConfiguration 类保持 SSL 连接的配置和状态。
The QSslDiffieHellmanParameters class provides an interface for Diffie-Hellman parameters for servers.
The QSslError class provides an SSL error.
The QSslKey class provides an interface for private and public keys.
The QSslPreSharedKeyAuthenticator class provides authentication data for pre shared keys (PSK) ciphersuites.
The QGLColormap class is used for installing custom colormaps into a QGLWidget.
QSqlField 类操纵 SQL 数据库表和视图中的字段。
QSqlQuery 类提供执行和操纵 SQL 语句的手段。
QSqlRecord 类封装数据库记录。