Class: QgsFontButton

A button for customizing QgsTextFormat settings.

The button will open a detailed text format settings dialog when clicked. An attached drop-down menu allows for copying and pasting text styles, picking colors for the text, and for dropping colors from other color widgets.

The button can be used in two different modes(). The default behavior is to include all settings used for configuring QgsTextFormat/QgsTextRenderer classes. A cut down mode (without settings for color) is also available when the resultant font is used only in a QFont object.

Class Hierarchy

Inheritance diagram of qgis.gui.QgsFontButton

Base classes

QToolButton

QAbstractButton

QWidget

QObject

QPaintDevice

class qgis.gui.QgsFontButton[source]

Bases: QToolButton

__init__(parent: QWidget | None = None, dialogTitle: str | None = '')

Construct a new font button. Use parent to attach a parent QWidget to the dialog. Use dialogTitle string to define the title to show in the text settings dialog.

Parameters:
  • parent (Optional[QWidget] = None)

  • dialogTitle (Optional[str] = '')

class Mode

Bases: int

ModeQFont = 1
ModeTextRenderer = 0
signal changed[source]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

copyColor(self)[source]

Copies the current text color to the clipboard. This is only used when mode() is ModeTextRenderer.

See also

pasteColor()

copyFormat(self)[source]

Copies the current text format to the clipboard.

See also

pasteFormat()

currentFont(self) QFont[source]

Returns the current QFont set by the widget. This is only used when mode() is ModeQFont.

See also

setCurrentFont()

Return type:

QFont

dialogTitle(self) str[source]

Returns the title for the text settings dialog window.

See also

setDialogTitle()

Return type:

str

layer(self) QgsVectorLayer | None[source]

Returns the layer associated with the widget.

See also

setLayer()

Added in version 3.10.

Return type:

Optional[QgsVectorLayer]

mapCanvas(self) QgsMapCanvas | None[source]

Returns the map canvas associated with the widget.

See also

setMapCanvas()

Return type:

Optional[QgsMapCanvas]

messageBar(self) QgsMessageBar | None[source]

Returns the message bar associated with the widget.

See also

setMessageBar()

Added in version 3.10.

Return type:

Optional[QgsMessageBar]

mode(self) QgsFontButton.Mode[source]

Returns the current button mode.

See also

setMode()

Return type:

QgsFontButton.Mode

pasteColor(self)[source]

Pastes a color from the clipboard to the text format. If clipboard does not contain a valid color or string representation of a color, then no change is applied. This is only used when mode() is ModeTextRenderer.

See also

copyColor()

pasteFormat(self)[source]

Pastes a format from the clipboard. If clipboard does not contain a valid format then no change is applied.

See also

copyFormat()

registerExpressionContextGenerator(self, generator: QgsExpressionContextGenerator | None)[source]

Register an expression context generator class that will be used to retrieve an expression context for the button when required.

Added in version 3.10.

Parameters:

generator (Optional[QgsExpressionContextGenerator])

setColor(self, color: QColor | Qt.GlobalColor)[source]

Sets the current color for the text. Will emit a changed signal if the color is different to the previous text color. This is only used when mode() is ModeTextRenderer.

Parameters:

color (Union[QColor, Qt.GlobalColor])

setCurrentFont(self, font: QFont)[source]

Sets the current text font to show in the widget. This is only used when mode() is ModeQFont.

See also

currentFont()

Parameters:

font (QFont)

setDialogTitle(self, title: str | None)[source]

Sets the title for the text settings dialog window.

See also

dialogTitle()

Parameters:

title (Optional[str])

setLayer(self, layer: QgsVectorLayer | None)[source]

Sets a layer to associate with the widget. This allows the widget to setup layer related settings within the text settings dialog, such as correctly populating data defined override buttons.

See also

layer()

Added in version 3.10.

Parameters:

layer (Optional[QgsVectorLayer])

setMapCanvas(self, canvas: QgsMapCanvas | None)[source]

Sets a map canvas to associate with the widget. This allows the widget to fetch current settings from the map canvas, such as current scale.

See also

mapCanvas()

Parameters:

canvas (Optional[QgsMapCanvas])

setMessageBar(self, bar: QgsMessageBar | None)[source]

Sets the message bar associated with the widget. This allows the widget to push feedback messages to the appropriate message bar.

See also

messageBar()

Added in version 3.10.

Parameters:

bar (Optional[QgsMessageBar])

setMode(self, mode: QgsFontButton.Mode)[source]

Sets the current button mode. This can be used to toggle between the full capabilities of the button (for configuring QgsTextFormat/QgsTextRenderer objects) and a cut-back version for configuring QFont object properties (i.e. with no color settings or the other advanced options QgsTextFormat allows).

See also

mode()

Parameters:

mode (QgsFontButton.Mode)

setNoFormatString(self, string: str | None)[source]

Sets the string to use for the “null format” option in the button’s drop-down menu.

Note

The “null format” option is only shown if showNullFormat() is True.

Added in version 3.16.

Parameters:

string (Optional[str])

setShowNullFormat(self, show: bool)[source]

Sets whether the “null format” option should be shown in the button’s drop-down menu. This option is only used for buttons set to the ModeTextRenderer mode().

If selected, the “null format” option sets the button’s format to an invalid QgsTextFormat. This can be used to represent a “use default format” state for the button.

By default this option is not shown.

See also

showNullFormat()

Added in version 3.16.

Parameters:

show (bool)

setTextFormat(self, format: QgsTextFormat)[source]

Sets the current text format to show in the widget. This is only used when mode() is ModeTextRenderer.

See also

textFormat()

Parameters:

format (QgsTextFormat)

setToNullFormat(self)[source]

Sets the text format to a null (invalid) QgsTextFormat.

This is only used when mode() is ModeTextRenderer.

Added in version 3.16.

showNullFormat(self) bool[source]

Returns whether the “null format” option will be shown in the button’s drop-down menu. This option is only used for buttons set to the ModeTextRenderer mode().

If selected, the “null format” option sets the button’s format to an invalid QgsTextFormat. This can be used to represent a “use default format” state for the button.

By default this option is not shown.

Added in version 3.16.

Return type:

bool

textFormat(self) QgsTextFormat[source]

Returns the current text formatting set by the widget. This is only used when mode() is ModeTextRenderer.

See also

setTextFormat()

Return type:

QgsTextFormat