QCPTextElement Class Reference
Author: Emanuel Eichhammer

· · · ·
Public Functions | Signals | Protected Functions
QCPTextElement Class Reference

A layout element displaying a text. More...

Inheritance diagram for QCPTextElement:

## Public Functions
| QCPTextElement (QCustomPlot *parentPlot)
| QCPTextElement (QCustomPlot *parentPlot, const QString &text)
| QCPTextElement (QCustomPlot *parentPlot, const QString &text, double pointSize)
| QCPTextElement (QCustomPlot *parentPlot, const QString &text, const QString &fontFamily, double pointSize)
| QCPTextElement (QCustomPlot *parentPlot, const QString &text, const QFont &font)
QString | text () const
int | textFlags () const
QFont | font () const
QColor | textColor () const
QFont | selectedFont () const
QColor | selectedTextColor () const
bool | selectable () const
bool | selected () const
void | setText (const QString &text)
void | setTextFlags (int flags)
void | setFont (const QFont &font)
void | setTextColor (const QColor &color)
void | setSelectedFont (const QFont &font)
void | setSelectedTextColor (const QColor &color)
Q_SLOT void | setSelectable (bool selectable)
Q_SLOT void | setSelected (bool selected)
virtual double | selectTest (const QPointF &pos, bool onlySelectable, QVariant *details=nullptr) const
virtual void | mousePressEvent (QMouseEvent *event, const QVariant &details)
virtual void | mouseReleaseEvent (QMouseEvent *event, const QPointF &startPos)
virtual void | mouseDoubleClickEvent (QMouseEvent *event, const QVariant &details)
Public Functions inherited from QCPLayoutElement
| QCPLayoutElement (QCustomPlot *parentPlot=nullptr)
QCPLayout * | layout () const
QRect | rect () const
QRect | outerRect () const
QMargins | margins () const
QMargins | minimumMargins () const
QCP::MarginSides | autoMargins () const
QSize | minimumSize () const
QSize | maximumSize () const
SizeConstraintRect | sizeConstraintRect () const
QCPMarginGroup * | marginGroup (QCP::MarginSide side) const
QHash< QCP::MarginSide, QCPMarginGroup * > | marginGroups () const
void | setOuterRect (const QRect &rect)
void | setMargins (const QMargins &margins)
void | setMinimumMargins (const QMargins &margins)
void | setAutoMargins (QCP::MarginSides sides)
void | setMinimumSize (const QSize &size)
void | setMinimumSize (int width, int height)
void | setMaximumSize (const QSize &size)
void | setMaximumSize (int width, int height)
void | setSizeConstraintRect (SizeConstraintRect constraintRect)
void | setMarginGroup (QCP::MarginSides sides, QCPMarginGroup *group)
virtual void | update (UpdatePhase phase)
virtual QList< QCPLayoutElement * > | elements (bool recursive) 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)
bool | realVisibility () const

## Signals
void | selectionChanged (bool selected)
void | selectableChanged (bool selectable)
void | clicked (QMouseEvent *event)
void | doubleClicked (QMouseEvent *event)
Signals inherited from QCPLayerable
void | layerChanged (QCPLayer *newLayer)

## Protected Functions
virtual void | applyDefaultAntialiasingHint (QCPPainter *painter) const
virtual void | draw (QCPPainter *painter)
virtual QSize | minimumOuterSizeHint () const
virtual QSize | maximumOuterSizeHint () const
virtual void | selectEvent (QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged)
virtual void | deselectEvent (bool *selectionStateChanged)
QFont | mainFont () const
QColor | mainTextColor () const
Protected Functions inherited from QCPLayoutElement
virtual int | calculateAutoMargin (QCP::MarginSide side)
virtual void | layoutChanged ()
virtual void | parentPlotInitialized (QCustomPlot *parentPlot)
Protected Functions inherited from QCPLayerable
virtual QCP::Interaction | selectionCategory () const
virtual QRect | clipRect () const
virtual void | mouseMoveEvent (QMouseEvent *event, const QPointF &startPos)
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
Public Types inherited from QCPLayoutElement
enum | UpdatePhase
enum | SizeConstraintRect

## Detailed Description

A layout element displaying a text.

The text may be specified with setText, the formatting can be controlled with setFont, setTextColor, and setTextFlags.

A text element can be added as follows:

customPlot->plotLayout()->insertRow(0); // inserts an empty row above the default axis rect
customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, "Your Text"));

## Constructor & Destructor Documentation

## ◆ QCPTextElement() [1/5]

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

This is an overloaded function.
Creates a new QCPTextElement instance and sets default values. The initial text is empty (setText).

## ◆ QCPTextElement() [2/5]

QCPTextElement::QCPTextElement | ( | QCustomPlot * | parentPlot,
|  | const QString & | text )

This is an overloaded function.
Creates a new QCPTextElement instance and sets default values.

The initial text is set to text.

## ◆ QCPTextElement() [3/5]

QCPTextElement::QCPTextElement | ( | QCustomPlot * | parentPlot,
|  | const QString & | text,
|  | double | pointSize )

This is an overloaded function.
Creates a new QCPTextElement instance and sets default values.

The initial text is set to text with pointSize.

## ◆ QCPTextElement() [4/5]

QCPTextElement::QCPTextElement | ( | QCustomPlot * | parentPlot,
|  | const QString & | text,
|  | const QString & | fontFamily,
|  | double | pointSize )

This is an overloaded function.
Creates a new QCPTextElement instance and sets default values.

The initial text is set to text with pointSize and the specified fontFamily.

## ◆ QCPTextElement() [5/5]

QCPTextElement::QCPTextElement | ( | QCustomPlot * | parentPlot,
|  | const QString & | text,
|  | const QFont & | font )

This is an overloaded function.
Creates a new QCPTextElement instance and sets default values.

The initial text is set to text with the specified font.

## Member Function Documentation

## ◆ setText()

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

Sets the text that will be displayed to text. Multiple lines can be created by insertion of "\n".

See also
  setFont, setTextColor, setTextFlags

## ◆ setTextFlags()

void QCPTextElement::setTextFlags | ( | int | flags | ) |

Sets options for text alignment and wrapping behaviour. flags is a bitwise OR-combination of `Qt::AlignmentFlag` and `Qt::TextFlag` enums.

Possible enums are:

- Qt::AlignLeft
- Qt::AlignRight
- Qt::AlignHCenter
- Qt::AlignJustify
- Qt::AlignTop
- Qt::AlignBottom
- Qt::AlignVCenter
- Qt::AlignCenter
- Qt::TextDontClip
- Qt::TextSingleLine
- Qt::TextExpandTabs
- Qt::TextShowMnemonic
- Qt::TextWordWrap
- Qt::TextIncludeTrailingSpaces

## ◆ setFont()

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

Sets the font of the text.

See also
  setTextColor, setSelectedFont

## ◆ setTextColor()

void QCPTextElement::setTextColor | ( | const QColor & | color | ) |

Sets the color of the text.

See also
  setFont, setSelectedTextColor

## ◆ setSelectedFont()

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

Sets the font of the text that will be used if the text element is selected (setSelected).

See also
  setFont

## ◆ setSelectedTextColor()

void QCPTextElement::setSelectedTextColor | ( | const QColor & | color | ) |

Sets the color of the text that will be used if the text element is selected (setSelected).

See also
  setTextColor

## ◆ setSelectable()

void QCPTextElement::setSelectable | ( | bool | selectable | ) |

Sets whether the user may select this text element.

Note that even when selectable is set to false, the selection state may be changed programmatically via setSelected.

## ◆ setSelected()

void QCPTextElement::setSelected | ( | bool | selected | ) |

Sets the selection state of this text element to selected. If the selection has changed, selectionChanged is emitted.

Note that this function can change the selection state independently of the current setSelectable state.

## ◆ selectTest()

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

Returns 0.99*selectionTolerance (see QCustomPlot::setSelectionTolerance) when pos is within the bounding box of the text element's text. Note that this bounding box is updated in the draw call.

If pos is outside the text's bounding box or if onlySelectable is true and this text element is not selectable (setSelectable), returns -1.

For general information about this virtual method, see the base class implementation.

Reimplemented from QCPLayoutElement.

## ◆ mousePressEvent()

void QCPTextElement::mousePressEvent | ( | QMouseEvent * | event,
|  | const QVariant & | details )
| virtual

Accepts the mouse event in order to emit the according click signal in the mouseReleaseEvent.

For general information about this virtual method, see the base class implementation.

Reimplemented from QCPLayerable.

## ◆ mouseReleaseEvent()

void QCPTextElement::mouseReleaseEvent | ( | QMouseEvent * | event,
|  | const QPointF & | startPos )
| virtual

Emits the clicked signal if the cursor hasn't moved by more than a few pixels since the mousePressEvent.

For general information about this virtual method, see the base class implementation.

Reimplemented from QCPLayerable.

## ◆ mouseDoubleClickEvent()

void QCPTextElement::mouseDoubleClickEvent | ( | QMouseEvent * | event,
|  | const QVariant & | details )
| virtual

Emits the doubleClicked signal.

For general information about this virtual method, see the base class implementation.

Reimplemented from QCPLayerable.

## ◆ selectionChanged

void QCPTextElement::selectionChanged | ( | bool | selected | ) |
| signal

This signal is emitted when the selection state has changed to selected, either by user interaction or by a direct call to setSelected.

See also
  setSelected, setSelectable

## ◆ clicked

void QCPTextElement::clicked | ( | QMouseEvent * | event | ) |
| signal

This signal is emitted when the text element is clicked.

See also
  doubleClicked, selectTest

## ◆ doubleClicked

void QCPTextElement::doubleClicked | ( | QMouseEvent * | event | ) |
| signal

This signal is emitted when the text element is double clicked.

See also
  clicked, selectTest

## ◆ applyDefaultAntialiasingHint()

void QCPTextElement::applyDefaultAntialiasingHint | ( | QCPPainter * | painter | ) | const
| protectedvirtual

This function applies the default antialiasing setting to the specified painter, using the function applyAntialiasingHint. It is the antialiasing state the painter is put in, when draw is called on the layerable. If the layerable has multiple entities whose antialiasing setting may be specified individually, this function should set the antialiasing state of the most prominent entity. In this case however, the draw function usually calls the specialized versions of this function before drawing each entity, effectively overriding the setting of the default antialiasing hint.

First example: QCPGraph has multiple entities that have an antialiasing setting: The graph line, fills and scatters. Those can be configured via QCPGraph::setAntialiased, QCPGraph::setAntialiasedFill and QCPGraph::setAntialiasedScatters. Consequently, there isn't only the QCPGraph::applyDefaultAntialiasingHint function (which corresponds to the graph line's antialiasing), but specialized ones like QCPGraph::applyFillAntialiasingHint and QCPGraph::applyScattersAntialiasingHint. So before drawing one of those entities, QCPGraph::draw calls the respective specialized applyAntialiasingHint function.

Second example: QCPItemLine consists only of a line so there is only one antialiasing setting which can be controlled with QCPItemLine::setAntialiased. (This function is inherited by all layerables. The specialized functions, as seen on QCPGraph, must be added explicitly to the respective layerable subclass.) Consequently it only has the normal QCPItemLine::applyDefaultAntialiasingHint. The QCPItemLine::draw function doesn't need to care about setting any antialiasing states, because the default antialiasing hint is already set on the painter when the draw function is called, and that's the state it wants to draw the line with.

Reimplemented from QCPLayoutElement.

## ◆ draw()

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

This function draws the layerable with the specified painter. It is only called by QCustomPlot, if the layerable is visible (setVisible).

Before this function is called, the painter's antialiasing state is set via applyDefaultAntialiasingHint, see the documentation there. Further, the clipping rectangle was set to clipRect.

Reimplemented from QCPLayoutElement.

## ◆ minimumOuterSizeHint()

QSize QCPTextElement::minimumOuterSizeHint | ( |  | ) | const
| protectedvirtual

Returns the suggested minimum size this layout element (the outerRect) may be compressed to, if no manual minimum size is set.

if a minimum size (setMinimumSize) was not set manually, parent layouts use the returned size (usually indirectly through QCPLayout::getFinalMinimumOuterSize) to determine the minimum allowed size of this layout element.

A manual minimum size is considered set if it is non-zero.

The default implementation simply returns the sum of the horizontal margins for the width and the sum of the vertical margins for the height. Reimplementations may use their detailed knowledge about the layout element's content to provide size hints.

Reimplemented from QCPLayoutElement.

## ◆ maximumOuterSizeHint()

QSize QCPTextElement::maximumOuterSizeHint | ( |  | ) | const
| protectedvirtual

Returns the suggested maximum size this layout element (the outerRect) may be expanded to, if no manual maximum size is set.

if a maximum size (setMaximumSize) was not set manually, parent layouts use the returned size (usually indirectly through QCPLayout::getFinalMaximumOuterSize) to determine the maximum allowed size of this layout element.

A manual maximum size is considered set if it is smaller than Qt's `QWIDGETSIZE_MAX`.

The default implementation simply returns `QWIDGETSIZE_MAX` for both width and height, implying no suggested maximum size. Reimplementations may use their detailed knowledge about the layout element's content to provide size hints.

Reimplemented from QCPLayoutElement.

## ◆ selectEvent()

void QCPTextElement::selectEvent | ( | QMouseEvent * | event,
|  | bool | additive,
|  | const QVariant & | details,
|  | bool * | selectionStateChanged )
| protectedvirtual

This event is called when the layerable shall be selected, as a consequence of a click by the user. Subclasses should react to it by setting their selection state appropriately. The default implementation does nothing.

event is the mouse event that caused the selection. additive indicates, whether the user was holding the multi-select-modifier while performing the selection (see QCustomPlot::setMultiSelectModifier). if additive is true, the selection state must be toggled (i.e. become selected when unselected and unselected when selected).

Every selectEvent is preceded by a call to selectTest, which has returned positively (i.e. returned a value greater than 0 and less than the selection tolerance of the parent QCustomPlot). The details data you output from selectTest is fed back via details here. You may use it to transport any kind of information from the selectTest to the possibly subsequent selectEvent. Usually details is used to transfer which part was clicked, if it is a layerable that has multiple individually selectable parts (like QCPAxis). This way selectEvent doesn't need to do the calculation again to find out which part was actually clicked.

selectionStateChanged is an output parameter. If the pointer is non-null, this function must set the value either to true or false, depending on whether the selection state of this layerable was actually changed. For layerables that only are selectable as a whole and not in parts, this is simple: if additive is true, selectionStateChanged must also be set to true, because the selection toggles. If additive is false, selectionStateChanged is only set to true, if the layerable was previously unselected and now is switched to the selected state.

See also
  selectTest, deselectEvent

Reimplemented from QCPLayerable.

## ◆ deselectEvent()

void QCPTextElement::deselectEvent | ( | bool * | selectionStateChanged | ) |
| protectedvirtual

This event is called when the layerable shall be deselected, either as consequence of a user interaction or a call to QCustomPlot::deselectAll. Subclasses should react to it by unsetting their selection appropriately.

just as in selectEvent, the output parameter selectionStateChanged (if non-null), must return true or false when the selection state of this layerable has changed or not changed, respectively.

See also
  selectTest, selectEvent

Reimplemented from QCPLayerable.

## ◆ mainFont()

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

Returns the main font to be used. This is mSelectedFont if setSelected is set to true, else mFont is returned.

## ◆ mainTextColor()

QColor QCPTextElement::mainTextColor | ( |  | ) | const
| protected

Returns the main color to be used. This is mSelectedTextColor if setSelected is set to true, else mTextColor is returned.

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

- src/layoutelements/layoutelement-textelement.h
- src/layoutelements/layoutelement-textelement.cpp