QTextObjectInterface Class

QTextObjectInterface クラスは、QTextDocumentでカスタムテキストオブジェクトの描画を可能にします

ヘッダー #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

詳細説明

テキストオブジェクトは、テキストドキュメント内の1つ以上の要素の構造を記述します。例えば、HTMLから取り込まれた画像はテキストオブジェクトを使って実装されます。例えば、HTML から取り込まれた画像は、テキストオブジェクトを使って実装されます。テキストオブジェクトは、ドキュメントがレンダリングされるときに、その要素をどのようにレイアウトして描画するかを知っています。

Qt では、QTextCharFormat にカスタムのobject type を登録することで、カスタムのテキストオブジェクトをドキュメントに挿入することができます。QTextObjectInterface もこのタイプ用に実装し、ドキュメントのQAbstractTextDocumentLayoutregistered する必要があります。QTextDocument をレンダリングしているときにそのオブジェクト型に出会うと、インターフェースのintrinsicSize() とdrawObject() 関数が呼び出されます。

以下のリストは、カスタム・テキスト・オブジェクトを文書に挿入するために必要な手順を説明したものです:

  • objectType を選択。objectType は、QTextFormat::UserObject 以上の値を持つ整数。
  • QTextCharFormat オブジェクトを作成し、setObjectType() 関数を使用してオブジェクト・タイプを選択したタイプに設定します。
  • QTextObjectInterface クラスを実装する。
  • QTextObjectInterface サブクラスのインスタンスでQAbstractTextDocumentLayout::registerHandler() を呼び出し、オブジェクト・タイプを登録します。
  • 選択したオブジェクト・タイプの前述のQTextCharFormatQChar::ObjectReplacementCharacter をドキュメントに挿入します。前述のように、QTextObjectInterfaceintrinsicSize() とdrawObject() の関数は、置換文字に遭遇するたびにQTextFormat をパラメータとして呼び出されます。

テキスト・オブジェクトを実装するクラスは、QObject と QTextObjectInterface の両方を継承する必要があります。QObject は最初に継承されるクラスでなければなりません。例えば:

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

テキスト・オブジェクトのデータは通常、QTextCharFormat::setProperty ()を使ってQTextCharFormat に格納され、QTextCharFormat::property ()を使って取り出されます。

警告: コピーと貼り付けの操作は、カスタム・テキスト・オブジェクトを無視します。

QTextCharFormat およびQTextLayoutも参照して ください。

メンバ関数ドキュメント

[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()関数は、指定されたドキュメント(doc)の指定された位置(posInDocument)で、format で表されるテキストオブジェクトのサイズを返します。

計算されたサイズは、このformat に対してdrawObject() を呼び出す際に使用されます。

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.