QIcon#
The QIcon
class provides scalable icons in different modes and states. More…
Synopsis#
Functions#
def
actualSize
(size[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])def
actualSize
(window, size[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])def
addFile
(fileName[, size=QSize()[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]]])def
addPixmap
(path)def
addPixmap
(pixmap[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])def
availableSizes
([mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])def
cacheKey
()def
isMask
()def
isNull
()def
name
()def
paint
(painter, rect[, alignment=Qt.AlignCenter[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]]])def
paint
(painter, x, y, w, h[, alignment=Qt.AlignCenter[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]]])def
pixmap
(window, size[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])def
pixmap
(size[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])def
pixmap
(size, devicePixelRatio[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])def
pixmap
(extent[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])def
pixmap
(w, h[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])def
setIsMask
(isMask)def
swap
(other)
Static functions#
def
fallbackSearchPaths
()def
fallbackThemeName
()def
fromTheme
(name)def
fromTheme
(name, fallback)def
hasThemeIcon
(name)def
setFallbackSearchPaths
(paths)def
setFallbackThemeName
(name)def
setThemeName
(path)def
setThemeSearchPaths
(searchpath)def
themeName
()def
themeSearchPaths
()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
A QIcon
can generate smaller, larger, active, and disabled pixmaps from the set of pixmaps it is given. Such pixmaps are used by Qt widgets to show an icon representing a particular action.
The simplest use of QIcon
is to create one from a QPixmap
file or resource, and then use it, allowing Qt to work out all the required icon styles and sizes. For example:
button = QToolButton() button.setIcon(QIcon("open.xpm"))
To undo a QIcon
, simply set a null icon in its place:
button.setIcon(QIcon())
Use the supportedImageFormats()
and supportedImageFormats()
functions to retrieve a complete list of the supported file formats.
When you retrieve a pixmap using pixmap(QSize, Mode, State), and no pixmap for this given size, mode and state has been added with addFile()
or addPixmap()
, then QIcon
will generate one on the fly. This pixmap generation happens in a QIconEngine
. The default engine scales pixmaps down if required, but never up, and it uses the current style to calculate a disabled appearance. By using custom icon engines, you can customize every aspect of generated icons. With QIconEnginePlugin
it is possible to register different icon engines for different file suffixes, making it possible for third parties to provide additional icon engines to those included with Qt.
Note
Since Qt 4.2, an icon engine that supports SVG is included.
Making Classes that Use QIcon#
If you write your own widgets that have an option to set a small pixmap, consider allowing a QIcon
to be set for that pixmap. The Qt class QToolButton is an example of such a widget.
Provide a method to set a QIcon
, and when you draw the icon, choose whichever pixmap is appropriate for the current state of your widget. For example:
def drawIcon(self, painter, pos): pixmap = icon.pixmap(QSize(22, 22), isEnabled() ? QIcon.Normal super().__init__() isChecked() ? QIcon.On super().__init__() painter.drawPixmap(pos, pixmap)
You might also make use of the Active
mode, perhaps making your widget Active
when the mouse is over the widget (see QWidget::enterEvent()), while the mouse is pressed pending the release that will activate the function, or when it is the currently selected item. If the widget can be toggled, the “On” mode might be used to draw a different icon.
Note
QIcon
needs a QGuiApplication
instance before the icon is created.
High DPI Icons#
There are two ways that QIcon
supports high DPI icons: via addFile()
and fromTheme()
.
addFile()
is useful if you have your own custom directory structure and do not need to use the Freedesktop Icon Theme Specification . Icons created via this approach use Qt’s "@nx" high DPI syntax
.
Using fromTheme()
is necessary if you plan on following the Icon Theme Specification. To make QIcon
use the high DPI version of an image, add an additional entry to the appropriate index.theme
file:
[Icon Theme] Name=Test Comment=Test Theme Directories=32x32/actions,32x32@2/actions [32x32/actions] Size=32 Context=Actions Type=Fixed # High DPI version of the entry above. [32x32@2/actions] Size=32 Scale=2 Type=Fixed
Your icon theme directory would then look something like this:
├── 32x32 │ └── actions │ └── appointment-new.png ├── 32x32@2 │ └── actions │ └── appointment-new.png └── index.theme
- class PySide6.QtGui.QIcon#
PySide6.QtGui.QIcon(engine)
PySide6.QtGui.QIcon(other)
PySide6.QtGui.QIcon(pixmap)
PySide6.QtGui.QIcon(fileName)
- Parameters:
pixmap –
PySide6.QtGui.QPixmap
other –
PySide6.QtGui.QIcon
fileName – str
engine –
PySide6.QtGui.QIconEngine
Constructs a null icon.
Creates an icon with a specific icon engine
. The icon takes ownership of the engine.
Constructs a copy of other
. This is very fast.
Constructs an icon from a pixmap
.
Constructs an icon from the file with the given fileName
. The file will be loaded on demand.
If fileName
contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
The file name can refer to an actual file on disk or to one of the application’s embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application’s executable.
Use the supportedImageFormats()
and supportedImageFormats()
functions to retrieve a complete list of the supported file formats.
- PySide6.QtGui.QIcon.Mode#
This enum type describes the mode for which a pixmap is intended to be used. The currently defined modes are:
Constant
Description
QIcon.Normal
Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available.
QIcon.Disabled
Display the pixmap when the functionality represented by the icon is not available.
QIcon.Active
Display the pixmap when the functionality represented by the icon is available and the user is interacting with the icon, for example, moving the mouse over it or clicking it.
QIcon.Selected
Display the pixmap when the item represented by the icon is selected.
- PySide6.QtGui.QIcon.State#
This enum describes the state for which a pixmap is intended to be used. The state can be:
Constant
Description
QIcon.Off
Display the pixmap when the widget is in an “off” state
QIcon.On
Display the pixmap when the widget is in an “on” state
- PySide6.QtGui.QIcon.actualSize(size[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])#
- Parameters:
size –
PySide6.QtCore.QSize
mode –
Mode
state – State
- Return type:
Returns the actual size of the icon for the requested size
, mode
, and state
. The result might be smaller than requested, but never larger. The returned size is in device-independent pixels (This is relevant for high-dpi pixmaps.)
- PySide6.QtGui.QIcon.actualSize(window, size[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])
- Parameters:
window –
PySide6.QtGui.QWindow
size –
PySide6.QtCore.QSize
mode –
Mode
state – State
- Return type:
Note
This function is deprecated.
Use actualSize
(size) instead.
Returns the actual size of the icon for the requested window
size
, mode
, and state
.
The pixmap can be smaller than the requested size. The returned size is in device-independent pixels (This is relevant for high-dpi pixmaps.)
See also
- PySide6.QtGui.QIcon.addFile(fileName[, size=QSize()[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]]])#
- Parameters:
fileName – str
size –
PySide6.QtCore.QSize
mode –
Mode
state – State
Adds an image from the file with the given fileName
to the icon, as a specialization for size
, mode
and state
. The file will be loaded on demand. Note: custom icon engines are free to ignore additionally added pixmaps.
If fileName
contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
The file name can refer to an actual file on disk or to one of the application’s embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application’s executable.
Use the supportedImageFormats()
and supportedImageFormats()
functions to retrieve a complete list of the supported file formats.
If a high resolution version of the image exists (identified by the suffix @2x
on the base name), it is automatically loaded and added with the device pixel ratio set to a value of 2. This can be disabled by setting the environment variable QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING
(see QImageReader
).
Note
When you add a non-empty filename to a QIcon
, the icon becomes non-null, even if the file doesn’t exist or points to a corrupt file.
See also
addPixmap()
devicePixelRatio()
- PySide6.QtGui.QIcon.addPixmap(path)#
- Parameters:
path –
PyPathLike
- PySide6.QtGui.QIcon.addPixmap(pixmap[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])
- Parameters:
pixmap –
PySide6.QtGui.QPixmap
mode –
Mode
state – State
Adds pixmap
to the icon, as a specialization for mode
and state
.
Custom icon engines are free to ignore additionally added pixmaps.
See also
- PySide6.QtGui.QIcon.availableSizes([mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])#
Returns a list of available icon sizes for the specified mode
and state
.
- PySide6.QtGui.QIcon.cacheKey()#
- Return type:
int
Returns a number that identifies the contents of this QIcon
object. Distinct QIcon
objects can have the same key if they refer to the same contents.
The cacheKey() will change when the icon is altered via addPixmap()
or addFile()
.
Cache keys are mostly useful in conjunction with caching.
See also
- static PySide6.QtGui.QIcon.fallbackSearchPaths()#
- Return type:
list of strings
Returns the fallback search paths for icons.
The fallback search paths are consulted for standalone icon files if the current icon theme
or fallback icon theme
do not provide results for an icon lookup.
If not set, the fallback search paths will be defined by the platform.
- static PySide6.QtGui.QIcon.fallbackThemeName()#
- Return type:
str
Returns the name of the fallback icon theme.
If not set, the fallback icon theme will be defined by the platform.
Note
Platform fallback icon themes are only implemented on Freedesktop based systems at the moment, and the icon theme depends on your desktop settings.
See also
- static PySide6.QtGui.QIcon.fromTheme(name)#
- Parameters:
name – str
- Return type:
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns the QIcon
corresponding to name
in the current icon theme
.
If the current theme does not provide an icon for name
, the fallback icon theme
is consulted, before falling back to looking up standalone icon files in the fallback icon search path
.
To fetch an icon from the current icon theme:
undoicon = QIcon.fromTheme("edit-undo")
If an icon theme
has not been explicitly set via setThemeName()
a platform defined icon theme will be used.
Note
Platform icon themes is only implemented on Freedesktop based systems at the moment, following the Freedesktop Icon Naming Specification . In order to use themed icons on other platforms, you will have to bundle a compliant theme
in one of your themeSearchPaths()
, and set the appropriate themeName()
.
- static PySide6.QtGui.QIcon.fromTheme(name, fallback)
- Parameters:
name – str
fallback –
PySide6.QtGui.QIcon
- Return type:
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
This is an overloaded function.
Returns the QIcon
corresponding to name
in the current icon theme
.
If the current theme does not provide an icon for name
, the fallback icon theme
is consulted, before falling back to looking up standalone icon files in the fallback icon search path
.
If no icon is found fallback
is returned.
This is useful to provide a guaranteed fallback, regardless of whether the current set of icon themes and fallbacks paths support the requested icon.
For example:
undoicon = QIcon.fromTheme("edit-undo", QIcon(":/undo.png"))See also
- static PySide6.QtGui.QIcon.hasThemeIcon(name)#
- Parameters:
name – str
- Return type:
bool
Returns true
if there is an icon available for name
in the current icon theme or any of the fallbacks, as described by fromTheme()
, otherwise returns false
.
See also
- PySide6.QtGui.QIcon.isMask()#
- Return type:
bool
Returns true
if this icon has been marked as a mask image. Certain platforms render mask icons differently (for example, menu icons on macOS).
See also
- PySide6.QtGui.QIcon.isNull()#
- Return type:
bool
Returns true
if the icon is empty; otherwise returns false
.
An icon is empty if it has neither a pixmap nor a filename.
Note: Even a non-null icon might not be able to create valid pixmaps, eg. if the file does not exist or cannot be read.
- PySide6.QtGui.QIcon.name()#
- Return type:
str
Returns the name used to create the icon, if available.
Depending on the way the icon was created, it may have an associated name. This is the case for icons created with fromTheme()
.
See also
- PySide6.QtGui.QIcon.paint(painter, rect[, alignment=Qt.AlignCenter[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]]])#
- Parameters:
painter –
PySide6.QtGui.QPainter
rect –
PySide6.QtCore.QRect
alignment – Combination of
Qt.AlignmentFlag
mode –
Mode
state – State
Uses the painter
to paint the icon with specified alignment
, required mode
, and state
into the rectangle rect
.
See also
- PySide6.QtGui.QIcon.paint(painter, x, y, w, h[, alignment=Qt.AlignCenter[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]]])
- Parameters:
painter –
PySide6.QtGui.QPainter
x – int
y – int
w – int
h – int
alignment – Combination of
Qt.AlignmentFlag
mode –
Mode
state – State
This is an overloaded function.
Paints the icon into the rectangle QRect(x
, y
, w
, h
).
- PySide6.QtGui.QIcon.pixmap(window, size[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])#
- Parameters:
window –
PySide6.QtGui.QWindow
size –
PySide6.QtCore.QSize
mode –
Mode
state – State
- Return type:
Note
This function is deprecated.
Use pixmap(size, devicePixelRatio) instead.
Returns a pixmap with the requested window
size
, mode
, and state
, generating one if necessary.
The pixmap can be smaller than the requested size. If window
is on a high-dpi display the pixmap can be larger. In that case it will have a devicePixelRatio larger than 1.
See also
- PySide6.QtGui.QIcon.pixmap(size[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])
- Parameters:
size –
PySide6.QtCore.QSize
mode –
Mode
state – State
- Return type:
Returns a pixmap with the requested size
, mode
, and state
, generating one if necessary. The pixmap might be smaller than requested, but never larger, unless the device-pixel ratio of the returned pixmap is larger than 1.
See also
- PySide6.QtGui.QIcon.pixmap(size, devicePixelRatio[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])
- Parameters:
size –
PySide6.QtCore.QSize
devicePixelRatio – float
mode –
Mode
state – State
- Return type:
This is an overloaded function.
Returns a pixmap with the requested size
, devicePixelRatio
, mode
, and state
, generating one if necessary.
See also
- PySide6.QtGui.QIcon.pixmap(extent[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])
- Parameters:
- Return type:
This is an overloaded function.
Returns a pixmap of size QSize(extent
, extent
). The pixmap might be smaller than requested, but never larger, unless the device-pixel ratio of the returned pixmap is larger than 1.
- PySide6.QtGui.QIcon.pixmap(w, h[, mode=QIcon.Mode.Normal[, state=QIcon.State.Off]])
- Parameters:
- Return type:
This is an overloaded function.
Returns a pixmap of size QSize(w
, h
). The pixmap might be smaller than requested, but never larger, unless the device-pixel ratio of the returned pixmap is larger than 1.
- static PySide6.QtGui.QIcon.setFallbackSearchPaths(paths)#
- Parameters:
paths – list of strings
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Sets the fallback search paths for icons to paths
.
The fallback search paths are consulted for standalone icon files if the current icon theme
or fallback icon theme
do not provide results for an icon lookup.
For example:
QIcon.setFallbackSearchPaths(QIcon.fallbackSearchPaths() << "my/search/path")
- static PySide6.QtGui.QIcon.setFallbackThemeName(name)#
- Parameters:
name – str
Sets the fallback icon theme to name
.
The fallback icon theme is consulted for icons not provided by the current icon theme
, or if the current icon theme
does not exist.
The name
should correspond to theme in the same format as documented by setThemeName()
, and will be looked up in themeSearchPaths()
.
Note
Fallback icon themes should be set before creating QGuiApplication
, to ensure correct initialization.
- PySide6.QtGui.QIcon.setIsMask(isMask)#
- Parameters:
isMask – bool
Indicate that this icon is a mask image(boolean isMask
), and hence can potentially be modified based on where it’s displayed.
See also
- static PySide6.QtGui.QIcon.setThemeName(path)#
- Parameters:
path – str
Sets the current icon theme to name
.
The theme will be will be looked up in themeSearchPaths()
.
At the moment the only supported icon theme format is the Freedesktop Icon Theme Specification . The name
should correspond to a directory name in the themeSearchPath() containing an index.theme
file describing its contents.
- static PySide6.QtGui.QIcon.setThemeSearchPaths(searchpath)#
- Parameters:
searchpath – list of strings
Sets the search paths for icon themes to paths
.
The content of paths
should follow the theme format documented by setThemeName()
.
See also
- PySide6.QtGui.QIcon.swap(other)#
- Parameters:
other –
PySide6.QtGui.QIcon
Swaps icon other
with this icon. This operation is very fast and never fails.
- static PySide6.QtGui.QIcon.themeName()#
- Return type:
str
Returns the name of the current icon theme.
If not set, the current icon theme will be defined by the platform.
Note
Platform icon themes are only implemented on Freedesktop based systems at the moment, and the icon theme depends on your desktop settings.
- static PySide6.QtGui.QIcon.themeSearchPaths()#
- Return type:
list of strings
Returns the search paths for icon themes.
The default search paths will be defined by the platform. All platforms will also have the resource directory :\icons
as a fallback.