QTextLayoutclass is used to lay out and render text. 更多 …
def
additionalFormats
()
def
beginLayout
()
def
boundingRect
()
def
cacheEnabled
()
def
clearAdditionalFormats
()
def
clearFormats
()
def
clearLayout
()
def
createLine
()
def
cursorMoveStyle
()
def
draw
(p, pos[, selections=list()[, clip=QRectF()]])
def
drawCursor
(p, pos, cursorPosition)
def
drawCursor
(p, pos, cursorPosition, width)
def
endLayout
()
def
font
()
def
formats
()
def
isValidCursorPosition
(pos)
def
leftCursorPosition
(oldPos)
def
lineAt
(i)
def
lineCount
()
def
lineForTextPosition
(pos)
def
maximumWidth
()
def
minimumWidth
()
def
nextCursorPosition
(oldPos[, mode=SkipCharacters])
def
position
()
def
preeditAreaPosition
()
def
preeditAreaText
()
def
previousCursorPosition
(oldPos[, mode=SkipCharacters])
def
rightCursorPosition
(oldPos)
def
setAdditionalFormats
(overrides)
def
setCacheEnabled
(enable)
def
setCursorMoveStyle
(style)
def
setFlags
(flags)
def
setFont
(f)
def
setFormats
(overrides)
def
setPosition
(p)
def
setPreeditArea
(position, text)
def
setRawFont
(rawFont)
def
setText
(string)
def
setTextOption
(option)
def
text
()
def
textOption
()
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.
QTextLayoutcan be used with both plain and rich text.
QTextLayoutcan be used to create a sequence ofQTextLineinstances 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
QTextLineobjects; usecreateLine()to create aQTextLineinstance, andlineAt()orlineForTextPosition()to retrieve created lines.Here is a code snippet that demonstrates the layout phase:
leading = fontMetrics.leading() height = 0 widthUsed = 0 textLayout.beginLayout() while True: line = textLayout.createLine() if not line.isValid(): break line.setLineWidth(lineWidth) height += leading line.setPosition(QPointF(0, height)) height += line.height() widthUsed = qMax(widthUsed, line.naturalTextWidth()) textLayout.endLayout()The text can then be rendered by calling the layout’s
draw()函数:painter = QPainter(self) textLayout.draw(painter, QPoint(0, 0))For a given position in the text you can find a valid cursor position with
isValidCursorPosition(),nextCursorPosition(),和previousCursorPosition().
QTextLayoutitself can be positioned withsetPosition(); it has aboundingRect(),和minimumWidth()和maximumWidth().另请参阅
QTextLayout
¶
QTextLayout(text)
QTextLayout(text, font[, paintdevice=None])
QTextLayout(b)
- param font
- param paintdevice
- param b
- param text
unicode
Constructs an empty text layout.
另请参阅
PySide2.QtGui.QTextLayout.
CursorMode
¶
|
常量 |
描述 |
|---|---|
|
QTextLayout.SkipCharacters |
|
|
QTextLayout.SkipWords |
PySide2.QtGui.QTextLayout.
beginLayout
(
)
¶
Begins the layout process.
警告
This will invalidate the layout, so all existing
QTextLine
objects that refer to the previous contents should now be discarded.
另请参阅
PySide2.QtGui.QTextLayout.
boundingRect
(
)
¶
QRectF
The smallest rectangle that contains all the lines in the layout.
PySide2.QtGui.QTextLayout.
cacheEnabled
(
)
¶
bool
返回
true
if the complete layout information is cached; otherwise returns
false
.
另请参阅
PySide2.QtGui.QTextLayout.
clearAdditionalFormats
(
)
¶
注意
此函数被弃用。
使用
clearFormats()
代替。
PySide2.QtGui.QTextLayout.
clearFormats
(
)
¶
Clears the list of additional formats supported by the text layout.
另请参阅
PySide2.QtGui.QTextLayout.
clearLayout
(
)
¶
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.
PySide2.QtGui.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
setLineWidth()
函数被调用。
一旦
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).
PySide2.QtGui.QTextLayout.
cursorMoveStyle
(
)
¶
CursorMoveStyle
The cursor movement style of this
QTextLayout
。默认为
LogicalMoveStyle
.
另请参阅
PySide2.QtGui.QTextLayout.
draw
(
p
,
pos
[
,
selections=list()
[
,
clip=QRectF()
]
]
)
¶
p
–
QPainter
pos
–
QPointF
selections –
clip
–
QRectF
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
.
PySide2.QtGui.QTextLayout.
drawCursor
(
p
,
pos
,
cursorPosition
)
¶
p
–
QPainter
pos
–
QPointF
cursorPosition
–
int
这是重载函数。
Draws a text cursor with the current pen at the given
position
使用
painter
specified. The corresponding position within the text is specified by
cursorPosition
.
PySide2.QtGui.QTextLayout.
drawCursor
(
p
,
pos
,
cursorPosition
,
width
)
¶
p
–
QPainter
pos
–
QPointF
cursorPosition
–
int
width
–
int
Draws a text cursor with the current pen and the specified
width
at the given
position
使用
painter
specified. The corresponding position within the text is specified by
cursorPosition
.
PySide2.QtGui.QTextLayout.
endLayout
(
)
¶
Ends the layout process.
另请参阅
PySide2.QtGui.QTextLayout.
font
(
)
¶
Returns the current font that is used for the layout, or a default font if none is set.
另请参阅
PySide2.QtGui.QTextLayout.
formats
(
)
¶
Returns the list of additional formats supported by the text layout.
PySide2.QtGui.QTextLayout.
isValidCursorPosition
(
pos
)
¶
pos
–
int
bool
/ Returns
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 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.
PySide2.QtGui.QTextLayout.
leftCursorPosition
(
oldPos
)
¶
oldPos
–
int
int
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.
PySide2.QtGui.QTextLayout.
lineAt
(
i
)
¶
i
–
int
返回
i
-th line of text in this text layout.
PySide2.QtGui.QTextLayout.
lineCount
(
)
¶
int
Returns the number of lines in this text layout.
另请参阅
PySide2.QtGui.QTextLayout.
lineForTextPosition
(
pos
)
¶
pos
–
int
Returns the line that contains the cursor position specified by
pos
.
PySide2.QtGui.QTextLayout.
maximumWidth
(
)
¶
qreal
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.
另请参阅
PySide2.QtGui.QTextLayout.
minimumWidth
(
)
¶
qreal
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.
另请参阅
PySide2.QtGui.QTextLayout.
nextCursorPosition
(
oldPos
[
,
mode=SkipCharacters
]
)
¶
oldPos
–
int
mode
–
CursorMode
int
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.
PySide2.QtGui.QTextLayout.
position
(
)
¶
QPointF
The global position of the layout. This is independent of the bounding rectangle and of the layout process.
另请参阅
PySide2.QtGui.QTextLayout.
preeditAreaPosition
(
)
¶
int
Returns the position of the area in the text layout that will be processed before editing occurs.
另请参阅
PySide2.QtGui.QTextLayout.
preeditAreaText
(
)
¶
unicode
Returns the text that is inserted in the layout before editing occurs.
PySide2.QtGui.QTextLayout.
previousCursorPosition
(
oldPos
[
,
mode=SkipCharacters
]
)
¶
oldPos
–
int
mode
–
CursorMode
int
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.
PySide2.QtGui.QTextLayout.
rightCursorPosition
(
oldPos
)
¶
oldPos
–
int
int
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.
PySide2.QtGui.QTextLayout.
setAdditionalFormats
(
overrides
)
¶
overrides –
注意
此函数被弃用。
使用
setFormats()
代替。
另请参阅
PySide2.QtGui.QTextLayout.
setCacheEnabled
(
enable
)
¶
enable
–
bool
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.
另请参阅
PySide2.QtGui.QTextLayout.
setCursorMoveStyle
(
style
)
¶
style
–
CursorMoveStyle
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
LogicalMoveStyle
.
另请参阅
PySide2.QtGui.QTextLayout.
setFlags
(
flags
)
¶
flags
–
int
PySide2.QtGui.QTextLayout.
setFont
(
f
)
¶
f
–
QFont
Sets the layout’s font to the given
font
. The layout is invalidated and must be laid out again.
另请参阅
PySide2.QtGui.QTextLayout.
setFormats
(
overrides
)
¶
overrides –
Sets the additional formats supported by the text layout to
formats
. The formats are applied with preedit area text in place.
另请参阅
PySide2.QtGui.QTextLayout.
setPosition
(
p
)
¶
p
–
QPointF
Moves the text layout to point
p
.
另请参阅
PySide2.QtGui.QTextLayout.
setPreeditArea
(
position
,
text
)
¶
position
–
int
text – unicode
设置
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.
PySide2.QtGui.QTextLayout.
setRawFont
(
rawFont
)
¶
rawFont
–
QRawFont
Sets a raw font, to be used with
glyphRuns
. Note that this only supports the needs of WebKit. Use of this function with e.g.
draw
will result in undefined behaviour.
PySide2.QtGui.QTextLayout.
setText
(
string
)
¶
string – unicode
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.
另请参阅
PySide2.QtGui.QTextLayout.
setTextOption
(
option
)
¶
option
–
QTextOption
Sets the text option structure that controls the layout process to the given
option
.
另请参阅
PySide2.QtGui.QTextLayout.
textOption
(
)
¶
Returns the current text option used to control the layout process.
另请参阅