Font icons#
The superqt.fonticon
module provides a set of utilities for working with font
icons such as Font Awesome or Material Design
Icons.
Basic Example#
from fonticon_fa5 import FA5S
from qtpy.QtCore import QSize
from qtpy.QtWidgets import QApplication, QPushButton
from superqt.fonticon import icon, pulse
app = QApplication([])
btn2 = QPushButton()
btn2.setIcon(icon(FA5S.smile, color="blue"))
btn2.setIconSize(QSize(225, 225))
btn2.show()
app.exec()
Font Icon plugins#
Ready-made fonticon packs are available as plugins.
A great way to search across most available icons libraries from a single search interface is to use glyphsearch: https://glyphsearch.com/
If a font library you'd like to use is unavailable as a superqt plugin, please open a feature request
Font Awesome 6#
Browse available icons at https://fontawesome.com/v6/search
pip install fonticon-fontawesome6
Font Awesome 5#
Browse available icons at https://fontawesome.com/v5/search
pip install fonticon-fontawesome5
Material Design Icons 7#
Browse available icons at https://materialdesignicons.com/
pip install fonticon-materialdesignicons7
Material Design Icons 6#
Browse available icons at https://materialdesignicons.com/ (note that the search defaults to v7, see changes from v6 in the changelog)
pip install fonticon-materialdesignicons6
See also#
- https://github.com/tlambert03/fonticon-bootstrapicons
- https://github.com/tlambert03/fonticon-linearicons
- https://github.com/tlambert03/fonticon-feather
superqt.fonticon
is a pluggable system, and font icon packs may use the "superqt.fonticon"
entry point to register themselves with superqt. See fonticon-cookiecutter
for a template, or look through the following repos for examples:
- https://github.com/tlambert03/fonticon-fontawesome6
- https://github.com/tlambert03/fonticon-fontawesome5
- https://github.com/tlambert03/fonticon-materialdesignicons6
API#
superqt.fonticon.icon(glyph_key, scale_factor=DEFAULT_SCALING_FACTOR, color=None, opacity=1, animation=None, transform=None, states=None)
#
Create a QIcon for glyph_key
, with a number of optional settings.
The glyph_key
(e.g. 'fa5s.smile') represents a Font-family & style, and a glyph.
In most cases, the key should be provided by a plugin in the environment, like:
- fonticon-fontawesome5 ('fa5s' & 'fa5r' prefixes)
- fonticon-materialdesignicons6 ('mdi6' prefix)
...but fonts can also be added manually using addFont
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
glyph_key |
str
|
String encapsulating a font-family, style, and glyph. e.g. 'fa5s.smile'. |
required |
scale_factor |
float
|
Scale factor (fraction of widget height), When widget icon is painted on widget,
it will use |
DEFAULT_SCALING_FACTOR
|
color |
ValidColor
|
Color for the font, by default None. (e.g. The default |
None
|
opacity |
float
|
Opacity of icon, by default 1 |
1
|
animation |
Animation
|
Animation for the icon. A subclass of superqt.fonticon.Animation, that provides
a concrete |
None
|
transform |
QTransform
|
A |
None
|
states |
dict
|
Provide additional styling for the icon in different states.
Missing keys in the state dicts will be taken from the default options, provided by the parameters above. |
None
|
Returns:
Type | Description |
---|---|
QFontIcon
|
A subclass of QIcon. Can be used wherever QIcons are used, such as
|
Examples:
simple example (using the string 'fa5s.smile'
assumes the fonticon-fontawesome5
plugin is installed)
>>> btn = QPushButton()
>>> btn.setIcon(icon('fa5s.smile'))
can also directly import from fonticon_fa5
>>> from fonticon_fa5 import FA5S
>>> btn.setIcon(icon(FA5S.smile))
with animation
>>> btn2 = QPushButton()
>>> btn2.setIcon(icon(FA5S.spinner, animation=pulse(btn2)))
complicated example
>>> btn = QPushButton()
>>> btn.setIcon(
... icon(
... FA5S.ambulance,
... color="blue",
... states={
... "active": {
... "glyph": FA5S.bath,
... "color": "red",
... "scale_factor": 0.5,
... "animation": pulse(btn),
... },
... "disabled": {
... "color": "green",
... "scale_factor": 0.8,
... "animation": spin(btn)
... },
... },
... )
... )
>>> btn.setIconSize(QSize(256, 256))
>>> btn.show()
options: heading_level: 3
superqt.fonticon.setTextIcon(widget, glyph_key, size=None)
#
Set text on a widget to a specific font & glyph.
This is an alternative to setting a QIcon with a pixmap. It may be easier to combine with dynamic stylesheets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
widget |
QWidget
|
A widget supporting a |
required |
glyph_key |
str
|
String encapsulating a font-family, style, and glyph. e.g. 'fa5s.smile'. |
required |
size |
int
|
Size for QFont. passed to |
None
|
options: heading_level: 3
superqt.fonticon.font(font_prefix, size=None)
#
Create QFont for font_prefix
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
font_prefix |
str
|
Font_prefix, such as 'fa5s' or 'mdi6', representing a font-family and style. |
required |
size |
int
|
Size for QFont. passed to |
None
|
Returns:
Type | Description |
---|---|
QFont
|
QFont instance that can be used to add fonticons to widgets. |
options: heading_level: 3
superqt.fonticon.IconOpts
dataclass
#
Options for rendering an icon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
glyph_key |
str
|
The key of the glyph to use, e.g. |
_Unset
|
scale_factor |
float
|
The scale factor to use, by default |
_Unset
|
color |
ValidColor
|
The color to use, by default |
_Unset
|
opacity |
float
|
The opacity to use, by default |
_Unset
|
animation |
Animation
|
The animation to use, by default |
_Unset
|
options: heading_level: 3
superqt.fonticon.addFont(filepath, prefix, charmap=None)
#
Add OTF/TTF file at filepath
to the registry under prefix
.
If you'd like to later use a fontkey in the form of prefix.some-name
, then
charmap
must be provided and provide a mapping for all of the glyph names
to their unicode numbers. If a charmap is not provided, glyphs must be directly
accessed with their unicode as something like key.
.
Note
in most cases, users will not need this. Instead, they should install a font plugin, like:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
Path to an OTF or TTF file containing the fonts |
required |
prefix |
str
|
A prefix that will represent this font file when used for lookup. For example, 'fa5s' for 'Font-Awesome 5 Solid'. |
required |
charmap |
Dict[str, str]
|
optional mapping for all of the glyph names to their unicode numbers. See note above. |
None
|
Returns:
Type | Description |
---|---|
(Tuple[str, str], optional)
|
font-family and font-style for the file just registered, or |
options: heading_level: 3
Animations#
the animation
parameter to icon()
accepts a subclass of
Animation
that will be
superqt.fonticon.Animation
#
options: heading_level: 3
options: heading_level: 3
options: heading_level: 3