このページでは

QTextFragment Class

QTextFragment クラスは、QTextDocument 内に、単一のQTextCharFormat を持つテキストの一部を保持します。続きを読む...

ヘッダー: #include <QTextFragment>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

注:このクラスのすべての関数は再入可能です。

パブリック関数

QTextFragment()
QTextFragment(const QTextFragment &other)
QTextCharFormat charFormat() const
int charFormatIndex() const
bool contains(int position) const
QList<QGlyphRun> glyphRuns(int pos = -1, int len = -1) const
bool isValid() const
int length() const
int position() const
QString text() const
bool operator!=(const QTextFragment &other) const
bool operator<(const QTextFragment &other) const
QTextFragment &operator=(const QTextFragment &other)
bool operator==(const QTextFragment &other) const

詳細な説明

テキストフラグメントは、単一の文字形式で保存されるテキストの一部を表します。文字形式が変化するテキストは、異なる形式のテキストフラグメントのシーケンスとして表現することができます。

ユーザーがフラグメント内のテキストを編集して、異なる文字形式を導入した場合、そのフラグメントのテキストは形式が変化する各ポイントで分割され、新しいフラグメントが作成されます。 たとえば、文の途中でテキストのスタイルを変更すると、そのフラグメントは 3 つの別々のフラグメントに分割されます。1 つ目と 3 つ目は以前と同じ形式で、2 つ目は新しいスタイルになります。 1 番目のフラグメントには文の先頭からのテキストが含まれ、2 番目のフラグメントには文の中間からのテキストが含まれ、3 番目のフラグメントには文の末尾からのテキストが含まれます。

中央のテキストを太字にすると、テキストが3つの部分に分かれる様子を示すスクリーンショット。

フラグメントのテキストおよび文字の書式は、text() およびcharFormat() 関数を使用して取得できます。length() 関数は、フラグメント内のテキストの長さを返します。position() は、ドキュメント内におけるフラグメントの開始位置を返します。フラグメントがドキュメント内の特定の位置を含むかどうかを判断するには、contains() 関数を使用します。

「 QTextDocument 」 および「リッチテキスト文書の構造」も参照してください 。

メンバー関数のドキュメント

QTextFragment::QTextFragment()

新しい空のテキストフラグメントを作成します。

QTextFragment::QTextFragment(const QTextFragment &other)

other テキストフラグメントの内容(テキストおよび書式)を、このテキストフラグメントにコピーします。

QTextCharFormat QTextFragment::charFormat() const

テキストフラグメントの文字形式を返します。

text()も参照してください 。

int QTextFragment::charFormatIndex() const

テキスト断片の文字形式に対応する、ドキュメントの内部文字形式リスト内のインデックスを返します。

QTextDocument::allFormats()も参照してください 。

bool QTextFragment::contains(int position) const

テキスト断片に、ドキュメント内の指定されたposition にあるテキストが含まれている場合はtrue を返し、そうでない場合はfalse を返します。

QList<QGlyphRun> QTextFragment::glyphRuns(int pos = -1, int len = -1) const

位置pos から始まる、このテキストフラグメントのlen 文字に対応するグリフを返します。グリフの位置は、QTextBlock のレイアウトの位置を基準としています。

pos がゼロより小さい場合、デフォルトではQTextFragment の先頭が使用されます。len がゼロより小さい場合、デフォルトではフラグメントの長さが使用されます。

関連項目: QGlyphRun 、QTextBlock::layout()、QTextLayout::position()、およびQPainter::drawGlyphRun()。

bool QTextFragment::isValid() const

これが有効なテキストフラグメント(つまり、文書内で有効な位置にあるもの)である場合は `true ` を返し、そうでない場合は `false` を返します。

int QTextFragment::length() const

テキスト断片に含まれる文字数を返します。

text()も参照してください 。

int QTextFragment::position() const

このテキスト断片がドキュメント内のどの位置にあるかを返します。

QString QTextFragment::text() const

テキストフラグメントをプレーンテキストとして返します。

length() およびcharFormat()も参照してください 。

bool QTextFragment::operator!=(const QTextFragment &other) const

このテキスト断片が、other のテキスト断片と(位置が異なるなどして)異なる場合は `true ` を返し、そうでない場合は `false` を返します。

bool QTextFragment::operator<(const QTextFragment &other) const

このテキスト断片が、other のテキスト断片よりもドキュメント内の早い位置に現れる場合は、true を返します。そうでない場合は、false を返します。

QTextFragment &QTextFragment::operator=(const QTextFragment &other)

other テキストフラグメントの内容(テキストおよび書式)を、このテキストフラグメントに割り当てます。

bool QTextFragment::operator==(const QTextFragment &other) const

このテキスト断片が、other のテキスト断片と(同じ位置で)一致する場合は `true ` を返し、一致しない場合は `false` を返します。

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