QTextObjectInterface Class

QTextObjectInterface 类允许在QTextDocuments 中绘制自定义文本对象

Header: #include <QTextObjectInterface>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

公共函数

virtual ~QTextObjectInterface()
virtual void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0
virtual QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) = 0

详细说明

文本对象描述了文本文档中一个或多个元素的结构;例如,从 HTML 导入的图像就是使用文本对象实现的。文本对象知道在渲染文档时如何布局和绘制其元素。

Qt 允许通过向QTextCharFormat 注册自定义object type ,将自定义文本对象插入到文档中。还必须为该类型实现一个 QTextObjectInterface,并与文档的QAbstractTextDocumentLayout registered 。当渲染QTextDocument 时遇到该对象类型时,将调用该接口的intrinsicSize() 和drawObject() 函数。

下面列出了在文档中插入自定义文本对象的必要步骤:

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

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

文本对象的数据通常使用QTextCharFormat::setProperty() 保存在QTextCharFormat 中,然后使用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

您还可以获取文档 (doc) 和format 在该文档中的位置 (posInDocument)。

另请参见 intrinsicSize()。

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

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

计算出的大小将在以后调用drawObject() 时用于此format

另请参见 drawObject()。

© 2025 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.