Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QTextLayout Class

TheQTextLayout class is used to lay out and render text.More...

Header:#include <QTextLayout>

Note: All functions in this class arereentrant.

Public Types

classFormatRange
enumCursorMode { SkipCharacters, SkipWords }

Public Functions

QTextLayout()
QTextLayout(const QString & text)
QTextLayout(const QString & text, const QFont & font, QPaintDevice * paintdevice = 0)
~QTextLayout()
QList<FormatRange>additionalFormats() const
voidbeginLayout()
QRectFboundingRect() const
boolcacheEnabled() const
voidclearAdditionalFormats()
voidclearLayout()
QTextLinecreateLine()
Qt::CursorMoveStylecursorMoveStyle() const
voiddraw(QPainter * p, const QPointF & pos, const QVector<FormatRange> & selections = QVector<FormatRange> (), const QRectF & clip = QRectF()) const
voiddrawCursor(QPainter * painter, const QPointF & position, int cursorPosition, int width) const
voiddrawCursor(QPainter * painter, const QPointF & position, int cursorPosition) const
voidendLayout()
QFontfont() const
QList<QGlyphRun>glyphRuns() const
boolisValidCursorPosition(int pos) const
intleftCursorPosition(int oldPos) const
QTextLinelineAt(int i) const
intlineCount() const
QTextLinelineForTextPosition(int pos) const
qrealmaximumWidth() const
qrealminimumWidth() const
intnextCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const
QPointFposition() const
intpreeditAreaPosition() const
QStringpreeditAreaText() const
intpreviousCursorPosition(int oldPos, CursorMode mode = SkipCharacters) const
intrightCursorPosition(int oldPos) const
voidsetAdditionalFormats(const QList<FormatRange> & formatList)
voidsetCacheEnabled(bool enable)
voidsetCursorMoveStyle(Qt::CursorMoveStyle style)
voidsetFont(const QFont & font)
voidsetPosition(const QPointF & p)
voidsetPreeditArea(int position, const QString & text)
voidsetText(const QString & string)
voidsetTextOption(const QTextOption & option)
QStringtext() const
QTextOptiontextOption() const

Detailed Description

TheQTextLayout 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 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 ofQTextLine 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 withsetText().

The layout can be seen as a sequence ofQTextLine objects; usecreateLine() to create aQTextLine instance, andlineAt() orlineForTextPosition() to retrieve created lines.

Here is a code snippet that demonstrates the layout phase:

int leading= fontMetrics.leading();qreal height=0;textLayout.beginLayout();while (1) {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'sdraw() function:

QPainter painter(this);textLayout.draw(&painter,QPoint(0,0));

For a given position in the text you can find a valid cursor position withisValidCursorPosition(),nextCursorPosition(), andpreviousCursorPosition().

TheQTextLayout itself can be positioned withsetPosition(); it has aboundingRect(), and aminimumWidth() and amaximumWidth().

See alsoQStaticText.

Member Type Documentation

enum QTextLayout::CursorMode

ConstantValue
QTextLayout::SkipCharacters0
QTextLayout::SkipWords1

Member Function Documentation

QTextLayout::QTextLayout()

Constructs an empty text layout.

See alsosetText().

QTextLayout::QTextLayout(constQString & text)

Constructs a text layout to lay out the giventext.

QTextLayout::QTextLayout(constQString & text, constQFont & font,QPaintDevice * paintdevice = 0)

Constructs a text layout to lay out the giventext with the specifiedfont.

All the metric and layout calculations will be done in terms of the paint device,paintdevice. Ifpaintdevice is 0 the calculations will be done in screen metrics.

QTextLayout::~QTextLayout()

Destructs the layout.

QList<FormatRange> QTextLayout::additionalFormats() const

Returns the list of additional formats supported by the text layout.

See alsosetAdditionalFormats() andclearAdditionalFormats().

void QTextLayout::beginLayout()

Begins the layout process.

See alsoendLayout().

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.

See alsosetCacheEnabled().

void QTextLayout::clearAdditionalFormats()

Clears the list of additional formats supported by the text layout.

See alsoadditionalFormats() andsetAdditionalFormats().

void QTextLayout::clearLayout()

Clears the line information in the layout. After having called this function,lineCount() returns 0.

This function was introduced in Qt 4.4.

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 theQTextLine::setLineWidth() function is called.

OnceQTextLine::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 theQTextLayout. If there is no text left to be inserted into the layout, theQTextLine returned will not be valid (isValid() will return false).

Qt::CursorMoveStyle QTextLayout::cursorMoveStyle() const

The cursor movement style of thisQTextLayout. The default isQt::LogicalMoveStyle.

This function was introduced in Qt 4.8.

See alsosetCursorMoveStyle().

void QTextLayout::draw(QPainter * p, constQPointF & pos, constQVector<FormatRange> & selections = QVector<FormatRange> (), constQRectF & clip = QRectF()) const

Draws the whole layout on the painterp at the position specified bypos. The rendered layout includes the givenselections and is clipped within the rectangle specified byclip.

void QTextLayout::drawCursor(QPainter * painter, constQPointF & position,int cursorPosition,int width) const

Draws a text cursor with the current pen and the specifiedwidth at the givenposition using thepainter specified. The corresponding position within the text is specified bycursorPosition.

void QTextLayout::drawCursor(QPainter * painter, constQPointF & position,int cursorPosition) const

This is an overloaded function.

Draws a text cursor with the current pen at the givenposition using thepainter specified. The corresponding position within the text is specified bycursorPosition.

void QTextLayout::endLayout()

Ends the layout process.

See alsobeginLayout().

QFont QTextLayout::font() const

Returns the current font that is used for the layout, or a default font if none is set.

See alsosetFont().

QList<QGlyphRun> QTextLayout::glyphRuns() const

Returns the glyph indexes and positions for all glyphs in thisQTextLayout. This is an expensive function, and should not be called in a time sensitive context.

This function was introduced in Qt 4.8.

See alsodraw() andQPainter::drawGlyphRun().

bool QTextLayout::isValidCursorPosition(int pos) const

/ Returns true if positionpos 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 diaresis (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.

int QTextLayout::leftCursorPosition(int oldPos) const

Returns the cursor position to the left ofoldPos, next to it. The position is dependent on the visual position of characters, after bi-directional reordering.

This function was introduced in Qt 4.8.

See alsorightCursorPosition() andpreviousCursorPosition().

QTextLine QTextLayout::lineAt(int i) const

Returns thei-th line of text in this text layout.

See alsolineCount() andlineForTextPosition().

int QTextLayout::lineCount() const

Returns the number of lines in this text layout.

See alsolineAt().

QTextLine QTextLayout::lineForTextPosition(int pos) const

Returns the line that contains the cursor position specified bypos.

See alsoisValidCursorPosition() andlineAt().

qreal QTextLayout::maximumWidth() const

The maximum width the layout could expand to; this is essentially the width of the entire text.

Warning: This function only returns a valid value after the layout has been done.

See alsominimumWidth().

qreal QTextLayout::minimumWidth() const

The minimum width the layout needs. This is the width of the layout's smallest non-breakable substring.

Warning: This function only returns a valid value after the layout has been done.

See alsomaximumWidth().

int QTextLayout::nextCursorPosition(int oldPos,CursorMode mode = SkipCharacters) const

Returns the next valid cursor position afteroldPos that respects the given cursormode. Returns value ofoldPos, ifoldPos is not a valid cursor position.

See alsoisValidCursorPosition() andpreviousCursorPosition().

QPointF QTextLayout::position() const

The global position of the layout. This is independent of the bounding rectangle and of the layout process.

This function was introduced in Qt 4.2.

See alsosetPosition().

int QTextLayout::preeditAreaPosition() const

Returns the position of the area in the text layout that will be processed before editing occurs.

See alsopreeditAreaText().

QString QTextLayout::preeditAreaText() const

Returns the text that is inserted in the layout before editing occurs.

See alsopreeditAreaPosition().

int QTextLayout::previousCursorPosition(int oldPos,CursorMode mode = SkipCharacters) const

Returns the first valid cursor position beforeoldPos that respects the given cursormode. Returns value ofoldPos, ifoldPos is not a valid cursor position.

See alsoisValidCursorPosition() andnextCursorPosition().

int QTextLayout::rightCursorPosition(int oldPos) const

Returns the cursor position to the right ofoldPos, next to it. The position is dependent on the visual position of characters, after bi-directional reordering.

This function was introduced in Qt 4.8.

See alsoleftCursorPosition() andnextCursorPosition().

void QTextLayout::setAdditionalFormats(constQList<FormatRange> & formatList)

Sets the additional formats supported by the text layout toformatList.

See alsoadditionalFormats() andclearAdditionalFormats().

void QTextLayout::setCacheEnabled(bool enable)

Enables caching of the complete layout information ifenable is true; otherwise disables layout caching. UsuallyQTextLayout throws most of the layouting information away after a call toendLayout() to reduce memory consumption. If you however want to draw the laid out text directly afterwards enabling caching might speed up drawing significantly.

See alsocacheEnabled().

void QTextLayout::setCursorMoveStyle(Qt::CursorMoveStyle style)

Set the cursor movement style. If theQTextLayout is backed by a document, you can ignore this and use the option inQTextDocument, this option is for widgets likeQLineEdit or custom widgets without aQTextDocument. Default value isQt::LogicalMoveStyle.

This function was introduced in Qt 4.8.

See alsocursorMoveStyle().

void QTextLayout::setFont(constQFont & font)

Sets the layout's font to the givenfont. The layout is invalidated and must be laid out again.

See alsofont().

void QTextLayout::setPosition(constQPointF & p)

Moves the text layout to pointp.

See alsoposition().

void QTextLayout::setPreeditArea(int position, constQString & text)

Sets theposition andtext of the area in the layout that is processed before editing occurs.

See alsopreeditAreaPosition() andpreeditAreaText().

void QTextLayout::setText(constQString & string)

Sets the layout's text to the givenstring. The layout is invalidated and must be laid out again.

Notice that when using thisQTextLayout as part of aQTextDocument this method will have no effect.

See alsotext().

void QTextLayout::setTextOption(constQTextOption & option)

Sets the text option structure that controls the layout process to the givenoption.

See alsotextOption().

QString QTextLayout::text() const

Returns the layout's text.

See alsosetText().

QTextOption QTextLayout::textOption() const

Returns the current text option used to control the layout process.

See alsosetTextOption().

© 2016 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 theGNU 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.


[8]ページ先頭

©2009-2025 Movatter.jp