QTextObjectInterface¶
The
QTextObjectInterface
class allows drawing of custom text objects inQTextDocument
s. More…
Detailed Description¶
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 type
withQTextCharFormat
. AQTextObjectInterface
must also be implemented for this type and beregistered
with theQAbstractTextDocumentLayout
of the document. When the object type is encountered while rendering aQTextDocument
, theintrinsicSize()
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
. TheobjectType
is an integer with a value greater or equal toUserObject
.Create a
QTextCharFormat
object and set the object type to the chosen type using the setObjectType() function.Implement the
QTextObjectInterface
class.Call
registerHandler()
with an instance of yourQTextObjectInterface
subclass to register your object type.Insert
ObjectReplacementCharacter
with the aforementionedQTextCharFormat
of the chosen object type into the document. As mentioned, the functions ofQTextObjectInterface
intrinsicSize()
anddrawObject()
will then be called with theQTextFormat
as parameter whenever the replacement character is encountered.A class implementing a text object needs to inherit both
QObject
andQTextObjectInterface
.QObject
must 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
usingsetProperty()
, and then retrieved withproperty()
.Warning
Copy and Paste operations ignore custom text objects.
- class PySide2.QtGui.QTextObjectInterface¶
- PySide2.QtGui.QTextObjectInterface.drawObject(painter, rect, doc, posInDocument, format)¶
- Parameters:
painter –
PySide2.QtGui.QPainter
rect –
PySide2.QtCore.QRectF
posInDocument – int
format –
PySide2.QtGui.QTextFormat
Draws this text object using the specified
painter
.The size of the rectangle,
rect
, to draw in is the size previously calculated byintrinsicSize()
. The rectangles position is relative to thepainter
.You also get the document (
doc
) and the position (posInDocument
) of theformat
in that document.See also
- PySide2.QtGui.QTextObjectInterface.intrinsicSize(doc, posInDocument, format)¶
- Parameters:
posInDocument – int
format –
PySide2.QtGui.QTextFormat
- Return type:
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 thisformat
.See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.