QTextObjectInterfaceclass allows drawing of custom text objects inQTextDocuments. 更多 …
A text object describes the structure of one or more elements in a text document; for instance, images imported from HTML are implemented using text objects. A text object knows how to lay out and draw its elements when a document is being rendered.
Qt allows custom text objects to be inserted into a document by registering a custom
object typewithQTextCharFormat. AQTextObjectInterfacemust also be implemented for this type and beregistered采用QAbstractTextDocumentLayoutof the document. When the object type is encountered while rendering aQTextDocument,intrinsicSize()anddrawObject()functions of the interface are called.The following list explains the required steps of inserting a custom text object into a document:
Choose an
objectType。objectTypeis an integer with a value greater or equal toUserObject.创建
QTextCharFormatobject and set the object type to the chosen type using the setObjectType() function.Implement the
QTextObjectInterface类。调用
registerHandler()with an instance of yourQTextObjectInterfacesubclass to register your object type.Insert
ObjectReplacementCharacterwith the aforementionedQTextCharFormatof the chosen object type into the document. As mentioned, the functions ofQTextObjectInterfaceintrinsicSize()anddrawObject()will then be called with theQTextFormatas parameter whenever the replacement character is encountered.A class implementing a text object needs to inherit both
QObjectandQTextObjectInterface.QObjectmust be the first class inherited. For instance:class SvgTextObject : public QObject, public QTextObjectInterface { Q_OBJECT Q_INTERFACES(QTextObjectInterface)The data of a text object is usually stored in the
QTextCharFormat使用setProperty(), and then retrieved withproperty().警告
拷贝和粘贴操作忽略自定义文本对象。
QTextObjectInterface
¶
PySide2.QtGui.QTextObjectInterface.
drawObject
(
painter
,
rect
,
doc
,
posInDocument
,
format
)
¶
painter
–
QPainter
rect
–
QRectF
doc
–
QTextDocument
posInDocument
–
int
format
–
QTextFormat
绘制此文本对象使用指定
painter
.
The size of the rectangle,
rect
, to draw in is the size previously calculated by
intrinsicSize()
. The rectangles position is relative to the
painter
.
You also get the document (
doc
) and the position (
posInDocument
) of the
format
in that document.
另请参阅
PySide2.QtGui.QTextObjectInterface.
intrinsicSize
(
doc
,
posInDocument
,
format
)
¶
doc
–
QTextDocument
posInDocument
–
int
format
–
QTextFormat
QSizeF
The function returns the size of the text object represented by
format
in the given document (
doc
) at the given position (
posInDocument
).
The size calculated will be used for subsequent calls to
drawObject()
for this
format
.
另请参阅