Qt リファレンスガイド QTextObjectInterface

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 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:

https://doc.qt.io/qt-6/qtextobjectinterface.html

「テキストオブジェクトはテキストドキュメント内の一つ、あるいはより多くの要素の構造を記述します。例えば、HTMLからインポートされたイメージはテキストオブジェクトを使って実装されます。テキストオブジェクトはドキュメントが表示されるときその要素をレイアウトし、描く方法を知っています。
 Qtはカスタムテキストオブジェクトが、QTextCharFormatでカスタムのobject typeを登録することによってドキュメントへ挿入できるようにします。
 QTextObjectInterfaceはこのobject typeのために実装されなければならず、そのドキュメントのQAbstractTextDocumentLayoutに登録されます。
 ①object typeがQTextDocument表示中に出会う時、インターフェースのintrinsicSize()とdrawObject()関数が呼ばれます。
次のリストはカスタムテキストオブジェクトをドキュメントへ挿入するための要求されたステップを説明します。」

Choose an objectType. The objectType is an integer with a value greater or equal to QTextFormat::UserObject.

Create a QTextCharFormat object and set the object type to the chosen type using the setObjectType() function.

Implement the QTextObjectInterface class.

Call QAbstractTextDocumentLayout::registerHandler() with an instance of your QTextObjectInterface subclass to register your object type.

Insert QChar::ObjectReplacementCharacter with the aforementioned QTextCharFormat of the chosen object type into the document. As mentioned, the functions of QTextObjectInterface intrinsicSize() and drawObject() will then be called with the QTextFormat as parameter whenever the replacement character is encountered.

A class implementing a text object needs to inherit both QObject and QTextObjectInterface. QObject must be the first class inherited. For instance:

https://doc.qt.io/qt-6/qtextobjectinterface.html

「オブジェクトタイプを選択してください。オブジェクトタイプは、QTextFormat::UserObjectと同じかそれ以上の数の整数値です。
QTextCharFormatオブジェクトを作り、選んだオブジェクトタイプと同じ数値をsetObjectType()関数でセットしてください。
QTextObjectInterfaceクラスを実装します。
QAbstractTextDocumentLayout::registerHandler()を、あなたのオブジェクトタイプを登録するためQTextObjectInterfaceのサブクラスのインスタンスで呼びます。

「QChar::objectReplacementCharacterを上で述べた選んだオブジェクトタイプのQTextCharFormatでドキュメントへ挿入します。述べた通り、QTextObjectInterface intrinsicSizeの関数とdrawObjectはその時引数としてQTextFormatと共にコールされるでしょう。リプレイスメントキャラクターがエンカウントされるといつでも。
テキストオブジェクトを実装するクラスはQObjectとQTextObjectInterfaceの両方を継承している必要があります。QObjectは継承する最初のクラスでなければなりません。例えば、」

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

The data of a text object is usually stored in the QTextCharFormat using QTextCharFormat::setProperty(), and then retrieved with QTextCharFormat::property().

https://doc.qt.io/qt-6/qtextobjectinterface.html

「テキストオブジェクトのデータはたいていQTextCharFormat.setPropertyを使ってQTextCharFormat内に確保され、それからproperty()によって取り出されます。」

Member Function Documentation

[virtual]QTextObjectInterface::~QTextObjectInterface()

Destroys this QTextObjectInterface.

https://doc.qt.io/qt-6/qtextobjectinterface.html#dtor.QTextObjectInterface

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

Draws this text object using the specified 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.
See also
intrinsicSize().

https://doc.qt.io/qt-6/qtextobjectinterface.html#drawObject

「指定したペインターを使ってこのテキストオブジェクトを描画します。
矩形範囲のサイズ、intrinsicSize()で以前計算したサイズです。
矩形範囲の位置はペインターと相対的です。
あなたはドキュメント内のドキュメントとその位置も取得します。」

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

The intrinsicSize() 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.
See also
drawObject().

https://doc.qt.io/qt-6/qtextobjectinterface.html#intrinsicSize


「intrinsicSize()関数は指定されたドキュメント内のフォーマットで表現されるテキストオブジェクトのサイズを返す。計算されたサイズはこのフォーマットのためにdrawObject()を行うための後のコールのために使われます。」

著者のメモ:
QTextObjectInterfaceは、行内画像文字をQTextEdit内で表現するために利用されます。QtはinsertImageで画像を文字として挿入できます。この機能の内部では、QTextObjectInterfaceが使用されており、私たちはこのクラスを継承することで、画像はもちろん、それ以外のものを、自分達が描きたいように、行内画像文字として挿入することができるようになります。
 例えば、ワードソフトのルビ文字、組み文字などというものも、行内画像文字の一つです。



この記事が気に入ったらサポートをしてみませんか?