QGraphicsTextItem Class▲
-
Header: QGraphicsTextItem
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
-
qmake: QT += widgets
-
Inherits: QGraphicsObject
-
Inherited By:
-
Group: QGraphicsTextItem is part of graphicsview-api
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 Qt::TextEditorInteraction flag using setTextInteractionFlags().
The item's preferred text width can be set using setTextWidth() and obtained using textWidth().
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.
QGraphicsTextItem accepts hover events by default. You can change this with setAcceptHoverEvents().
See Also▲
Member Type Documentation▲
enum QGraphicsTextItem::anonymous▲
The value returned by the virtual type() function.
Constant |
Value |
Description |
---|---|---|
QGraphicsTextItem::Type |
8 |
A graphics text item |
Property Documentation▲
openExternalLinks : bool▲
Specifies whether QGraphicsTextItem should automatically open links using QDesktopServices::openUrl() instead of emitting the linkActivated signal.
The default value is false.
Access functions:
-
bool openExternalLinks() const
-
void setOpenExternalLinks(bool open)
textCursor : QTextCursor▲
This property represents the visible text cursor in an editable text item.
By default, if the item's text has not been set, this property contains a null text cursor; otherwise it contains a text cursor placed at the start of the item's document.
Access functions:
-
textCursor() const
-
void setTextCursor(const &cursor)
Member Function Documentation▲
[explicit] QGraphicsTextItem::QGraphicsTextItem(QGraphicsItem *parent = nullptr)▲
Constructs a QGraphicsTextItem. parent is passed to QGraphicsItem's constructor.
See Also▲
See also QGraphicsScene::addItem()
[explicit] QGraphicsTextItem::QGraphicsTextItem(const QString &text, QGraphicsItem *parent = nullptr)▲
Constructs a QGraphicsTextItem, using text as the default plain text. parent is passed to QGraphicsItem's constructor.
See Also▲
See also QGraphicsScene::addItem()
[virtual] QGraphicsTextItem::~QGraphicsTextItem()▲
Destroys the QGraphicsTextItem.
void QGraphicsTextItem::adjustSize()▲
Adjusts the text item to a reasonable size.
[override virtual] QRectF QGraphicsTextItem::boundingRect() const▲
Reimplements: QGraphicsItem::boundingRect() const.
[override virtual] bool QGraphicsTextItem::contains(const QPointF &point) const▲
Reimplements: QGraphicsItem::contains(const QPointF &point) const.
[override virtual protected] void QGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)▲
QColor QGraphicsTextItem::defaultTextColor() const▲
Returns the default text color that is used for unformatted text.
See Also▲
See also setDefaultTextColor()
QTextDocument *QGraphicsTextItem::document() const▲
[override virtual protected] void QGraphicsTextItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)▲
[override virtual protected] void QGraphicsTextItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)▲
[override virtual protected] void QGraphicsTextItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event)▲
[override virtual protected] void QGraphicsTextItem::dropEvent(QGraphicsSceneDragDropEvent *event)▲
Reimplements: QGraphicsItem::dropEvent(QGraphicsSceneDragDropEvent *event).
[override virtual protected] void QGraphicsTextItem::focusInEvent(QFocusEvent *event)▲
Reimplements: QGraphicsItem::focusInEvent(QFocusEvent *event).
[override virtual protected] void QGraphicsTextItem::focusOutEvent(QFocusEvent *event)▲
Reimplements: QGraphicsItem::focusOutEvent(QFocusEvent *event).
QFont QGraphicsTextItem::font() const▲
[override virtual protected] void QGraphicsTextItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)▲
[override virtual protected] void QGraphicsTextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)▲
[override virtual protected] void QGraphicsTextItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)▲
[override virtual protected] void QGraphicsTextItem::inputMethodEvent(QInputMethodEvent *event)▲
Reimplements: QGraphicsItem::inputMethodEvent(QInputMethodEvent *event).
[override virtual protected] QVariant QGraphicsTextItem::inputMethodQuery(Qt::InputMethodQuery query) const▲
[override virtual] bool QGraphicsTextItem::isObscuredBy(const QGraphicsItem *item) const▲
Reimplements: QGraphicsItem::isObscuredBy(const QGraphicsItem *item) const.
[override virtual protected] void QGraphicsTextItem::keyPressEvent(QKeyEvent *event)▲
Reimplements: QGraphicsItem::keyPressEvent(QKeyEvent *event).
[override virtual protected] void QGraphicsTextItem::keyReleaseEvent(QKeyEvent *event)▲
Reimplements: QGraphicsItem::keyReleaseEvent(QKeyEvent *event).
void QGraphicsTextItem::linkActivated(const QString &link)▲
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▲
See also setTextInteractionFlags()
void QGraphicsTextItem::linkHovered(const QString &link)▲
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▲
See also setTextInteractionFlags()
[override virtual protected] void QGraphicsTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)▲
[override virtual protected] void QGraphicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)▲
[override virtual protected] void QGraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)▲
[override virtual protected] void QGraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)▲
[override virtual] QPainterPath QGraphicsTextItem::opaqueArea() const▲
Reimplements: QGraphicsItem::opaqueArea() const.
[override virtual] void QGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)▲
Reimplements: QGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget).
[override virtual protected] bool QGraphicsTextItem::sceneEvent(QEvent *event)▲
Reimplements: QGraphicsItem::sceneEvent(QEvent *event).
void QGraphicsTextItem::setDefaultTextColor(const QColor &col)▲
void QGraphicsTextItem::setDocument(QTextDocument *document)▲
void QGraphicsTextItem::setFont(const QFont &font)▲
void QGraphicsTextItem::setHtml(const QString &text)▲
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▲
See also toHtml(), hasFocus(), QGraphicsSimpleTextItem
void QGraphicsTextItem::setPlainText(const QString &text)▲
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▲
void QGraphicsTextItem::setTabChangesFocus(bool b)▲
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▲
See also tabChangesFocus(), ItemIsFocusable, textInteractionFlags()
void QGraphicsTextItem::setTextInteractionFlags(Qt::TextInteractionFlags flags)▲
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▲
See also textInteractionFlags()
void QGraphicsTextItem::setTextWidth(qreal width)▲
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▲
See also textWidth(), QTextDocument::setTextWidth()
[override virtual] QPainterPath QGraphicsTextItem::shape() const▲
Reimplements: QGraphicsItem::shape() const.
bool QGraphicsTextItem::tabChangesFocus() const▲
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.
See Also▲
See also setTabChangesFocus()
Qt::TextInteractionFlags QGraphicsTextItem::textInteractionFlags() const▲
qreal QGraphicsTextItem::textWidth() const▲
Returns the text width.
The width is calculated with the QTextDocument that QGraphicsTextItem keeps internally.
See Also▲
See also setTextWidth(), QTextDocument::textWidth()
QString QGraphicsTextItem::toHtml() const▲
Returns the item's text converted to HTML, or an empty QString if no text has been set.
See Also▲
See also setHtml()
QString QGraphicsTextItem::toPlainText() const▲
Returns the item's text converted to plain text, or an empty QString if no text has been set.
See Also▲
See also setPlainText()
[override virtual] int QGraphicsTextItem::type() const▲
Reimplements: QGraphicsItem::type() const.