QCPItemText Class Reference
Author: Emanuel Eichhammer

· · · ·
Public Functions | Public Members | Protected Types | Protected Functions
QCPItemText Class Reference

A text label. More...

Inheritance diagram for QCPItemText:

## Public Functions
| QCPItemText (QCustomPlot *parentPlot)
QColor | color () const
QColor | selectedColor () const
QPen | pen () const
QPen | selectedPen () const
QBrush | brush () const
QBrush | selectedBrush () const
QFont | font () const
QFont | selectedFont () const
QString | text () const
Qt::Alignment | positionAlignment () const
Qt::Alignment | textAlignment () const
double | rotation () const
QMargins | padding () const
void | setColor (const QColor &color)
void | setSelectedColor (const QColor &color)
void | setPen (const QPen &pen)
void | setSelectedPen (const QPen &pen)
void | setBrush (const QBrush &brush)
void | setSelectedBrush (const QBrush &brush)
void | setFont (const QFont &font)
void | setSelectedFont (const QFont &font)
void | setText (const QString &text)
void | setPositionAlignment (Qt::Alignment alignment)
void | setTextAlignment (Qt::Alignment alignment)
void | setRotation (double degrees)
void | setPadding (const QMargins &padding)
virtual double | selectTest (const QPointF &pos, bool onlySelectable, QVariant *details=nullptr) const
Public Functions inherited from QCPAbstractItem
| QCPAbstractItem (QCustomPlot *parentPlot)
bool | clipToAxisRect () const
QCPAxisRect * | clipAxisRect () const
bool | selectable () const
bool | selected () const
void | setClipToAxisRect (bool clip)
void | setClipAxisRect (QCPAxisRect *rect)
Q_SLOT void | setSelectable (bool selectable)
Q_SLOT void | setSelected (bool selected)
virtual double | selectTest (const QPointF &pos, bool onlySelectable, QVariant *details=nullptr) const =0
QList< QCPItemPosition * > | positions () const
QList< QCPItemAnchor * > | anchors () const
QCPItemPosition * | position (const QString &name) const
QCPItemAnchor * | anchor (const QString &name) const
bool | hasAnchor (const QString &name) const
Public Functions inherited from QCPLayerable
| QCPLayerable (QCustomPlot *plot, QString targetLayer=QString(), QCPLayerable *parentLayerable=nullptr)
bool | visible () const
QCustomPlot * | parentPlot () const
QCPLayerable * | parentLayerable () const
QCPLayer * | layer () const
bool | antialiased () const
void | setVisible (bool on)
Q_SLOT bool | setLayer (QCPLayer *layer)
bool | setLayer (const QString &layerName)
void | setAntialiased (bool enabled)
virtual double | selectTest (const QPointF &pos, bool onlySelectable, QVariant *details=nullptr) const
bool | realVisibility () const

## Public Members
QCPItemPosition *const | position
QCPItemAnchor *const | topLeft
QCPItemAnchor *const | top
QCPItemAnchor *const | topRight
QCPItemAnchor *const | right
QCPItemAnchor *const | bottomRight
QCPItemAnchor *const | bottom
QCPItemAnchor *const | bottomLeft
QCPItemAnchor *const | left

## Protected Types
enum | AnchorIndex

## Protected Functions
virtual void | draw (QCPPainter *painter)
virtual QPointF | anchorPixelPosition (int anchorId) const
QPointF | getTextDrawPoint (const QPointF &pos, const QRectF &rect, Qt::Alignment positionAlignment) const
QFont | mainFont () const
QColor | mainColor () const
QPen | mainPen () const
QBrush | mainBrush () const
Protected Functions inherited from QCPAbstractItem
virtual QCP::Interaction | selectionCategory () const
virtual QRect | clipRect () const
virtual void | applyDefaultAntialiasingHint (QCPPainter *painter) const
virtual void | draw (QCPPainter *painter)=0
virtual void | selectEvent (QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged)
virtual void | deselectEvent (bool *selectionStateChanged)
virtual QPointF | anchorPixelPosition (int anchorId) const
double | rectDistance (const QRectF &rect, const QPointF &pos, bool filledRect) const
QCPItemPosition * | createPosition (const QString &name)
QCPItemAnchor * | createAnchor (const QString &name, int anchorId)
Protected Functions inherited from QCPLayerable
virtual void | parentPlotInitialized (QCustomPlot *parentPlot)
virtual QCP::Interaction | selectionCategory () const
virtual QRect | clipRect () const
virtual void | applyDefaultAntialiasingHint (QCPPainter *painter) const =0
virtual void | draw (QCPPainter *painter)=0
virtual void | selectEvent (QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged)
virtual void | deselectEvent (bool *selectionStateChanged)
virtual void | mousePressEvent (QMouseEvent *event, const QVariant &details)
virtual void | mouseMoveEvent (QMouseEvent *event, const QPointF &startPos)
virtual void | mouseReleaseEvent (QMouseEvent *event, const QPointF &startPos)
virtual void | mouseDoubleClickEvent (QMouseEvent *event, const QVariant &details)
virtual void | wheelEvent (QWheelEvent *event)
void | initializeParentPlot (QCustomPlot *parentPlot)
void | setParentLayerable (QCPLayerable *parentLayerable)
bool | moveToLayer (QCPLayer *layer, bool prepend)
void | applyAntialiasingHint (QCPPainter *painter, bool localAntialiased, QCP::AntialiasedElement overrideElement) const

## Additional Inherited Members
Signals inherited from QCPAbstractItem
void | selectionChanged (bool selected)
void | selectableChanged (bool selectable)
Signals inherited from QCPLayerable
void | layerChanged (QCPLayer *newLayer)

## Detailed Description

A text label.

Text example. Blue dotted circles are anchors, solid blue discs are positions.

Its position is defined by the member position and the setting of setPositionAlignment. The latter controls which part of the text rect shall be aligned with position.

The text alignment itself (i.e. left, center, right) can be controlled with setTextAlignment.

The text may be rotated around the position point with setRotation.

## Constructor & Destructor Documentation

## ◆ QCPItemText()

QCPItemText::QCPItemText | ( | QCustomPlot * | parentPlot | ) |
| explicit

Creates a text item and sets default values.

The created item is automatically registered with parentPlot. This QCustomPlot instance takes ownership of the item, so do not delete it manually but use QCustomPlot::removeItem() instead.

## Member Function Documentation

## ◆ setColor()

void QCPItemText::setColor | ( | const QColor & | color | ) |

Sets the color of the text.

## ◆ setSelectedColor()

void QCPItemText::setSelectedColor | ( | const QColor & | color | ) |

Sets the color of the text that will be used when the item is selected.

## ◆ setPen()

void QCPItemText::setPen | ( | const QPen & | pen | ) |

Sets the pen that will be used do draw a rectangular border around the text. To disable the border, set pen to Qt::NoPen.

See also
  setSelectedPen, setBrush, setPadding

## ◆ setSelectedPen()

void QCPItemText::setSelectedPen | ( | const QPen & | pen | ) |

Sets the pen that will be used do draw a rectangular border around the text, when the item is selected. To disable the border, set pen to Qt::NoPen.

See also
  setPen

## ◆ setBrush()

void QCPItemText::setBrush | ( | const QBrush & | brush | ) |

Sets the brush that will be used do fill the background of the text. To disable the background, set brush to Qt::NoBrush.

See also
  setSelectedBrush, setPen, setPadding

## ◆ setSelectedBrush()

void QCPItemText::setSelectedBrush | ( | const QBrush & | brush | ) |

Sets the brush that will be used do fill the background of the text, when the item is selected. To disable the background, set brush to Qt::NoBrush.

See also
  setBrush

## ◆ setFont()

void QCPItemText::setFont | ( | const QFont & | font | ) |

Sets the font of the text.

See also
  setSelectedFont, setColor

## ◆ setSelectedFont()

void QCPItemText::setSelectedFont | ( | const QFont & | font | ) |

Sets the font of the text that will be used when the item is selected.

See also
  setFont

## ◆ setText()

void QCPItemText::setText | ( | const QString & | text | ) |

Sets the text that will be displayed. Multi-line texts are supported by inserting a line break character, e.g. '
'.

See also
  setFont, setColor, setTextAlignment

## ◆ setPositionAlignment()

void QCPItemText::setPositionAlignment | ( | Qt::Alignment | alignment | ) |

Sets which point of the text rect shall be aligned with position.

Examples:

- If alignment is `Qt::AlignHCenter | Qt::AlignTop`, the text will be positioned such that the top of the text rect will be horizontally centered on position.
- If alignment is `Qt::AlignLeft | Qt::AlignBottom`, position will indicate the bottom left corner of the text rect.

If you want to control the alignment of (multi-lined) text within the text rect, use setTextAlignment.

## ◆ setTextAlignment()

void QCPItemText::setTextAlignment | ( | Qt::Alignment | alignment | ) |

Controls how (multi-lined) text is aligned inside the text rect (typically Qt::AlignLeft, Qt::AlignCenter or Qt::AlignRight).

## ◆ setRotation()

void QCPItemText::setRotation | ( | double | degrees | ) |

Sets the angle in degrees by which the text (and the text rectangle, if visible) will be rotated around position.

## ◆ setPadding()

void QCPItemText::setPadding | ( | const QMargins & | padding | ) |

Sets the distance between the border of the text rectangle and the text. The appearance (and visibility) of the text rectangle can be controlled with setPen and setBrush.

## ◆ selectTest()

double QCPItemText::selectTest | ( | const QPointF & | pos,
|  | bool | onlySelectable,
|  | QVariant * | details = `nullptr`
| ) |  | const
| virtual

This function is used to decide whether a click hits a layerable object or not.

pos is a point in pixel coordinates on the QCustomPlot surface. This function returns the shortest pixel distance of this point to the object. If the object is either invisible or the distance couldn't be determined, -1.0 is returned. Further, if onlySelectable is true and the object is not selectable, -1.0 is returned, too.

If the object is represented not by single lines but by an area like a QCPItemText or the bars of a QCPBars plottable, a click inside the area should also be considered a hit. In these cases this function thus returns a constant value greater zero but still below the parent plot's selection tolerance. (typically the selectionTolerance multiplied by 0.99).

Providing a constant value for area objects allows selecting line objects even when they are obscured by such area objects, by clicking close to the lines (i.e. closer than 0.99*selectionTolerance).

The actual setting of the selection state is not done by this function. This is handled by the parent QCustomPlot when the mouseReleaseEvent occurs, and the finally selected object is notified via the selectEvent/ deselectEvent methods.

details is an optional output parameter. Every layerable subclass may place any information in details. This information will be passed to selectEvent when the parent QCustomPlot decides on the basis of this selectTest call, that the object was successfully selected. The subsequent call to selectEvent will carry the details. This is useful for multi-part objects (like QCPAxis). This way, a possibly complex calculation to decide which part was clicked is only done once in selectTest. The result (i.e. the actually clicked part) can then be placed in details. So in the subsequent selectEvent, the decision which part was selected doesn't have to be done a second time for a single selection operation.

In the case of 1D Plottables (QCPAbstractPlottable1D, like QCPGraph or QCPBars) details will be set to a QCPDataSelection, describing the closest data point to pos.

You may pass `nullptr` as details to indicate that you are not interested in those selection details.

See also
  selectEvent, deselectEvent, mousePressEvent, wheelEvent, QCustomPlot::setInteractions, QCPAbstractPlottable1D::selectTestRect

Implements QCPAbstractItem.

## ◆ draw()

void QCPItemText::draw | ( | QCPPainter * | painter | ) |
| protectedvirtual

Draws this item with the provided painter.

The cliprect of the provided painter is set to the rect returned by clipRect before this function is called. The clipRect depends on the clipping settings defined by setClipToAxisRect and setClipAxisRect.

Implements QCPAbstractItem.

## ◆ anchorPixelPosition()

QPointF QCPItemText::anchorPixelPosition | ( | int | anchorId | ) | const
| protectedvirtual

Returns the pixel position of the anchor with Id anchorId. This function must be reimplemented in item subclasses if they want to provide anchors (QCPItemAnchor).

For example, if the item has two anchors with id 0 and 1, this function takes one of these anchor ids and returns the respective pixel points of the specified anchor.

See also
  createAnchor

Reimplemented from QCPAbstractItem.

## ◆ getTextDrawPoint()

QPointF QCPItemText::getTextDrawPoint | ( | const QPointF & | pos,
|  | const QRectF & | rect,
|  | Qt::Alignment | positionAlignment
| ) |  | const
| protected

Returns the point that must be given to the QPainter::drawText function (which expects the top left point of the text rect), according to the position pos, the text bounding box rect and the requested positionAlignment.

For example, if positionAlignment is `Qt::AlignLeft | Qt::AlignBottom` the returned point will be shifted upward by the height of rect, starting from pos. So if the text is finally drawn at that point, the lower left corner of the resulting text rect is at pos.

## ◆ mainFont()

QFont QCPItemText::mainFont | ( |  | ) | const
| protected

Returns the font that should be used for drawing text. Returns mFont when the item is not selected and mSelectedFont when it is.

## ◆ mainColor()

QColor QCPItemText::mainColor | ( |  | ) | const
| protected

Returns the color that should be used for drawing text. Returns mColor when the item is not selected and mSelectedColor when it is.

## ◆ mainPen()

QPen QCPItemText::mainPen | ( |  | ) | const
| protected

Returns the pen that should be used for drawing lines. Returns mPen when the item is not selected and mSelectedPen when it is.

## ◆ mainBrush()

QBrush QCPItemText::mainBrush | ( |  | ) | const
| protected

Returns the brush that should be used for drawing fills of the item. Returns mBrush when the item is not selected and mSelectedBrush when it is.

The documentation for this class was generated from the following files:

- src/items/item-text.h
- src/items/item-text.cpp