QgsTextFormat class

Container for all settings relating to text rendering.

Public static functions

static auto fromMimeData(const QMimeData* data, bool* ok = nullptr) -> QgsTextFormat
Attempts to parse the provided mime data as a QgsTextFormat.
static auto fromQFont(const QFont& font) -> QgsTextFormat
Returns a text format matching the settings from an input font.

Constructors, destructors, conversion operators

QgsTextFormat(const QgsTextFormat& other)
Copy constructor.

Public functions

auto background() -> QgsTextBackgroundSettings&
Returns a reference to the text background settings.
auto background() const -> QgsTextBackgroundSettings
Returns a reference to the text background settings.
auto blendMode() const -> QPainter::CompositionMode
Returns the blending mode used for drawing the text.
auto buffer() -> QgsTextBufferSettings&
Returns a reference to the text buffer settings.
auto buffer() const -> QgsTextBufferSettings
Returns a reference to the text buffer settings.
auto color() const -> QColor
Returns the color that text will be rendered in.
auto containsAdvancedEffects() const -> bool
Returns true if any component of the font format requires advanced effects such as blend modes, which require output in raster formats to be fully respected.
auto font() const -> QFont
Returns the font used for rendering text.
auto fontFound() const -> bool
Returns true if the specified font was found on the system, or false if the font was not found and a replacement was used instead.
auto lineHeight() const -> double
Returns the line height for text.
auto namedStyle() const -> QString
Returns the named style for the font used for rendering text (e.g., "bold").
auto opacity() const -> double
Returns the text's opacity.
void readFromLayer(QgsVectorLayer* layer)
Reads settings from a layer's custom properties (for QGIS 2.x projects).
void readXml(const QDomElement& elem, const QgsReadWriteContext& context)
Read settings from a DOM element.
auto resolvedFontFamily() const -> QString
Returns the family for the resolved font, ie if the specified font was not found on the system this will return the name of the replacement font.
auto scaledFont(const QgsRenderContext& context) const -> QFont
Returns a font with the size scaled to match the format's size settings (including units and map unit scale) for a specified render context.
void setBackground(const QgsTextBackgroundSettings& backgroundSettings)
Sets the text's background settings.q.
void setBlendMode(QPainter::CompositionMode mode)
Sets the blending mode used for drawing the text.
void setBuffer(const QgsTextBufferSettings& bufferSettings)
Sets the text's buffer settings.
void setColor(const QColor& color)
Sets the color that text will be rendered in.
void setFont(const QFont& font)
Sets the font used for rendering text.
void setLineHeight(double height)
Sets the line height for text.
void setNamedStyle(const QString& style)
Sets the named style for the font used for rendering text.
void setOpacity(double opacity)
Sets the text's opacity.
void setShadow(const QgsTextShadowSettings& shadowSettings)
Sets the text's drop shadow settings.
void setSize(double size)
Sets the size for rendered text.
void setSizeMapUnitScale(const QgsMapUnitScale& scale)
Sets the map unit scale object for the size.
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the size of rendered text.
auto shadow() -> QgsTextShadowSettings&
Returns a reference to the text drop shadow settings.
auto shadow() const -> QgsTextShadowSettings
Returns a reference to the text drop shadow settings.
auto size() const -> double
Returns the size for rendered text.
auto sizeMapUnitScale() const -> QgsMapUnitScale
Returns the map unit scale object for the size.
auto sizeUnit() const -> QgsUnitTypes::RenderUnit
Returns the units for the size of rendered text.
auto toMimeData() const -> QMimeData*
Returns new mime data representing the text format settings.
auto toQFont() const -> QFont
Returns a QFont matching the relevant settings from this text format.
auto writeXml(QDomDocument& doc, const QgsReadWriteContext& context) const -> QDomElement
Write settings into a DOM element.

Function documentation

static QgsTextFormat QgsTextFormat::fromMimeData(const QMimeData* data, bool* ok = nullptr)

Attempts to parse the provided mime data as a QgsTextFormat.

If data can be parsed as a text format, ok will be set to true.

static QgsTextFormat QgsTextFormat::fromQFont(const QFont& font)

Returns a text format matching the settings from an input font.

Unlike setFont(), this method also handles the size and size units from font.

QgsTextFormat::QgsTextFormat(const QgsTextFormat& other)

Copy constructor.

Parameters
other source QgsTextFormat

QgsTextBackgroundSettings& QgsTextFormat::background()

Returns a reference to the text background settings.

QgsTextBackgroundSettings QgsTextFormat::background() const

Returns a reference to the text background settings.

QPainter::CompositionMode QgsTextFormat::blendMode() const

Returns the blending mode used for drawing the text.

QgsTextBufferSettings& QgsTextFormat::buffer()

Returns a reference to the text buffer settings.

QgsTextBufferSettings QgsTextFormat::buffer() const

Returns a reference to the text buffer settings.

QColor QgsTextFormat::color() const

Returns the color that text will be rendered in.

QFont QgsTextFormat::font() const

Returns the font used for rendering text.

Note that the size of the font is not used, and size() should be called instead to determine the size of rendered text.

bool QgsTextFormat::fontFound() const

Returns true if the specified font was found on the system, or false if the font was not found and a replacement was used instead.

double QgsTextFormat::lineHeight() const

Returns the line height for text.

This is a number between 0.0 and 10.0 representing the leading between lines as a multiplier of line height.

QString QgsTextFormat::namedStyle() const

Returns the named style for the font used for rendering text (e.g., "bold").

double QgsTextFormat::opacity() const

Returns the text's opacity.

The opacity is a double value between 0 (fully transparent) and 1 (totally opaque).

void QgsTextFormat::readFromLayer(QgsVectorLayer* layer)

Reads settings from a layer's custom properties (for QGIS 2.x projects).

Parameters
layer source vector layer

void QgsTextFormat::readXml(const QDomElement& elem, const QgsReadWriteContext& context)

Read settings from a DOM element.

QString QgsTextFormat::resolvedFontFamily() const

Returns the family for the resolved font, ie if the specified font was not found on the system this will return the name of the replacement font.

QFont QgsTextFormat::scaledFont(const QgsRenderContext& context) const

Returns a font with the size scaled to match the format's size settings (including units and map unit scale) for a specified render context.

Parameters
context destination render context
Returns font with scaled size

void QgsTextFormat::setBackground(const QgsTextBackgroundSettings& backgroundSettings)

Sets the text's background settings.q.

Parameters
backgroundSettings background settings

void QgsTextFormat::setBlendMode(QPainter::CompositionMode mode)

Sets the blending mode used for drawing the text.

Parameters
mode blending mode

void QgsTextFormat::setBuffer(const QgsTextBufferSettings& bufferSettings)

Sets the text's buffer settings.

Parameters
bufferSettings buffer settings

void QgsTextFormat::setColor(const QColor& color)

Sets the color that text will be rendered in.

Parameters
color text color

void QgsTextFormat::setFont(const QFont& font)

Sets the font used for rendering text.

Parameters
font desired font

Note that the size of the font is not used, and setSize() should be called instead to explicitly set the size of rendered text.

void QgsTextFormat::setLineHeight(double height)

Sets the line height for text.

Parameters
height a number between 0.0 and 10.0 representing the leading between lines as a multiplier of line height.

void QgsTextFormat::setNamedStyle(const QString& style)

Sets the named style for the font used for rendering text.

Parameters
style named style, e.g., "bold"

void QgsTextFormat::setOpacity(double opacity)

Sets the text's opacity.

Parameters
opacity opacity as a double value between 0 (fully transparent) and 1 (totally opaque)

void QgsTextFormat::setShadow(const QgsTextShadowSettings& shadowSettings)

Sets the text's drop shadow settings.

Parameters
shadowSettings shadow settings

void QgsTextFormat::setSize(double size)

Sets the size for rendered text.

Parameters
size size of rendered text. Units are set using setSizeUnit()

void QgsTextFormat::setSizeMapUnitScale(const QgsMapUnitScale& scale)

Sets the map unit scale object for the size.

This is only used if the sizeUnit() is set to QgsUnitTypes::RenderMapUnit.

void QgsTextFormat::setSizeUnit(QgsUnitTypes::RenderUnit unit)

Sets the units for the size of rendered text.

Parameters
unit size units

QgsTextShadowSettings& QgsTextFormat::shadow()

Returns a reference to the text drop shadow settings.

QgsTextShadowSettings QgsTextFormat::shadow() const

Returns a reference to the text drop shadow settings.

double QgsTextFormat::size() const

Returns the size for rendered text.

Units are retrieved using sizeUnit().

QgsMapUnitScale QgsTextFormat::sizeMapUnitScale() const

Returns the map unit scale object for the size.

This is only used if the sizeUnit() is set to QgsUnitTypes::RenderMapUnit.

QgsUnitTypes::RenderUnit QgsTextFormat::sizeUnit() const

Returns the units for the size of rendered text.

QMimeData* QgsTextFormat::toMimeData() const

Returns new mime data representing the text format settings.

Caller takes responsibility for deleting the returned object.

QFont QgsTextFormat::toQFont() const

Returns a QFont matching the relevant settings from this text format.

Unlike font(), this method also handles the size and size units from the text format.

QDomElement QgsTextFormat::writeXml(QDomDocument& doc, const QgsReadWriteContext& context) const

Write settings into a DOM element.