本页内容

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 中所有字形(glyphs)的索引和位置,这些字形对应于由from 和length 定义的范围内的字符。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 中所有字形(glyphs)的索引和位置,这些字形对应于由from 和length 定义的字符范围。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 ` 为真,则将正行前距计入行高;否则不计入行前距。

默认情况下,不包含行间距。

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

另请参阅 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

将 x 坐标x 转换为最接近的匹配光标位置,具体取决于光标位置类型cpos 。请注意,结果光标位置可能包含预编辑区域中的文本。

另请参阅 cursorToX()。

qreal QTextLine::y() const

返回该行的 y 坐标。

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

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