QTextFragment 类

QTextFragment 类保持一块文本在 QTextDocument 采用单 QTextCharFormat . 更多...

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

注意: 此类的所有函数 可重入 .

公共函数

  QTextFragment (const QTextFragment & other )
  QTextFragment ()
QTextFragment & operator= (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
bool operator== (const QTextFragment & other ) const

详细描述

A text fragment describes a piece of text that is stored with a single character format. Text in which the character format changes can be represented by sequences of text fragments with different formats.

If the user edits the text in a fragment and introduces a different character format, the fragment's text will be split at each point where the format changes, and new fragments will be created. For example, changing the style of some text in the middle of a sentence will cause the fragment to be broken into three separate fragments: the first and third with the same format as before, and the second with the new style. The first fragment will contain the text from the beginning of the sentence, the second will contain the text from the middle, and the third takes the text from the end of the sentence.

A fragment's text and character format can be obtained with the text () 和 charFormat () 函数。 length () function gives the length of the text in the fragment. position () gives the position in the document of the start of the fragment. To determine whether the fragment contains a particular position within the document, use the contains () 函数。

另请参阅 QTextDocument and 富文本文档结构 .

成员函数文档编制

QTextFragment:: QTextFragment (const QTextFragment & other )

Copies the content (text and format) of the other text fragment to this text fragment.

QTextFragment:: QTextFragment ()

Creates a new empty text fragment.

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

Assigns the content (text and format) of the other text fragment to this text fragment.

QTextCharFormat QTextFragment:: charFormat () const

Returns the text fragment's character format.

另请参阅 text ().

int QTextFragment:: charFormatIndex () const

Returns an index into the document's internal list of character formats for the text fragment's character format.

另请参阅 QTextDocument::allFormats ().

bool QTextFragment:: contains ( int position ) const

返回 true if the text fragment contains the text at the given position in the document; otherwise returns false .

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

Returns the glyphs corresponding to len characters of this text fragment starting at position pos . The positions of the glyphs are relative to the position of the QTextBlock 's layout.

pos is less than zero, it will default to the start of the QTextFragment 。若 len is less than zero, it will default to the length of the fragment.

另请参阅 QGlyphRun , QTextBlock::layout (), QTextLayout::position (),和 QPainter::drawGlyphRun ().

bool QTextFragment:: isValid () const

返回 true if this is a valid text fragment (i.e. has a valid position in a document); otherwise returns false .

int QTextFragment:: length () const

Returns the number of characters in the text fragment.

另请参阅 text ().

int QTextFragment:: position () const

Returns the position of this text fragment in the document.

QString QTextFragment:: text () const

Returns the text fragment's as plain text.

另请参阅 length () 和 charFormat ().

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

返回 true if this text fragment is different (at a different position) from the other text fragment; otherwise returns false.

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

返回 true if this text fragment appears earlier in the document than the other text fragment; otherwise returns false .

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

返回 true if this text fragment is the same (at the same position) as the other text fragment; otherwise returns false .