QGraphicsTextItem

The QGraphicsTextItem class provides a text item that you can add to a QGraphicsScene to display formatted text. More

Inheritance diagram of PySide2.QtWidgets.QGraphicsTextItem

Synopsis

Functions

Virtual functions

Signals

Detailed Description

If you only need to show plain text in an item, consider using QGraphicsSimpleTextItem instead.

To set the item’s text, pass a QString to QGraphicsTextItem ‘s constructor, or call setHtml() / setPlainText() .

QGraphicsTextItem uses the text’s formatted size and the associated font to provide a reasonable implementation of boundingRect() , shape() , and contains() . You can set the font by calling setFont() .

It is possible to make the item editable by setting the TextEditorInteraction flag using setTextInteractionFlags() .

The item’s preferred text width can be set using setTextWidth() and obtained using textWidth() .

Note

In order to align HTML text in the center, the item’s text width must be set. Otherwise, you can call adjustSize() after setting the item’s text.

../../_images/graphicsview-textitem.png

Note

QGraphicsTextItem accepts hover events by default. You can change this with setAcceptHoverEvents() .

class PySide2.QtWidgets.QGraphicsTextItem([parent=None])

PySide2.QtWidgets.QGraphicsTextItem(text[, parent=None])

param parent:

PySide2.QtWidgets.QGraphicsItem

param text:

str

Constructs a QGraphicsTextItem . parent is passed to QGraphicsItem ‘s constructor.

See also

addItem()

Constructs a QGraphicsTextItem , using text as the default plain text. parent is passed to QGraphicsItem ‘s constructor.

See also

addItem()

PySide2.QtWidgets.QGraphicsTextItem.adjustSize()

Adjusts the text item to a reasonable size.

PySide2.QtWidgets.QGraphicsTextItem.defaultTextColor()
Return type:

PySide2.QtGui.QColor

Returns the default text color that is used for unformatted text.

PySide2.QtWidgets.QGraphicsTextItem.document()
Return type:

PySide2.QtGui.QTextDocument

Returns the item’s text document.

See also

setDocument()

PySide2.QtWidgets.QGraphicsTextItem.font()
Return type:

PySide2.QtGui.QFont

Returns the item’s font, which is used to render the text.

See also

setFont()

PySide2.QtWidgets.QGraphicsTextItem.linkActivated(arg__1)
Parameters:

arg__1 – str

PySide2.QtWidgets.QGraphicsTextItem.linkHovered(arg__1)
Parameters:

arg__1 – str

Return type:

bool

PySide2.QtWidgets.QGraphicsTextItem.setDefaultTextColor(c)
Parameters:

cPySide2.QtGui.QColor

Sets the color for unformatted text to col .

PySide2.QtWidgets.QGraphicsTextItem.setDocument(document)
Parameters:

documentPySide2.QtGui.QTextDocument

Sets the text document document on the item.

See also

document()

PySide2.QtWidgets.QGraphicsTextItem.setExtension(extension, variant)
Parameters:
PySide2.QtWidgets.QGraphicsTextItem.setFont(font)
Parameters:

fontPySide2.QtGui.QFont

Sets the font used to render the text item to font .

See also

font()

PySide2.QtWidgets.QGraphicsTextItem.setHtml(html)
Parameters:

html – str

Sets the item’s text to text , assuming that text is HTML formatted. If the item has keyboard input focus, this function will also call ensureVisible() to ensure that the text is visible in all viewports.

See also

toHtml() hasFocus() QGraphicsSimpleTextItem

Parameters:

open – bool

PySide2.QtWidgets.QGraphicsTextItem.setPlainText(text)
Parameters:

text – str

Sets the item’s text to text . If the item has keyboard input focus, this function will also call ensureVisible() to ensure that the text is visible in all viewports.

See also

toHtml() hasFocus()

PySide2.QtWidgets.QGraphicsTextItem.setTabChangesFocus(b)
Parameters:

b – bool

If b is true, the Tab key will cause the widget to change focus; otherwise, the tab key will insert a tab into the document.

In some occasions text edits should not allow the user to input tabulators or change indentation using the Tab key, as this breaks the focus chain. The default is false.

See also

tabChangesFocus() ItemIsFocusable textInteractionFlags()

PySide2.QtWidgets.QGraphicsTextItem.setTextCursor(cursor)
Parameters:

cursorPySide2.QtGui.QTextCursor

See also

textCursor()

PySide2.QtWidgets.QGraphicsTextItem.setTextInteractionFlags(flags)
Parameters:

flagsTextInteractionFlags

Sets the flags flags to specify how the text item should react to user input.

The default for a QGraphicsTextItem is NoTextInteraction . This function also affects the ItemIsFocusable QGraphicsItem flag by setting it if flags is different from NoTextInteraction and clearing it otherwise.

By default, the text is read-only. To transform the item into an editor, set the TextEditable flag.

PySide2.QtWidgets.QGraphicsTextItem.setTextWidth(width)
Parameters:

width – float

Sets the preferred width for the item’s text. If the actual text is wider than the specified width then it will be broken into multiple lines.

If width is set to -1 then the text will not be broken into multiple lines unless it is enforced through an explicit line break or a new paragraph.

The default value is -1.

Note that QGraphicsTextItem keeps a QTextDocument internally, which is used to calculate the text width.

See also

textWidth() setTextWidth()

PySide2.QtWidgets.QGraphicsTextItem.supportsExtension(extension)
Parameters:

extensionExtension

Return type:

bool

PySide2.QtWidgets.QGraphicsTextItem.tabChangesFocus()
Return type:

bool

Returns true if the Tab key will cause the widget to change focus; otherwise, false is returned.

By default, this behavior is disabled, and this function will return false.

PySide2.QtWidgets.QGraphicsTextItem.textCursor()
Return type:

PySide2.QtGui.QTextCursor

See also

setTextCursor()

PySide2.QtWidgets.QGraphicsTextItem.textInteractionFlags()
Return type:

TextInteractionFlags

Returns the current text interaction flags.

PySide2.QtWidgets.QGraphicsTextItem.textWidth()
Return type:

float

Returns the text width.

The width is calculated with the QTextDocument that QGraphicsTextItem keeps internally.

See also

setTextWidth() textWidth()

PySide2.QtWidgets.QGraphicsTextItem.toHtml()
Return type:

str

Returns the item’s text converted to HTML, or an empty QString if no text has been set.

See also

setHtml()

PySide2.QtWidgets.QGraphicsTextItem.toPlainText()
Return type:

str

Returns the item’s text converted to plain text, or an empty QString if no text has been set.

See also

setPlainText()