内容表

上一话题

帮助系统

下一话题

输入/输出和网络

隐式共享

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 .

概述

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 QSharedData and QSharedDataPointer 类。

隐式共享细节

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 隐式共享迭代器问题 .

QPen class, 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();
											

在此范例中, p1 and p2 共享数据直到 begin() is called for p2 ,因为描绘像素图会修改它。

警告

Be careful with copying an implicitly shared container ( QMap , QVector , etc.) while you use STL 样式迭代器 。见 隐式共享迭代器问题 .

QDebug

The QDebug class provides an output stream for debugging information.

PySide2.QtCore.QDir

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

PySide2.QtCore.QFileInfo

QFileInfo 类提供与系统无关的文件信息。

PySide2.QtCore.QProcessEnvironment

QProcessEnvironment 类保持可以被传递给程序的环境变量。

PySide2.QtCore.QStorageInfo

提供有关当前挂载的存储和驱动器的信息。

PySide2.QtCore.QUrl

QUrl 类提供操控 URL 的方便接口。

PySide2.QtCore.QUrlQuery

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

PySide2.QtCore.QPersistentModelIndex

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

PySide2.QtCore.QVariant

QVariant 类充当用于最常见 Qt 数据类型的 Union (并集)。

PySide2.QtCore.QMimeType

QMimeType 类描述由 MIME 类型字符串表示的文件或数据的类型。

PySide2.QtCore.QJsonArray

QJsonArray 类封装 JSON 数组。

PySide2.QtCore.QJsonDocument

QJsonDocument 类提供读写 JSON 文档的办法。

PySide2.QtCore.QJsonObject

QJsonObject 类封装 JSON 对象。

PySide2.QtCore.QJsonParseError

QJsonParseError 类被用于在 JSON 剖析期间报告错误。

PySide2.QtCore.QJsonValue

The QJsonValue class encapsulates a value in JSON.

PySide2.QtCore.QByteArray

QByteArray 类提供字节数组。

QByteArrayList

QByteArrayList 类提供字节数组列表。

PySide2.QtCore.QCollator

QCollator 类根据本地整理算法比较字符串。

PySide2.QtCore.QCollatorSortKey

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

PySide2.QtCore.QLocale

QLocale 类在各种语言的数字及其字符串表示之间转换。

PySide2.QtCore.QRegExp

QRegExp 类提供以正则表达式进行模式匹配。

PySide2.QtCore.QRegularExpression

QRegularExpression 类提供以正则表达式进行模式匹配。

PySide2.QtCore.QRegularExpressionMatch

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

PySide2.QtCore.QRegularExpressionMatchIterator

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

PySide2.QtCore.QString

QString 类提供 Unicode 字符串。

The QStringBuilder class is a template class that provides a facility to build up QStrings and QByteArrays from smaller chunks.

PySide2.QtCore.QStringList

QStringList 类提供字符串列表。

PySide2.QtCore.QTextBoundaryFinder

QTextBoundaryFinder 类提供在字符串中查找 Unicode 文本边界的办法。

PySide2.QtCore.QDateTime

QDateTime 类提供日期和时间功能。

PySide2.QtCore.QBitArray

The QBitArray class provides an array of bits.

QCache

QCache 类是提供缓存的模板类。

PySide2.QtCore.QCommandLineOption

QCommandLineOption 类定义可能的命令行选项。

QContiguousCache

QContiguousCache 类是提供连续缓存的模板类。

QHash

QHash 类是提供基于哈希表的字典的模板类。

QMultiHash

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

QLinkedList

QLinkedList 类是提供链接列表的模板类。

QList

QList 类是提供列表的模板类。

QMap

QMap 类是提供基于红-黑-树的字典的模板类。

QMultiMap

The QMultiMap class is a convenience QMap subclass that provides multi-valued maps.

QQueue

QQueue 类是提供队列的通用容器。

QSet

QSet 类是提供基于哈希表的集的模板类。

QStack

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

QVector

QVector 类是提供动态数组的模板类。

PySide2.QtGui.QBitmap

QBitmap 类提供单色 (1 位深度) 像素图。

PySide2.QtGui.QIcon

QIcon 类提供在不同模式和状态下的可缩放图标。

PySide2.QtGui.QImage

QImage 类提供独立于硬件的图像表示 (允许直接访问像素数据,且可以被用作描绘设备)。

PySide2.QtGui.QPicture

The QPicture class is a paint device that records and replays QPainter commands.

PySide2.QtGui.QPixmap

QPixmap 类是可以用作描绘设备的离屏图像表示。

PySide2.QtGui.QCursor

QCursor 类提供具有任意形状的鼠标光标。

PySide2.QtGui.QKeySequence

QKeySequence 类封装作为快捷键使用的键序列。

PySide2.QtGui.QPalette

QPalette 类包含用于各 Widget 状态的颜色组。

PySide2.QtGui.QOpenGLDebugMessage

QOpenGLDebugMessage 类包裹 OpenGL 调试消息。

PySide2.QtGui.QBrush

The QBrush class defines the fill pattern of shapes drawn by QPainter.

PySide2.QtGui.QGradient

The QGradient class is used in combination with QBrush to specify gradient fills.

PySide2.QtGui.QPainterPath

QPainterPath 类提供用于描绘操作的容器,使图形形状能够被构造和重用。

PySide2.QtGui.QPen

The QPen class defines how a QPainter should draw lines and outlines of shapes.

PySide2.QtGui.QPolygon

The QPolygon class provides a vector of points using integer precision.

PySide2.QtGui.QPolygonF

The QPolygonF class provides a vector of points using floating point precision.

PySide2.QtGui.QRegion

QRegion 类为描绘器指定裁剪区域。

PySide2.QtGui.QFont

QFont 类指定用于绘制文本的字体查询。

PySide2.QtGui.QFontInfo

The QFontInfo class provides general information about fonts.

PySide2.QtGui.QFontMetrics

QFontMetrics 类提供字体规格信息。

PySide2.QtGui.QFontMetricsF

QFontMetricsF 类提供字体规格信息。

QGlyphRun

QGlyphRun 类提供对字体内部字形的直接访问。

PySide2.QtGui.QRawFont

QRawFont 类 提供访问字体的单物理实例。

PySide2.QtGui.QStaticText

The QStaticText class enables optimized drawing of text when the text and its layout is updated rarely.

PySide2.QtGui.QTextCursor

QTextCursor 类提供能访问和修改 QTextDocument 的 API。

PySide2.QtGui.QTextDocumentFragment

The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument.

PySide2.QtGui.QTextFormat

The QTextFormat class provides formatting information for a QTextDocument.

PySide2.QtGui.QTextCharFormat

The QTextCharFormat class provides formatting information for characters in a QTextDocument.

PySide2.QtGui.QTextBlockFormat

The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument.

PySide2.QtGui.QTextListFormat

The QTextListFormat class provides formatting information for lists in a QTextDocument.

PySide2.QtGui.QTextFrameFormat

The QTextFrameFormat class provides formatting information for frames in a QTextDocument.

PySide2.QtGui.QTextTableFormat

The QTextTableFormat class provides formatting information for tables in a QTextDocument.

PySide2.QtGui.QTextImageFormat

The QTextImageFormat class provides formatting information for images in a QTextDocument.

PySide2.QtGui.QTextTableCellFormat

The QTextTableCellFormat class provides formatting information for table cells in a QTextDocument.

PySide2.QtNetwork.QNetworkCacheMetaData

The QNetworkCacheMetaData class provides cache information.

QHttp2Configuration

The QHttp2Configuration class controls HTTP/2 parameters and settings.

PySide2.QtNetwork.QHttpPart

The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message.

PySide2.QtNetwork.QNetworkCookie

The QNetworkCookie class holds one network cookie.

PySide2.QtNetwork.QNetworkRequest

The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.

PySide2.QtNetwork.QNetworkConfiguration

The QNetworkConfiguration class provides an abstraction of one or more access point configurations.

PySide2.QtNetwork.QDnsDomainNameRecord

QDnsDomainNameRecord 类存储关于域名记录的信息。

PySide2.QtNetwork.QDnsHostAddressRecord

QDnsHostAddressRecord 类存储有关主机地址记录的信息。

PySide2.QtNetwork.QDnsMailExchangeRecord

QDnsMailExchangeRecord 类存储有关 DNS MX 记录的信息。

PySide2.QtNetwork.QDnsServiceRecord

QDnsServiceRecord 类存储有关 DNS SRV 记录的信息。

PySide2.QtNetwork.QDnsTextRecord

QDnsTextRecord 类存储有关 DNS TXT 记录的信息。

PySide2.QtNetwork.QHostAddress

QHostAddress 类提供 IP 地址。

PySide2.QtNetwork.QNetworkAddressEntry

QNetworkAddressEntry 类存储由网络接口支持的一个 IP 地址,及其关联的 Netmask (网络掩码) 和广播地址。

PySide2.QtNetwork.QNetworkInterface

The QNetworkInterface class provides a listing of the host’s IP addresses and network interfaces.

PySide2.QtNetwork.QNetworkProxy

QNetworkProxy 类提供网络层代理。

PySide2.QtNetwork.QNetworkProxyQuery

The QNetworkProxyQuery class is used to query the proxy settings for a socket.

PySide2.QtNetwork.QSslCertificate

The QSslCertificate class provides a convenient API for an X509 certificate.

PySide2.QtNetwork.QSslCertificateExtension

The QSslCertificateExtension class provides an API for accessing the extensions of an X509 certificate.

PySide2.QtNetwork.QSslCipher

The QSslCipher class represents an SSL cryptographic cipher.

PySide2.QtNetwork.QSslConfiguration

QSslConfiguration 类保持 SSL 连接的配置和状态。

PySide2.QtNetwork.QSslDiffieHellmanParameters

The QSslDiffieHellmanParameters class provides an interface for Diffie-Hellman parameters for servers.

PySide2.QtNetwork.QSslError

The QSslError class provides an SSL error.

PySide2.QtNetwork.QSslKey

The QSslKey class provides an interface for private and public keys.

PySide2.QtNetwork.QSslPreSharedKeyAuthenticator

The QSslPreSharedKeyAuthenticator class provides authentication data for pre shared keys (PSK) ciphersuites.

PySide2.QtOpenGL.QGLColormap

The QGLColormap class is used for installing custom colormaps into a QGLWidget.

PySide2.QtSql.QSqlField

QSqlField 类操纵 SQL 数据库表和视图中的字段。

PySide2.QtSql.QSqlQuery

QSqlQuery 类提供执行和操纵 SQL 语句的手段。

PySide2.QtSql.QSqlRecord

QSqlRecord 类封装数据库记录。