QgsLegendSettings class

The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLegendRenderer.

Contents

The content of the legend is given in QgsLegendModel class.

Public functions

auto drawRasterStroke() const -> bool
Returns whether a stroke will be drawn around raster symbol items.
void drawText(QPainter* p, double x, double y, const QString& text, const QFont& font) const
Draws Text.
void drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap) const
Like the above, but with a rectangle for multiline text.
auto fontAscentMillimeters(const QFont& font) const -> double
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.
auto fontDescentMillimeters(const QFont& font) const -> double
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.
auto fontHeightCharacterMM(const QFont& font, QChar c) const -> double
Returns the font height of a character in millimeters.
auto mapScale() const -> double
Returns the legend map scale.
auto pixelFontSize(double pointSize) const -> double
Calculates font to from point size to pixel size.
auto rasterStrokeColor() const -> QColor
Returns the stroke color for the stroke drawn around raster symbol items.
auto rasterStrokeWidth() const -> double
Returns the stroke width (in millimeters) for the stroke drawn around raster symbol items.
auto rstyle(QgsLegendStyle::Style s) -> QgsLegendStyle&
Returns reference to modifiable style.
auto scaledFontPixelSize(const QFont& font) const -> QFont
Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE.
void setDrawRasterStroke(bool enabled)
Sets whether a stroke will be drawn around raster symbol items.
void setMapScale(double scale)
Sets the legend map scale.
void setRasterStrokeColor(const QColor& color)
Sets the stroke color for the stroke drawn around raster symbol items.
void setRasterStrokeWidth(double width)
Sets the stroke width for the stroke drawn around raster symbol items.
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
auto splitStringForWrapping(const QString& stringToSplt) const -> QStringList
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapping.
auto style(QgsLegendStyle::Style s) const -> QgsLegendStyle
Returns style.
auto textWidthMillimeters(const QFont& font, const QString& text) const -> double
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.
auto titleAlignment() const -> Qt::AlignmentFlag
Returns the alignment of the legend title.

Function documentation

bool QgsLegendSettings::drawRasterStroke() const

Returns whether a stroke will be drawn around raster symbol items.

void QgsLegendSettings::drawText(QPainter* p, double x, double y, const QString& text, const QFont& font) const

Draws Text.

Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter to work around the Qt font bug)

void QgsLegendSettings::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap) const

Like the above, but with a rectangle for multiline text.

Parameters
p painter to use
rect rectangle to draw into
text text to draw
font font to use
halignment optional horizontal alignment
valignment optional vertical alignment
flags allows for passing Qt::TextFlags to control appearance of rendered text

double QgsLegendSettings::mapScale() const

Returns the legend map scale.

The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.

QColor QgsLegendSettings::rasterStrokeColor() const

Returns the stroke color for the stroke drawn around raster symbol items.

The stroke is only drawn if drawRasterStroke() is true.

double QgsLegendSettings::rasterStrokeWidth() const

Returns the stroke width (in millimeters) for the stroke drawn around raster symbol items.

The stroke is only drawn if drawRasterStroke() is true.

QgsLegendStyle& QgsLegendSettings::rstyle(QgsLegendStyle::Style s)

Returns reference to modifiable style.

void QgsLegendSettings::setDrawRasterStroke(bool enabled)

Sets whether a stroke will be drawn around raster symbol items.

Parameters
enabled set to true to draw borders

void QgsLegendSettings::setMapScale(double scale)

Sets the legend map scale.

The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.

void QgsLegendSettings::setRasterStrokeColor(const QColor& color)

Sets the stroke color for the stroke drawn around raster symbol items.

Parameters
color stroke color

The stroke is only drawn if drawRasterStroke() is true.

void QgsLegendSettings::setRasterStrokeWidth(double width)

Sets the stroke width for the stroke drawn around raster symbol items.

Parameters
width stroke width in millimeters

The stroke is only drawn if drawRasterStroke() is true.

void QgsLegendSettings::setTitleAlignment(Qt::AlignmentFlag alignment)

Sets the alignment of the legend title.

Parameters
alignment Text alignment for drawing the legend title

Qt::AlignmentFlag QgsLegendSettings::titleAlignment() const

Returns the alignment of the legend title.

Returns Qt::AlignmentFlag for the legend title