QTextLine Class

QTextLine 类表示QTextLayout 内的一行文本 ... 更多...

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

注意:该类中的所有函数都是可重入的

公共类型

enum CursorPosition { CursorBetweenCharacters, CursorOnCharacter }
enum Edge { Leading, Trailing }

公共函数

QTextLine()
qreal ascent() const
qreal cursorToX(int *cursorPos, QTextLine::Edge edge = Leading) const
qreal cursorToX(int cursorPos, QTextLine::Edge edge = Leading) const
qreal descent() const
void draw(QPainter *painter, const QPointF &position) const
(since 6.5) QList<QGlyphRun> glyphRuns(int from, int length, QTextLayout::GlyphRunRetrievalFlags retrievalFlags) const
QList<QGlyphRun> glyphRuns(int from = -1, int length = -1) const
qreal height() const
qreal horizontalAdvance() const
bool isValid() const
qreal leading() const
bool leadingIncluded() const
int lineNumber() const
QRectF naturalTextRect() const
qreal naturalTextWidth() const
QPointF position() const
QRectF rect() const
void setLeadingIncluded(bool included)
void setLineWidth(qreal width)
void setNumColumns(int numColumns)
void setNumColumns(int numColumns, qreal alignmentWidth)
void setPosition(const QPointF &pos)
int textLength() const
int textStart() const
qreal width() const
qreal x() const
int xToCursor(qreal x, QTextLine::CursorPosition cpos = CursorBetweenCharacters) const
qreal y() const

详细说明

文本行通常由QTextLayout::createLine() 创建。

创建文本行后,可使用setLineWidth() 或setNumColumns() 函数填充文本行。文本行有许多属性,包括它所占的矩形rect() 、它的坐标x() 和y() 、它的textLength() 、width() 和naturalTextWidth() 以及它相对于文本的ascent() 和descent() 。光标在行中的位置可从cursorToX() 中获得,其反向位置可从xToCursor() 中获得。使用setPosition() 可以移动一行。

成员类型文档

enum QTextLine::CursorPosition

常数数值
QTextLine::CursorBetweenCharacters0
QTextLine::CursorOnCharacter1

enum QTextLine::Edge

常数
QTextLine::Leading0
QTextLine::Trailing1

成员函数文档

QTextLine::QTextLine()

创建无效行。

qreal QTextLine::ascent() const

返回直线的坡度。

另请参阅 descent() 和height()。

qreal QTextLine::cursorToX(int *cursorPos, QTextLine::Edge edge = Leading) const

将光标位置cursorPos 转换为行内相应的 x 位置,同时考虑到edge

如果cursorPos 不是有效的光标位置,则会使用最近的有效光标位置,并修改cursorPos 以指向该有效光标位置。

另请参见 xToCursor() 。

qreal QTextLine::cursorToX(int cursorPos, QTextLine::Edge edge = Leading) const

这是一个重载函数。

qreal QTextLine::descent() const

返回直线的下降值。

另请参阅 ascent() 和height()。

void QTextLine::draw(QPainter *painter, const QPointF &position) const

在给定的painter 上以指定的position 画一条直线。

[since 6.5] QList<QGlyphRun> QTextLine::glyphRuns(int from, int length, QTextLayout::GlyphRunRetrievalFlags retrievalFlags) const

返回QTextLine 中所有字形在fromlength 所定义范围内的字形索引和位置。from 索引是相对于包含QTextLayout 的文本开头而言的,范围必须在函数textStart() 和textLength() 给出的QTextLine 范围内。

retrievalFlags 指定将从布局中检索QGlyphRun 的哪些属性。为尽量减少分配和内存消耗,应将其设置为只包含稍后需要访问的属性。

如果from 为负值,则默认为textStart() ;如果length 为负值,则默认为textLength() 的返回值。

此函数在 Qt 6.5 中引入。

另请参阅 QTextLayout::glyphRuns()。

QList<QGlyphRun> QTextLine::glyphRuns(int from = -1, int length = -1) const

这是一个重载函数。

返回QTextLine 中所有字形在fromlength 所定义范围内的字形索引和位置。from 索引是相对于包含QTextLayout 的文本开头而言的,范围必须在函数textStart() 和textLength() 给出的QTextLine 范围内。

如果from 为负值,则默认为textStart() ;如果length 为负值,则默认为textLength() 的返回值。

注意: 这等同于调用 glyphRuns(from,length,QTextLayout::GlyphRunRetrievalFlag::GlyphIndexes | QTextLayout::GlyphRunRetrievalFlag::GlyphPositions)。

另请参阅 QTextLayout::glyphRuns().

qreal QTextLine::height() const

返回行的高度。如果不包括前导,则等于ascent() +descent() 。如果包含前导,则等于ascent() +descent() +leading()。

另请参阅 ascent()、descent()、leading() 和setLeadingIncluded()。

qreal QTextLine::horizontalAdvance() const

返回文本的水平前进值。文字的前进方向是从其位置到下一个自然绘制文字的位置的距离。

通过在文本行的位置上加上前移值,并以此作为第二行文本的位置,就可以将两行并排定位,中间不会出现空隙。

bool QTextLine::isValid() const

如果该文本行有效,则返回true ;否则返回false

qreal QTextLine::leading() const

返回该行的前导。

另请参阅 ascent()、descent() 和height()。

bool QTextLine::leadingIncluded() const

如果行高包含正前角,则返回true ;否则返回false

默认情况下,不包含前导。

另请参见 setLeadingIncluded().

int QTextLine::lineNumber() const

返回该行在文本引擎中的位置。

QRectF QTextLine::naturalTextRect() const

返回直线覆盖的矩形区域。

qreal QTextLine::naturalTextWidth() const

返回文本所在行的宽度。它总是 <= 到width(),是 layout() 在不改变换行位置的情况下可以使用的最小宽度。

QPointF QTextLine::position() const

返回该行相对于文本布局位置的位置。

另请参阅 setPosition()。

QRectF QTextLine::rect() const

返回直线的边界矩形。

另请参阅 x()、y()、textLength() 和width()。

void QTextLine::setLeadingIncluded(bool included)

如果included 为 true,则在行高中包含正前导;否则不包含前导。

默认情况下,不包含前导。

请注意,负前导将被忽略,必须在使用文本行的代码中通过让行重叠来处理。

另请参见 leadingIncluded()。

void QTextLine::setLineWidth(qreal width)

用给定的width 行。该行将从起始位置开始填入尽可能多的字符。如果文本无法在行尾分割,则会在下一个空白处或文本末尾填入更多字符。

void QTextLine::setNumColumns(int numColumns)

起行。该行将从起始位置开始填充numColumns 所指定的字符数。如果文本在numColumns 字符之前无法分割,则该行将填入相同数量的字符,直至下一个空白处或文本末尾。

void QTextLine::setNumColumns(int numColumns, qreal alignmentWidth)

起行。该行将从起始位置开始填充numColumns 所指定的字符数。如果文本在numColumns 字符之前无法分割,则该行将填入与下一个空白处或文本末尾同样多的字符。所提供的alignmentWidth 将用作对齐的参考宽度。

void QTextLine::setPosition(const QPointF &pos)

将行移动到pos 位置。

另请参见 position() 。

int QTextLine::textLength() const

返回行中文本的长度。

另请参阅 naturalTextWidth()。

int QTextLine::textStart() const

返回从传递给QTextLayout 的字符串开始的行的起始位置。

qreal QTextLine::width() const

返回 layout() 函数指定的行宽。

另请参阅 naturalTextWidth()、x()、y()、textLength() 和rect()。

qreal QTextLine::x() const

返回该行的 x 位置。

另请参阅 rect()、y()、textLength() 和width()。

int QTextLine::xToCursor(qreal x, QTextLine::CursorPosition cpos = CursorBetweenCharacters) const

根据光标位置类型cpos ,将 x 坐标x 转换为最接近的匹配光标位置。请注意,结果光标位置包括可能的预编辑区文本。

另请参阅 cursorToX() 。

qreal QTextLine::y() const

返回直线的 y 位置。

另请参阅 x()、rect()、textLength() 和width()。

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