QTextLayout 类用于布置和渲染文本。 更多...
头: | #include <QTextLayout> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
注意: 此类的所有函数 可重入 .
struct | FormatRange |
enum | CursorMode { SkipCharacters, SkipWords } |
(从 6.5 起)
enum
|
GlyphRunRetrievalFlag { RetrieveGlyphIndexes, RetrieveGlyphPositions, RetrieveStringIndexes, RetrieveString, RetrieveAll } |
flags | GlyphRunRetrievalFlags |
QTextLayout () | |
QTextLayout (const QString & text ) | |
QTextLayout (const QString & text , const QFont & font , const QPaintDevice * paintdevice = nullptr) | |
~QTextLayout () | |
void | beginLayout () |
QRectF | boundingRect () const |
bool | cacheEnabled () const |
void | clearFormats () |
void | clearLayout () |
QTextLine | createLine () |
Qt::CursorMoveStyle | cursorMoveStyle () const |
void | draw (QPainter * p , const QPointF & pos , const QList<QTextLayout::FormatRange> & selections = QList<FormatRange>(), const QRectF & clip = QRectF()) const |
void | drawCursor (QPainter * painter , const QPointF & position , int cursorPosition , int width ) const |
void | drawCursor (QPainter * painter , const QPointF & position , int cursorPosition ) const |
void | endLayout () |
QFont | font () const |
QList<QTextLayout::FormatRange> | formats () const |
(从 6.5 起)
QList<QGlyphRun>
|
glyphRuns (int from , int length , QTextLayout::GlyphRunRetrievalFlags retrievalFlags ) const |
QList<QGlyphRun> | glyphRuns (int from = -1, int length = -1) const |
bool | isValidCursorPosition (int pos ) const |
int | leftCursorPosition (int oldPos ) const |
QTextLine | lineAt (int i ) const |
int | lineCount () const |
QTextLine | lineForTextPosition (int pos ) const |
qreal | maximumWidth () const |
qreal | minimumWidth () const |
int | nextCursorPosition (int oldPos , QTextLayout::CursorMode mode = SkipCharacters) const |
QPointF | position () const |
int | preeditAreaPosition () const |
QString | preeditAreaText () const |
int | previousCursorPosition (int oldPos , QTextLayout::CursorMode mode = SkipCharacters) const |
int | rightCursorPosition (int oldPos ) const |
void | setCacheEnabled (bool enable ) |
void | setCursorMoveStyle (Qt::CursorMoveStyle style ) |
void | setFont (const QFont & font ) |
void | setFormats (const QList<QTextLayout::FormatRange> & 格式 ) |
void | setPosition (const QPointF & p ) |
void | setPreeditArea (int position , const QString & text ) |
void | setText (const QString & string ) |
void | setTextOption (const QTextOption & option ) |
QString | text () const |
const QTextOption & | textOption () const |
It offers many features expected from a modern text layout engine, including Unicode compliant rendering, line breaking and handling of cursor positioning. It can also produce and render device independent layout, something that is important for WYSIWYG applications.
The class has a rather low level API and unless you intend to implement your own text rendering for some specialized widget, you probably won't need to use it directly.
QTextLayout can be used with both plain and rich text.
QTextLayout can be used to create a sequence of QTextLine instances with given widths and can position them independently on the screen. Once the layout is done, these lines can be drawn on a paint device.
The text to be laid out can be provided in the constructor or set with setText ().
The layout can be seen as a sequence of QTextLine objects; use createLine () 以创建 QTextLine instance, and lineAt () 或 lineForTextPosition () to retrieve created lines.
Here is a code snippet that demonstrates the layout phase:
int leading = fontMetrics.leading(); qreal height = 0; textLayout.setCacheEnabled(true); textLayout.beginLayout(); while (true) { QTextLine line = textLayout.createLine(); if (!line.isValid()) break; line.setLineWidth(lineWidth); height += leading; line.setPosition(QPointF(0, height)); height += line.height(); } textLayout.endLayout();
The text can then be rendered by calling the layout's draw () 函数:
QPainter painter(this); textLayout.draw(&painter, QPoint(0, 0));
It is also possible to draw each line individually, for instance to draw the last line that fits into a widget elided:
QPainter painter(this); QFontMetrics fontMetrics = painter.fontMetrics(); int lineSpacing = fontMetrics.lineSpacing(); int y = 0; QTextLayout textLayout(content, painter.font()); textLayout.beginLayout(); while (true) { QTextLine line = textLayout.createLine(); if (!line.isValid()) break; line.setLineWidth(width()); const int nextLineY = y + lineSpacing; if (height() >= nextLineY + lineSpacing) { line.draw(&painter, QPoint(0, y)); y = nextLineY; } else { const QString lastLine = content.mid(line.textStart()); const QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width()); painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine); line = textLayout.createLine(); break; } } textLayout.endLayout();
For a given position in the text you can find a valid cursor position with isValidCursorPosition (), nextCursorPosition (),和 previousCursorPosition ().
The QTextLayout itself can be positioned with setPosition (); it has a boundingRect (), and a minimumWidth () 和 maximumWidth ().
另请参阅 QStaticText .
常量 | 值 |
---|---|
QTextLayout::SkipCharacters
|
0
|
QTextLayout::SkipWords
|
1
|
[since 6.5]
enum QTextLayout::
GlyphRunRetrievalFlag
GlyphRunRetrievalFlag specifies flags passed to the glyphRuns () functions to determine which properties of the layout are returned in the QGlyphRun objects. Since each property will consume memory and may require additional allocations, it is a good practice to only request the properties you will need to access later.
常量 | 值 | 描述 |
---|---|---|
QTextLayout::RetrieveGlyphIndexes
|
0x1
|
Retrieves the indexes in the font which correspond to the glyphs. |
QTextLayout::RetrieveGlyphPositions
|
0x2
|
Retrieves the relative positions of the glyphs in the layout. |
QTextLayout::RetrieveStringIndexes
|
0x4
|
Retrieves the indexes in the original string that correspond to each of the glyphs. |
QTextLayout::RetrieveString
|
0x8
|
Retrieves the original source string from the layout. |
QTextLayout::RetrieveAll
|
0xffff
|
Retrieves all available properties of the layout. |
该枚举在 Qt 6.5 引入。
The GlyphRunRetrievalFlags type is a typedef for QFlags <GlyphRunRetrievalFlag>. It stores an OR combination of GlyphRunRetrievalFlag values.
另请参阅 glyphRuns () 和 QTextLine::glyphRuns ().
Constructs an empty text layout.
另请参阅 setText ().
Constructs a text layout to lay out the given text .
Constructs a text layout to lay out the given text 采用指定 font .
All the metric and layout calculations will be done in terms of the paint device,
paintdevice
。若
paintdevice
is
nullptr
the calculations will be done in screen metrics.
[noexcept]
QTextLayout::
~QTextLayout
()
Destructs the layout.
Begins the layout process.
警告: This will invalidate the layout, so all existing QTextLine objects that refer to the previous contents should now be discarded.
另请参阅 endLayout ().
The smallest rectangle that contains all the lines in the layout.
返回
true
if the complete layout information is cached; otherwise returns
false
.
另请参阅 setCacheEnabled ().
Clears the list of additional formats supported by the text layout.
另请参阅 formats () 和 setFormats ().
Clears the line information in the layout. After having called this function, lineCount () returns 0.
警告: This will invalidate the layout, so all existing QTextLine objects that refer to the previous contents should now be discarded.
Returns a new text line to be laid out if there is text to be inserted into the layout; otherwise returns an invalid text line.
The text layout creates a new line object that starts after the last line in the layout, or at the beginning if the layout is empty. The layout maintains an internal cursor, and each line is filled with text from the cursor position onwards when the QTextLine::setLineWidth () 函数被调用。
一旦 QTextLine::setLineWidth () is called, a new line can be created and filled with text. Repeating this process will lay out the whole block of text contained in the QTextLayout . If there is no text left to be inserted into the layout, the QTextLine returned will not be valid (isValid() will return false).
The cursor movement style of this QTextLayout 。默认为 Qt::LogicalMoveStyle .
另请参阅 setCursorMoveStyle ().
Draws the whole layout on the painter p at the position specified by pos . The rendered layout includes the given selections and is clipped within the rectangle specified by clip .
Draws a text cursor with the current pen and the specified width 在给定 position 使用 painter specified. The corresponding position within the text is specified by cursorPosition .
这是重载函数。
Draws a text cursor with the current pen at the given position 使用 painter specified. The corresponding position within the text is specified by cursorPosition .
Ends the layout process.
另请参阅 beginLayout ().
Returns the current font that is used for the layout, or a default font if none is set.
另请参阅 setFont ().
Returns the list of additional formats supported by the text layout.
另请参阅 setFormats () 和 clearFormats ().
[since 6.5]
QList
<
QGlyphRun
> QTextLayout::
glyphRuns
(
int
from
,
int
length
,
QTextLayout::GlyphRunRetrievalFlags
retrievalFlags
) const
这是重载函数。
Returns the glyph indexes and positions for all glyphs corresponding to the length characters starting at the position from 在此 QTextLayout . This is an expensive function, and should not be called in a time sensitive context.
若 from is less than zero, then the glyph run will begin at the first character in the layout. If length is less than zero, it will span the entire string from the start position.
The retrievalFlags specifies which properties of the QGlyphRun will be retrieved from the layout. To minimize allocations and memory consumption, this should be set to include only the properties that you need to access later.
该函数在 Qt 6.5 引入。
另请参阅 draw () 和 QPainter::drawGlyphRun ().
这是重载函数。
Returns the glyph indexes and positions for all glyphs corresponding to the length characters starting at the position from 在此 QTextLayout . This is an expensive function, and should not be called in a time sensitive context.
若 from is less than zero, then the glyph run will begin at the first character in the layout. If length is less than zero, it will span the entire string from the start position.
注意: This is equivalent to calling glyphRuns(from, length, QTextLayout::GlyphRunRetrievalFlag::GlyphIndexes | QTextLayout::GlyphRunRetrievalFlag::GlyphPositions).
另请参阅 draw () 和 QPainter::drawGlyphRun ().
返回
true
if position
pos
is a valid cursor position.
In a Unicode context some positions in the text are not valid cursor positions, because the position is inside a Unicode surrogate or a grapheme cluster.
A grapheme cluster is a sequence of two or more Unicode characters that form one indivisible entity on the screen. For example the latin character `Ä' can be represented in Unicode by two characters, `A' (0x41), and the combining diaeresis (0x308). A text cursor can only validly be positioned before or after these two characters, never between them since that wouldn't make sense. In indic languages every syllable forms a grapheme cluster.
Returns the cursor position to the left of oldPos , next to it. It's dependent on the visual position of characters, after bi-directional reordering.
另请参阅 rightCursorPosition () 和 previousCursorPosition ().
返回 i -th line of text in this text layout.
另请参阅 lineCount () 和 lineForTextPosition ().
Returns the number of lines in this text layout.
另请参阅 lineAt ().
Returns the line that contains the cursor position specified by pos .
另请参阅 isValidCursorPosition () 和 lineAt ().
The maximum width the layout could expand to; this is essentially the width of the entire text.
警告: This function only returns a valid value after the layout has been done.
另请参阅 minimumWidth ().
The minimum width the layout needs. This is the width of the layout's smallest non-breakable substring.
警告: This function only returns a valid value after the layout has been done.
另请参阅 maximumWidth ().
Returns the next valid cursor position after oldPos that respects the given cursor mode . Returns value of oldPos ,若 oldPos is not a valid cursor position.
另请参阅 isValidCursorPosition () 和 previousCursorPosition ().
The global position of the layout. This is independent of the bounding rectangle and of the layout process.
另请参阅 setPosition ().
Returns the position of the area in the text layout that will be processed before editing occurs.
另请参阅 preeditAreaText ().
Returns the text that is inserted in the layout before editing occurs.
另请参阅 preeditAreaPosition ().
Returns the first valid cursor position before oldPos that respects the given cursor mode . Returns value of oldPos ,若 oldPos is not a valid cursor position.
另请参阅 isValidCursorPosition () 和 nextCursorPosition ().
Returns the cursor position to the right of oldPos , next to it. It's dependent on the visual position of characters, after bi-directional reordering.
另请参阅 leftCursorPosition () 和 nextCursorPosition ().
Enables caching of the complete layout information if enable is true; otherwise disables layout caching. Usually QTextLayout throws most of the layouting information away after a call to endLayout () to reduce memory consumption. If you however want to draw the laid out text directly afterwards enabling caching might speed up drawing significantly.
另请参阅 cacheEnabled ().
Sets the visual cursor movement style to the given style 。若 QTextLayout is backed by a document, you can ignore this and use the option in QTextDocument , this option is for widgets like QLineEdit or custom widgets without a QTextDocument . Default value is Qt::LogicalMoveStyle .
另请参阅 cursorMoveStyle ().
Sets the layout's font to the given font . The layout is invalidated and must be laid out again.
另请参阅 font ().
Sets the additional formats supported by the text layout to 格式 . The formats are applied with preedit area text in place.
另请参阅 formats () 和 clearFormats ().
Moves the text layout to point p .
另请参阅 position ().
设置 position and text of the area in the layout that is processed before editing occurs. The layout is invalidated and must be laid out again.
另请参阅 preeditAreaPosition () 和 preeditAreaText ().
Sets the layout's text to the given string . The layout is invalidated and must be laid out again.
Notice that when using this QTextLayout as part of a QTextDocument this method will have no effect.
另请参阅 text ().
Sets the text option structure that controls the layout process to the given option .
另请参阅 textOption ().
Returns the layout's text.
另请参阅 setText ().
Returns the current text option used to control the layout process.
另请参阅 setTextOption ().