• PySide 模块
  • PySide.QtGui
  • 内容表

    上一话题

    QPicture

    下一话题

    QTextLayout.FormatRange

    QTextLayout

    概要

    函数

    详细描述

    PySide.QtGui.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 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.

    PySide.QtGui.QTextLayout can be used with both plain and rich text.

    PySide.QtGui.QTextLayout can be used to create a sequence of PySide.QtGui.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 PySide.QtGui.QTextLayout.setText() .

    The layout can be seen as a sequence of PySide.QtGui.QTextLine objects; use PySide.QtGui.QTextLayout.createLine() to create a PySide.QtGui.QTextLine instance, and PySide.QtGui.QTextLayout.lineAt() or PySide.QtGui.QTextLayout.lineForTextPosition() 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 PySide.QtGui.QTextLayout.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 PySide.QtGui.QTextLayout.isValidCursorPosition() , PySide.QtGui.QTextLayout.nextCursorPosition() ,和 PySide.QtGui.QTextLayout.previousCursorPosition() .

    PySide.QtGui.QTextLayout itself can be positioned with PySide.QtGui.QTextLayout.setPosition() ; it has a PySide.QtGui.QTextLayout.boundingRect() ,和 PySide.QtGui.QTextLayout.minimumWidth() PySide.QtGui.QTextLayout.maximumWidth() .

    另请参阅

    QStaticText

    class PySide.QtGui. QTextLayout
    class PySide.QtGui. QTextLayout ( text )
    class PySide.QtGui. QTextLayout ( text , font [ , paintdevice=None ] )
    class PySide.QtGui. QTextLayout ( b )
    参数:

    Constructs an empty text layout.

    Constructs a text layout to lay out the given text .

    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 paint device, paintdevice 。若 paintdevice is 0 the calculations will be done in screen metrics.

    Constructs a text layout to lay out the given block .

    PySide.QtGui.QTextLayout. CursorMode
    常量 描述
    QTextLayout.SkipCharacters  
    QTextLayout.SkipWords  
    PySide.QtGui.QTextLayout. additionalFormats ( )
    返回类型:

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

    PySide.QtGui.QTextLayout. beginLayout ( )

    Begins the layout process.

    PySide.QtGui.QTextLayout. boundingRect ( )
    返回类型: PySide.QtCore.QRectF

    The smallest rectangle that contains all the lines in the layout.

    PySide.QtGui.QTextLayout. cacheEnabled ( )
    返回类型: PySide.QtCore.bool

    Returns true if the complete layout information is cached; otherwise returns false.

    PySide.QtGui.QTextLayout. clearAdditionalFormats ( )

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

    PySide.QtGui.QTextLayout. clearLayout ( )

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

    PySide.QtGui.QTextLayout. createLine ( )
    返回类型: PySide.QtGui.QTextLine

    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() 函数被调用。

    一旦 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 PySide.QtGui.QTextLayout . If there is no text left to be inserted into the layout, the PySide.QtGui.QTextLine returned will not be valid ( isValid() will return false).

    PySide.QtGui.QTextLayout. cursorMoveStyle ( )
    返回类型: PySide.QtCore.Qt.CursorMoveStyle

    The cursor movement style of this PySide.QtGui.QTextLayout 。默认为 Qt.LogicalMoveStyle .

    PySide.QtGui.QTextLayout. draw ( p , pos [ , selections=list() [ , clip=QRectF() ] ] )
    参数:
    PySide.QtGui.QTextLayout. drawCursor ( p , pos , cursorPosition )
    参数:

    这是重载函数。

    Draws a text cursor with the current pen at the given position 使用 painter specified. The corresponding position within the text is specified by cursorPosition .

    PySide.QtGui.QTextLayout. drawCursor ( p , pos , cursorPosition , width )
    参数:

    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 .

    PySide.QtGui.QTextLayout. endLayout ( )

    Ends the layout process.

    PySide.QtGui.QTextLayout. font ( )
    返回类型: PySide.QtGui.QFont

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

    PySide.QtGui.QTextLayout. isValidCursorPosition ( pos )
    参数: pos PySide.QtCore.int
    返回类型: PySide.QtCore.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 ` .. raw:: html Ä ‘ 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.

    PySide.QtGui.QTextLayout. leftCursorPosition ( oldPos )
    参数: oldPos PySide.QtCore.int
    返回类型: PySide.QtCore.int

    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.

    PySide.QtGui.QTextLayout. lineAt ( i )
    参数: i PySide.QtCore.int
    返回类型: PySide.QtGui.QTextLine

    返回 i -th line of text in this text layout.

    PySide.QtGui.QTextLayout. lineCount ( )
    返回类型: PySide.QtCore.int

    Returns the number of lines in this text layout.

    PySide.QtGui.QTextLayout. lineForTextPosition ( pos )
    参数: pos PySide.QtCore.int
    返回类型: PySide.QtGui.QTextLine

    Returns the line that contains the cursor position specified by pos .

    PySide.QtGui.QTextLayout. maximumWidth ( )
    返回类型: PySide.QtCore.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.

    PySide.QtGui.QTextLayout. minimumWidth ( )
    返回类型: PySide.QtCore.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.

    PySide.QtGui.QTextLayout. nextCursorPosition ( oldPos [ , mode=SkipCharacters ] )
    参数:
    返回类型:

    PySide.QtCore.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.

    PySide.QtGui.QTextLayout. position ( )
    返回类型: PySide.QtCore.QPointF

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

    PySide.QtGui.QTextLayout. preeditAreaPosition ( )
    返回类型: PySide.QtCore.int

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

    PySide.QtGui.QTextLayout. preeditAreaText ( )
    返回类型: unicode

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

    PySide.QtGui.QTextLayout. previousCursorPosition ( oldPos [ , mode=SkipCharacters ] )
    参数:
    返回类型:

    PySide.QtCore.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.

    PySide.QtGui.QTextLayout. rightCursorPosition ( oldPos )
    参数: oldPos PySide.QtCore.int
    返回类型: PySide.QtCore.int

    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.

    PySide.QtGui.QTextLayout. setAdditionalFormats ( overrides )
    参数: overrides
    PySide.QtGui.QTextLayout. setCacheEnabled ( enable )
    参数: enable PySide.QtCore.bool

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

    PySide.QtGui.QTextLayout. setCursorMoveStyle ( style )
    参数: style PySide.QtCore.Qt.CursorMoveStyle
    PySide.QtGui.QTextLayout. setFlags ( flags )
    参数: flags PySide.QtCore.int
    PySide.QtGui.QTextLayout. setFont ( f )
    参数: f PySide.QtGui.QFont

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

    PySide.QtGui.QTextLayout. setPosition ( p )
    参数: p PySide.QtCore.QPointF

    Moves the text layout to point p .

    PySide.QtGui.QTextLayout. setPreeditArea ( position , text )
    参数:
    • position PySide.QtCore.int
    • text – unicode

    设置 position and text of the area in the layout that is processed before editing occurs.

    PySide.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 PySide.QtGui.QTextLayout as part of a PySide.QtGui.QTextDocument this method will have no effect.

    PySide.QtGui.QTextLayout. setTextOption ( option )
    参数: option PySide.QtGui.QTextOption

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

    PySide.QtGui.QTextLayout. text ( )
    返回类型: unicode

    Returns the layout's text.

    PySide.QtGui.QTextLayout. textOption ( )
    返回类型: PySide.QtGui.QTextOption

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