QTextObjectInterface 类

The QTextObjectInterface class allows drawing of custom text objects in QTextDocuments. 更多...

头文件 #include <QTextObjectInterface>
CMakefind_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmakeQT += gui

公共函数

虚拟~QTextObjectInterface()
虚拟 voiddrawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0
虚拟 QSizeFintrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0

详细说明

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 with QTextCharFormat. A QTextObjectInterface must also be implemented for this type and be registered with the QAbstractTextDocumentLayout of the document. When the object type is encountered while rendering a QTextDocument, the intrinsicSize() and drawObject() functions of the interface are called.

The following list explains the required steps of inserting a custom text object into a document

实现文本对象的类需要继承QObject和QTextObjectInterface。其中,QObject必须是继承的第一个类。例如

class SvgTextObject : public QObject, public QTextObjectInterface
{
    Q_OBJECT
    Q_INTERFACES(QTextObjectInterface)

文本对象的数据通常使用QTextCharFormatQTextCharFormat::setProperty()存储,然后通过QTextCharFormat::property()检索。

警告:复制和粘贴操作会忽略自定义文本对象。

另请参阅:QTextCharFormatQTextLayout

成员函数文档

[virtual noexcept] QTextObjectInterface::~QTextObjectInterface()

销毁此QTextObjectInterface

[pure virtual] void QTextObjectInterface::drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)

使用指定的painter绘制此文本对象。

要绘制的矩形的尺寸rect是使用intrinsicSize()预先计算的尺寸。矩形的定位相对于painter

您还可以获得format所在的文档doc和位置posInDocument

另请参阅:intrinsicSize

[pure virtual] QSizeF QTextObjectInterface::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)

intrinsicSize()函数返回代表format的文本对象在给定文档doc中给定位置posInDocument的尺寸。

计算出的尺寸将被用于后续对drawObject()对此format的调用。

另请参阅:drawObject

© 2024 Qt公司所有。本文件中包含的文档贡献的版权归各自的所有者所有。提供的文档是根据Free Software Foundation发布的GNU自由文档许可协议版本1.3的条款许可的。Qt及其相关标志是The Qt Company Ltd.在芬兰及其他世界各地的商标。所有其他商标均为其各自所有者的财产。