|  | Home · All Classes · Modules

# QGraphicsTextItem Class Reference
[QtGui module]

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

Inherits QGraphicsObject.

### Methods

- __init__ (self, QGraphicsItem parent = None, QGraphicsScene scene = None)
- __init__ (self, QString, QGraphicsItem parent = None, QGraphicsScene scene = None)
- adjustSize (self)
- QRectF boundingRect (self)
- bool contains (self, QPointF)
- contextMenuEvent (self, QGraphicsSceneContextMenuEvent)
- QColor defaultTextColor (self)
- QTextDocument document (self)
- dragEnterEvent (self, QGraphicsSceneDragDropEvent)
- dragLeaveEvent (self, QGraphicsSceneDragDropEvent)
- dragMoveEvent (self, QGraphicsSceneDragDropEvent)
- dropEvent (self, QGraphicsSceneDragDropEvent)
- focusInEvent (self, QFocusEvent)
- focusOutEvent (self, QFocusEvent)
- QFont font (self)
- hoverEnterEvent (self, QGraphicsSceneHoverEvent)
- hoverLeaveEvent (self, QGraphicsSceneHoverEvent)
- hoverMoveEvent (self, QGraphicsSceneHoverEvent)
- inputMethodEvent (self, QInputMethodEvent)
- QVariant inputMethodQuery (self, Qt.InputMethodQuery)
- bool isObscuredBy (self, QGraphicsItem)
- keyPressEvent (self, QKeyEvent)
- keyReleaseEvent (self, QKeyEvent)
- mouseDoubleClickEvent (self, QGraphicsSceneMouseEvent)
- mouseMoveEvent (self, QGraphicsSceneMouseEvent)
- mousePressEvent (self, QGraphicsSceneMouseEvent)
- mouseReleaseEvent (self, QGraphicsSceneMouseEvent)
- QPainterPath opaqueArea (self)
- bool openExternalLinks (self)
- paint (self, QPainter, QStyleOptionGraphicsItem, QWidget)
- bool sceneEvent (self, QEvent)
- setDefaultTextColor (self, QColor)
- setDocument (self, QTextDocument)
- setFont (self, QFont)
- setHtml (self, QString)
- setOpenExternalLinks (self, bool)
- setPlainText (self, QString)
- setTabChangesFocus (self, bool)
- setTextCursor (self, QTextCursor)
- setTextInteractionFlags (self, Qt.TextInteractionFlags)
- setTextWidth (self, float)
- QPainterPath shape (self)
- bool tabChangesFocus (self)
- QTextCursor textCursor (self)
- Qt.TextInteractionFlags textInteractionFlags (self)
- float textWidth (self)
- QString toHtml (self)
- QString toPlainText (self)
- int type (self)

### Qt Signals

- void linkActivated (const QString&)
- void linkHovered (const QString&)

## Detailed Description

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

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 Qt.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.

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

See also QGraphicsSimpleTextItem, QGraphicsPathItem, QGraphicsRectItem, QGraphicsEllipseItem, QGraphicsPixmapItem, QGraphicsPolygonItem, QGraphicsLineItem, and The Graphics View Framework.

## Method Documentation

### QGraphicsTextItem.__init__ (self, QGraphicsItem parent = None, QGraphicsScene scene = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

The scene argument, if not None, causes self to be owned by Qt instead of PyQt.

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

See also QGraphicsScene.addItem().

### QGraphicsTextItem.__init__ (self, QString, QGraphicsItem parent = None, QGraphicsScene scene = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

The scene argument, if not None, causes self to be owned by Qt instead of PyQt.

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

See also QGraphicsScene.addItem().

### QGraphicsTextItem.adjustSize (self)

Adjusts the text item to a reasonable size.

### QRectF QGraphicsTextItem.boundingRect (self)

Reimplemented from QGraphicsItem.boundingRect().

### bool QGraphicsTextItem.contains (self, QPointF)

Reimplemented from QGraphicsItem.contains().

### QGraphicsTextItem.contextMenuEvent (self, QGraphicsSceneContextMenuEvent)

Reimplemented from QGraphicsItem.contextMenuEvent().

### QColor QGraphicsTextItem.defaultTextColor (self)

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

See also setDefaultTextColor().

### QTextDocument QGraphicsTextItem.document (self)

Returns the item's text document.

See also setDocument().

### QGraphicsTextItem.dragEnterEvent (self, QGraphicsSceneDragDropEvent)

Reimplemented from QGraphicsItem.dragEnterEvent().

### QGraphicsTextItem.dragLeaveEvent (self, QGraphicsSceneDragDropEvent)

Reimplemented from QGraphicsItem.dragLeaveEvent().

### QGraphicsTextItem.dragMoveEvent (self, QGraphicsSceneDragDropEvent)

Reimplemented from QGraphicsItem.dragMoveEvent().

### QGraphicsTextItem.dropEvent (self, QGraphicsSceneDragDropEvent)

Reimplemented from QGraphicsItem.dropEvent().

### QGraphicsTextItem.focusInEvent (self, QFocusEvent)

Reimplemented from QGraphicsItem.focusInEvent().

### QGraphicsTextItem.focusOutEvent (self, QFocusEvent)

Reimplemented from QGraphicsItem.focusOutEvent().

### QFont QGraphicsTextItem.font (self)

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

See also setFont().

### QGraphicsTextItem.hoverEnterEvent (self, QGraphicsSceneHoverEvent)

Reimplemented from QGraphicsItem.hoverEnterEvent().

### QGraphicsTextItem.hoverLeaveEvent (self, QGraphicsSceneHoverEvent)

Reimplemented from QGraphicsItem.hoverLeaveEvent().

### QGraphicsTextItem.hoverMoveEvent (self, QGraphicsSceneHoverEvent)

Reimplemented from QGraphicsItem.hoverMoveEvent().

### QGraphicsTextItem.inputMethodEvent (self, QInputMethodEvent)

Reimplemented from QGraphicsItem.inputMethodEvent().

### QVariant QGraphicsTextItem.inputMethodQuery (self, Qt.InputMethodQuery)

Reimplemented from QGraphicsItem.inputMethodQuery().

### bool QGraphicsTextItem.isObscuredBy (self, QGraphicsItem)

Reimplemented from QGraphicsItem.isObscuredBy().

### QGraphicsTextItem.keyPressEvent (self, QKeyEvent)

Reimplemented from QGraphicsItem.keyPressEvent().

### QGraphicsTextItem.keyReleaseEvent (self, QKeyEvent)

Reimplemented from QGraphicsItem.keyReleaseEvent().

### QGraphicsTextItem.mouseDoubleClickEvent (self, QGraphicsSceneMouseEvent)

Reimplemented from QGraphicsItem.mouseDoubleClickEvent().

### QGraphicsTextItem.mouseMoveEvent (self, QGraphicsSceneMouseEvent)

Reimplemented from QGraphicsItem.mouseMoveEvent().

### QGraphicsTextItem.mousePressEvent (self, QGraphicsSceneMouseEvent)

Reimplemented from QGraphicsItem.mousePressEvent().

### QGraphicsTextItem.mouseReleaseEvent (self, QGraphicsSceneMouseEvent)

Reimplemented from QGraphicsItem.mouseReleaseEvent().

### QPainterPath QGraphicsTextItem.opaqueArea (self)

Reimplemented from QGraphicsItem.opaqueArea().

### bool QGraphicsTextItem.openExternalLinks (self)

### QGraphicsTextItem.paint (self, QPainter, QStyleOptionGraphicsItem, QWidget)

Reimplemented from QGraphicsItem.paint().

### bool QGraphicsTextItem.sceneEvent (self, QEvent)

Reimplemented from QGraphicsItem.sceneEvent().

### QGraphicsTextItem.setDefaultTextColor (self, QColor)

Sets the color for unformatted text to col.

See also defaultTextColor().

### QGraphicsTextItem.setDocument (self, QTextDocument)

Sets the text document document on the item.

See also document().

### QGraphicsTextItem.setFont (self, QFont)

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

See also font().

### QGraphicsTextItem.setHtml (self, QString)

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(), and QGraphicsSimpleTextItem.

### QGraphicsTextItem.setOpenExternalLinks (self, bool)

### QGraphicsTextItem.setPlainText (self, QString)

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() and hasFocus().

### QGraphicsTextItem.setTabChangesFocus (self, 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.

This function was introduced in Qt 4.5.

See also tabChangesFocus(), ItemIsFocusable, and textInteractionFlags().

### QGraphicsTextItem.setTextCursor (self, QTextCursor)

### QGraphicsTextItem.setTextInteractionFlags (self, Qt.TextInteractionFlags)

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

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

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

See also textInteractionFlags().

### QGraphicsTextItem.setTextWidth (self, 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() and QTextDocument.setTextWidth().

### QPainterPath QGraphicsTextItem.shape (self)

Reimplemented from QGraphicsItem.shape().

### bool QGraphicsTextItem.tabChangesFocus (self)

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.

This function was introduced in Qt 4.5.

See also setTabChangesFocus().

### QTextCursor QGraphicsTextItem.textCursor (self)

### Qt.TextInteractionFlags QGraphicsTextItem.textInteractionFlags (self)

Returns the current text interaction flags.

See also setTextInteractionFlags().

### float QGraphicsTextItem.textWidth (self)

Returns the text width.

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

See also setTextWidth() and QTextDocument.textWidth().

### QString QGraphicsTextItem.toHtml (self)

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

See also setHtml().

### QString QGraphicsTextItem.toPlainText (self)

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

See also setPlainText().

### int QGraphicsTextItem.type (self)

Reimplemented from QGraphicsItem.type().

## Qt Signal Documentation

### void linkActivated (const QString&)

This is the default overload of this signal.

This signal is emitted when the user clicks on a link on a text item that enables Qt.LinksAccessibleByMouse or Qt.LinksAccessibleByKeyboard. link is the link that was clicked.

See also setTextInteractionFlags().

### void linkHovered (const QString&)

This is the default overload of this signal.

This signal is emitted when the user hovers over a link on a text item that enables Qt.LinksAccessibleByMouse. link is the link that was hovered over.

See also setTextInteractionFlags().