QT设计重要特点:QT隐式共享(Implicit Sharing)

隐式共享是QT设计的一个重要特点。

QT隐式共享的原文是这样的(来源于https://doc.qt.io/archives/qt-4.8/implicit-sharing.html):

“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.”

“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.”

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

总结几句话就是:

1)QT里对很多C++类采用了隐式共享的方法;隐式共享保证了高效共享数据;

2)隐式共享实质就是浅拷贝;QT认为深拷贝过于耗费内存和CPU;

3)如果用隐式共享,用QSharedData and QSharedDataPointer 这两种类。

由上可以得知,所谓隐式共享,不过是浅拷贝;同时,保证了浅拷贝有引用计数,避免删除。

QSharedData和QSharedDataPointer是可以用于隐式共享的QT方法。

目前,QT里默认隐式共享的方法是:

OpenGL

QGLColormap class is used for installing custom colormaps into a QGLWidget

QBitArray

Array of bits

QBitmap

Monochrome (1-bit depth) pixmaps

QBrush

Defines the fill pattern of shapes drawn by QPainter

QByteArray

Array of bytes

QCache

Template class that provides a cache

QContiguousCache

Template class that provides a contiguous cache

QCursor

Mouse cursor with an arbitrary shape

QDir

Access to directory structures and their contents

QFileInfo

System-independent file information

QFont

Specifies a font used for drawing text

QFontInfo

General information about fonts

QFontMetrics

Font metrics information

QFontMetricsF

Font metrics information

QGradient

Used in combination with QBrush to specify gradient fills

QHash

Template class that provides a hash-table-based dictionary

QIcon

Scalable icons in different modes and states

QImage

Hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device

QKeySequence

Encapsulates a key sequence as used by shortcuts

QLinkedList

Template class that provides linked lists

QList

Template class that provides lists

QLocale

Converts between numbers and their string representations in various languages

QMap

Template class that provides a skip-list-based dictionary

QMultiHash

Convenience QHash subclass that provides multi-valued hashes

QMultiMap

Convenience QMap subclass that provides multi-valued maps

QPainterPath

Container for painting operations, enabling graphical shapes to be constructed and reused

QPalette

Contains color groups for each widget state

QPen

Defines how a QPainter should draw lines and outlines of shapes

QPicture

Paint device that records and replays QPainter commands

QPixmap

Off-screen image representation that can be used as a paint device

QPolygon

Vector of points using integer precision

QPolygonF

Vector of points using floating point precision

QQueue

Generic container that provides a queue

QRegExp

Pattern matching using regular expressions

QRegion

Specifies a clip region for a painter

QSet

Template class that provides a hash-table-based set

QSqlField

Manipulates the fields in SQL database tables and views

QSqlQuery

Means of executing and manipulating SQL statements

QSqlRecord

Encapsulates a database record

QStack

Template class that provides a stack

QString

Unicode character string

QStringList

List of strings

QTextBoundaryFinder

Way of finding Unicode text boundaries in a string

QTextCursor

Offers an API to access and modify QTextDocuments

QTextDocumentFragment

Represents a piece of formatted text from a QTextDocument

QTextFormat

Formatting information for a QTextDocument

QUrl

Convenient interface for working with URLs

QVariant

Acts like a union for the most common Qt data types

QVector

Template class that provides a dynamic array

QX11Info

Information about the X display configuration

猜你喜欢

转载自blog.csdn.net/wangzhezhilu001/article/details/105139989