![]() |
CopperSpice API
1.9.2
|
The QTextLayout class is used to lay out and render text. More...
Classes | |
class | FormatRange |
This structure is used to apply extra formatting information More... | |
Public Types | |
enum | CursorMode |
Public Methods | |
QTextLayout () | |
QTextLayout (const QString &text) | |
QTextLayout (const QString &text, const QFont &font, 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 *painter, const QPointF &point, const QVector< FormatRange > &selections=QVector< FormatRange >(), const QRectF &clip=QRectF ()) const |
void | drawCursor (QPainter *painter, const QPointF &point, int cursorPosition, int width=1) const |
void | endLayout () |
QFont | font () const |
QVector< FormatRange > | formats () 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, CursorMode mode=SkipCharacters) const |
QPointF | position () const |
int | preeditAreaPosition () const |
QString | preeditAreaText () const |
int | previousCursorPosition (int oldPos, 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 QVector< FormatRange > &formats) |
void | setPosition (const QPointF &point) |
void | setPreeditArea (int position, const QString &text) |
void | setRawFont (const QRawFont &rawFont) |
void | setText (const QString &string) |
void | setTextOption (const QTextOption &option) |
QString | text () const |
const QTextOption & | textOption () const |
Friends | |
class | QGraphicsSimpleTextItem |
class | QPainter |
The QTextLayout class is used to lay out and render text. 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 low level API and unless you intend to implement your own text rendering for some specialized widget, you probably will not need to use it directly.
QTextLayout can be used with both plain and rich text. It can also 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() to create a QTextLine instance, and lineAt() or lineForTextPosition() to retrieve created lines.
For a given position in the text you can find a valid cursor position with isValidCursorPosition(), nextCursorPosition(), and previousCursorPosition(). The QTextLayout itself can be positioned with setPosition(); it has a boundingRect(), and a minimumWidth() and a maximumWidth().
The following code demonstrates the layout and draw process.
Constant | Value |
---|---|
QTextLayout::SkipCharacters | 0 |
QTextLayout::SkipWords | 1 |
QTextLayout::QTextLayout | ( | ) |
Constructs an empty text layout.
QTextLayout::QTextLayout | ( | const QString & | text | ) |
Constructs a text layout to lay out the given text.
QTextLayout::QTextLayout | ( | const QString & | text, |
const QFont & | font, | ||
QPaintDevice * | paintdevice = nullptr |
||
) |
Constructs a text layout to lay out the given text with the specified font. All the metric and layout calculations will be done in terms of the paintdevice. If paintdevice is a nullptr the calculations will be done in screen metrics.
QTextLayout::~QTextLayout | ( | ) |
Destructs the layout.
void QTextLayout::beginLayout | ( | ) |
Begins the layout process.
QRectF QTextLayout::boundingRect | ( | ) | const |
The smallest rectangle that contains all the lines in the layout.
bool QTextLayout::cacheEnabled | ( | ) | const |
Returns true if the complete layout information is cached, otherwise returns false.
void QTextLayout::clearFormats | ( | ) |
Clears the list of additional formats supported by the text layout.
void QTextLayout::clearLayout | ( | ) |
Clears the line information in the layout. After having called this method lineCount() will return 0.
QTextLine QTextLayout::createLine | ( | ) |
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() function is called.
Once 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).
Qt::CursorMoveStyle QTextLayout::cursorMoveStyle | ( | ) | const |
The cursor movement style of this QTextLayout. The default is Qt::LogicalMoveStyle.
void QTextLayout::draw | ( | QPainter * | painter, |
const QPointF & | point, | ||
const QVector< FormatRange > & | selections = QVector<FormatRange>() , |
||
const QRectF & | clip = QRectF() |
||
) | const |
Draws the whole layout on the painter at the position specified by point. The rendered layout includes the given selections and is clipped within the rectangle specified by clip.
void QTextLayout::drawCursor | ( | QPainter * | painter, |
const QPointF & | point, | ||
int | cursorPosition, | ||
int | width = 1 |
||
) | const |
Draws a text cursor with the current pen and the specified width at the given point using the painter specified. The corresponding position within the text is specified by cursorPosition.
void QTextLayout::endLayout | ( | ) |
Ends the layout process.
QFont QTextLayout::font | ( | ) | const |
Returns the current font that is used for the layout, or a default font if none is set.
QVector< FormatRange > QTextLayout::formats | ( | ) | const |
Returns the list of additional formats supported by the text layout.
Returns the glyphs corresponding to length code points of this text fragment starting at position from. The positions of the glyphs are relative to the position of the QTextBlock layout. If from is less than 0 the glyphs will start at the beginning. If length is less than 0 the glyphs will stop at the end of the fragment.
bool QTextLayout::isValidCursorPosition | ( | int | pos | ) | const |
Returns true if position pos is a valid cursor position. Some positions in the text are not valid cursor positions because the position is within a grapheme cluster.
int QTextLayout::leftCursorPosition | ( | int | oldPos | ) | const |
Returns the cursor position to the left of oldPos, next to it. The position is dependent on the visual position of characters, after bi-directional reordering.
QTextLine QTextLayout::lineAt | ( | int | i | ) | const |
Returns the line of text at position i in this text layout.
int QTextLayout::lineCount | ( | ) | const |
Returns the number of lines in this text layout.
QTextLine QTextLayout::lineForTextPosition | ( | int | pos | ) | const |
Returns the line that contains the cursor position specified by pos.
qreal QTextLayout::maximumWidth | ( | ) | const |
The maximum width the layout could expand to; this is essentially the width of the entire text.
qreal QTextLayout::minimumWidth | ( | ) | const |
The minimum width the layout needs. This is the width of the layout's smallest non-breakable substring.
int QTextLayout::nextCursorPosition | ( | int | oldPos, |
CursorMode | mode = SkipCharacters |
||
) | const |
Returns the next valid cursor position after oldPos that respects the given cursor mode. Returns value of oldPos, if oldPos is not a valid cursor position.
QPointF QTextLayout::position | ( | ) | const |
The global position of the layout. This is independent of the bounding rectangle and of the layout process.
int QTextLayout::preeditAreaPosition | ( | ) | const |
Returns the position of the area in the text layout that will be processed before editing occurs.
QString QTextLayout::preeditAreaText | ( | ) | const |
Returns the text that is inserted in the layout before editing occurs.
int QTextLayout::previousCursorPosition | ( | int | oldPos, |
CursorMode | mode = SkipCharacters |
||
) | const |
Returns the first valid cursor position before oldPos that respects the given cursor mode. Returns value of oldPos, if oldPos is not a valid cursor position.
int QTextLayout::rightCursorPosition | ( | int | oldPos | ) | const |
Returns the cursor position to the right of oldPos, next to it. The position is dependent on the visual position of characters, after bi-directional reordering.
void QTextLayout::setCacheEnabled | ( | bool | enable | ) |
Enables caching of the complete layout information if enable is true, otherwise disables layout caching. Usually QTextLayout throws most of the layout 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.
void QTextLayout::setCursorMoveStyle | ( | Qt::CursorMoveStyle | style | ) |
Set the cursor movement style. If the 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.
void QTextLayout::setFont | ( | const QFont & | font | ) |
Sets the layout's font to the given font. The layout is invalidated and must be laid out again.
void QTextLayout::setFormats | ( | const QVector< FormatRange > & | formats | ) |
Sets the additional formats supported by the text layout to formats. The formats are applied with preedit area text in place.
void QTextLayout::setPosition | ( | const QPointF & | point | ) |
Moves the text layout to the given point.
void QTextLayout::setPreeditArea | ( | int | position, |
const QString & | text | ||
) |
Sets the position and text of the area in the layout that is processed before editing occurs.
void QTextLayout::setRawFont | ( | const QRawFont & | rawFont | ) |
Sets the layout's font to the given rawFont. The layout is invalidated and must be laid out again.
void QTextLayout::setText | ( | const QString & | string | ) |
Sets the layout's text to the given string. The layout is invalidated and must be laid out again. When using this QTextLayout as part of a QTextDocument this method will have no effect.
void QTextLayout::setTextOption | ( | const QTextOption & | option | ) |
Sets the text option structure that controls the layout process to the given option.
const QTextOption & QTextLayout::textOption | ( | ) | const |
Returns the current text option used to control the layout process.