QGraphicsTextItem 类

The QGraphicsTextItem class provides a text item that you can add to a QGraphicsScene 以显示格式化文本。 更多...

头: #include <QGraphicsTextItem>
qmake: QT += widgets
Since: Qt 4.2
继承: QGraphicsObject

特性

公共函数

QGraphicsTextItem (QGraphicsItem * parent = Q_NULLPTR)
QGraphicsTextItem (const QString & text , QGraphicsItem * parent = Q_NULLPTR)
~QGraphicsTextItem ()
void adjustSize ()
QColor defaultTextColor () const
QTextDocument * document () const
QFont font () const
bool openExternalLinks () const
void setDefaultTextColor (const QColor & col )
void setDocument (QTextDocument * document )
void setFont (const QFont & font )
void setHtml (const QString & text )
void setOpenExternalLinks (bool open )
void setPlainText (const QString & text )
void setTabChangesFocus (bool b )
void setTextCursor (const QTextCursor & cursor )
void setTextInteractionFlags (Qt::TextInteractionFlags flags )
void setTextWidth (qreal width )
bool tabChangesFocus () const
QTextCursor textCursor () const
Qt::TextInteractionFlags textInteractionFlags () const
qreal textWidth () const
QString toHtml () const
QString toPlainText () const

重实现公共函数

virtual QRectF boundingRect () const
virtual bool contains (const QPointF & point ) const
virtual bool isObscuredBy (const QGraphicsItem * item ) const
virtual QPainterPath opaqueArea () const
virtual void paint (QPainter * painter , const QStyleOptionGraphicsItem * option , QWidget * widget )
virtual QPainterPath shape () const
virtual int type () const

信号

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

重实现保护函数

virtual void contextMenuEvent (QGraphicsSceneContextMenuEvent * event )
virtual void dragEnterEvent (QGraphicsSceneDragDropEvent * event )
virtual void dragLeaveEvent (QGraphicsSceneDragDropEvent * event )
virtual void dragMoveEvent (QGraphicsSceneDragDropEvent * event )
virtual void dropEvent (QGraphicsSceneDragDropEvent * event )
virtual void focusInEvent (QFocusEvent * event )
virtual void focusOutEvent (QFocusEvent * event )
virtual void hoverEnterEvent (QGraphicsSceneHoverEvent * event )
virtual void hoverLeaveEvent (QGraphicsSceneHoverEvent * event )
virtual void hoverMoveEvent (QGraphicsSceneHoverEvent * event )
virtual void inputMethodEvent (QInputMethodEvent * event )
virtual QVariant inputMethodQuery (Qt::InputMethodQuery query ) const
virtual void keyPressEvent (QKeyEvent * event )
virtual void keyReleaseEvent (QKeyEvent * event )
virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent * event )
virtual void mouseMoveEvent (QGraphicsSceneMouseEvent * event )
virtual void mousePressEvent (QGraphicsSceneMouseEvent * event )
virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent * event )
virtual bool sceneEvent (QEvent * event )

额外继承成员

详细描述

The QGraphicsTextItem class provides a text item that you can add to a QGraphicsScene 以显示格式化文本。

若只需要在项中展示纯文本,考虑使用 QGraphicsSimpleTextItem 代替。

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 (),和 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 接受 悬停事件 by default. You can change this with setAcceptHoverEvents() .

另请参阅 QGraphicsSimpleTextItem , QGraphicsPathItem , QGraphicsRectItem , QGraphicsEllipseItem , QGraphicsPixmapItem , QGraphicsPolygonItem , QGraphicsLineItem ,和 图形视图框架 .

特性文档编制

指定是否 QGraphicsTextItem 应自动打开链接使用 QDesktopServices::openUrl () 而不是发射 linkActivated 信号。

默认值为 false。

访问函数:

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.

访问函数:

QTextCursor textCursor () const
void setTextCursor (const QTextCursor & cursor )

成员函数文档编制

QGraphicsTextItem:: QGraphicsTextItem ( QGraphicsItem * parent = Q_NULLPTR)

构造 QGraphicsTextItem . parent 会被传递给 QGraphicsItem 的构造函数。

另请参阅 QGraphicsScene::addItem ().

QGraphicsTextItem:: QGraphicsTextItem (const QString & text , QGraphicsItem * parent = Q_NULLPTR)

构造 QGraphicsTextItem ,使用 text 作为默认纯文本。 parent 会被传递给 QGraphicsItem 的构造函数。

另请参阅 QGraphicsScene::addItem ().

QGraphicsTextItem:: ~QGraphicsTextItem ()

销毁 QGraphicsTextItem .

void QGraphicsTextItem:: adjustSize ()

Adjusts the text item to a reasonable size.

[虚拟] QRectF QGraphicsTextItem:: boundingRect () const

重实现自 QGraphicsItem::boundingRect ().

[虚拟] bool QGraphicsTextItem:: contains (const QPointF & point ) const

重实现自 QGraphicsItem::contains ().

[virtual protected] void QGraphicsTextItem:: contextMenuEvent ( QGraphicsSceneContextMenuEvent * event )

重实现自 QGraphicsItem::contextMenuEvent ().

QColor QGraphicsTextItem:: defaultTextColor () const

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

另请参阅 setDefaultTextColor ().

QTextDocument *QGraphicsTextItem:: document () const

返回项的文本文档。

另请参阅 setDocument ().

[virtual protected] void QGraphicsTextItem:: dragEnterEvent ( QGraphicsSceneDragDropEvent * event )

重实现自 QGraphicsItem::dragEnterEvent ().

[virtual protected] void QGraphicsTextItem:: dragLeaveEvent ( QGraphicsSceneDragDropEvent * event )

重实现自 QGraphicsItem::dragLeaveEvent ().

[virtual protected] void QGraphicsTextItem:: dragMoveEvent ( QGraphicsSceneDragDropEvent * event )

重实现自 QGraphicsItem::dragMoveEvent ().

[virtual protected] void QGraphicsTextItem:: dropEvent ( QGraphicsSceneDragDropEvent * event )

重实现自 QGraphicsItem::dropEvent ().

[virtual protected] void QGraphicsTextItem:: focusInEvent ( QFocusEvent * event )

重实现自 QGraphicsItem::focusInEvent ().

[virtual protected] void QGraphicsTextItem:: focusOutEvent ( QFocusEvent * event )

重实现自 QGraphicsItem::focusOutEvent ().

QFont QGraphicsTextItem:: font () const

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

另请参阅 setFont ().

[virtual protected] void QGraphicsTextItem:: hoverEnterEvent ( QGraphicsSceneHoverEvent * event )

重实现自 QGraphicsItem::hoverEnterEvent ().

[virtual protected] void QGraphicsTextItem:: hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )

重实现自 QGraphicsItem::hoverLeaveEvent ().

[virtual protected] void QGraphicsTextItem:: hoverMoveEvent ( QGraphicsSceneHoverEvent * event )

重实现自 QGraphicsItem::hoverMoveEvent ().

[virtual protected] void QGraphicsTextItem:: inputMethodEvent ( QInputMethodEvent * event )

重实现自 QGraphicsItem::inputMethodEvent ().

[virtual protected] QVariant QGraphicsTextItem:: inputMethodQuery ( Qt::InputMethodQuery query ) const

重实现自 QGraphicsItem::inputMethodQuery ().

[虚拟] bool QGraphicsTextItem:: isObscuredBy (const QGraphicsItem * item ) const

重实现自 QGraphicsItem::isObscuredBy ().

[virtual protected] void QGraphicsTextItem:: keyPressEvent ( QKeyEvent * event )

重实现自 QGraphicsItem::keyPressEvent ().

[virtual protected] void QGraphicsTextItem:: keyReleaseEvent ( QKeyEvent * event )

重实现自 QGraphicsItem::keyReleaseEvent ().

[signal] 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.

另请参阅 setTextInteractionFlags ().

[signal] 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.

另请参阅 setTextInteractionFlags ().

[virtual protected] void QGraphicsTextItem:: mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event )

重实现自 QGraphicsItem::mouseDoubleClickEvent ().

[virtual protected] void QGraphicsTextItem:: mouseMoveEvent ( QGraphicsSceneMouseEvent * event )

重实现自 QGraphicsItem::mouseMoveEvent ().

[virtual protected] void QGraphicsTextItem:: mousePressEvent ( QGraphicsSceneMouseEvent * event )

重实现自 QGraphicsItem::mousePressEvent ().

[virtual protected] void QGraphicsTextItem:: mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )

重实现自 QGraphicsItem::mouseReleaseEvent ().

[虚拟] QPainterPath QGraphicsTextItem:: opaqueArea () const

重实现自 QGraphicsItem::opaqueArea ().

[虚拟] void QGraphicsTextItem:: paint ( QPainter * painter , const QStyleOptionGraphicsItem * option , QWidget * widget )

重实现自 QGraphicsItem::paint ().

[virtual protected] bool QGraphicsTextItem:: sceneEvent ( QEvent * event )

重实现自 QGraphicsItem::sceneEvent ().

void QGraphicsTextItem:: setDefaultTextColor (const QColor & col )

Sets the color for unformatted text to col .

另请参阅 defaultTextColor ().

void QGraphicsTextItem:: setDocument ( QTextDocument * document )

Sets the text document document on the item.

另请参阅 document ().

void QGraphicsTextItem:: setFont (const QFont & font )

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

另请参阅 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.

另请参阅 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.

另请参阅 toHtml () 和 hasFocus ().

void QGraphicsTextItem:: setTabChangesFocus ( bool b )

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.

该函数在 Qt 4.5 引入。

另请参阅 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 标志。

另请参阅 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.

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.

默认值为 -1。

注意, QGraphicsTextItem keeps a QTextDocument internally, which is used to calculate the text width.

另请参阅 textWidth () 和 QTextDocument::setTextWidth ().

[虚拟] QPainterPath QGraphicsTextItem:: shape () const

重实现自 QGraphicsItem::shape ().

bool QGraphicsTextItem:: tabChangesFocus () const

返回 true 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.

该函数在 Qt 4.5 引入。

另请参阅 setTabChangesFocus ().

Qt::TextInteractionFlags QGraphicsTextItem:: textInteractionFlags () const

Returns the current text interaction flags.

另请参阅 setTextInteractionFlags ().

qreal QGraphicsTextItem:: textWidth () const

Returns the text width.

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

另请参阅 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.

另请参阅 setHtml ().

QString QGraphicsTextItem:: toPlainText () const

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

另请参阅 setPlainText ().

[虚拟] int QGraphicsTextItem:: type () const

重实现自 QGraphicsItem::type ().