Skip to content

PythonQt.QtGui.QIcon

QIcon

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

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:

QToolButton *button = new QToolButton;
button->setIcon(QIcon("open.xpm"));

To undo a QIcon, simply set a null icon in its place:

button->setIcon(QIcon());

Use the QImageReader::supportedImageFormats() and QImageWriter::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:

void MyWidget::drawIcon(QPainter *painter, QPoint pos)
{
    QPixmap pixmap = icon.pixmap(QSize(22, 22),
                                   isEnabled() ? QIcon::Normal
                                               : QIcon::Disabled,
                                   isChecked() ? QIcon::On
                                               : QIcon::Off);
    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.

QIcon

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.org 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

Source: https://doc.qt.io/archives/qt-5.15/qicon.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
Active attribute Any
Disabled attribute Any
Mode attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
Normal attribute Any (shared captured text)
Off attribute Any
On attribute Any
Selected attribute Any
State attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
actualSize function (self, window: Any, size: Any, mode: Any, state: Any) -> Any X.actualSize(window, size, mode, state) -> PythonQt.QtCore.QSize
addFile function (self, fileName: str, size: Any, mode: Any, state: Any) -> None X.addFile(fileName, size, mode, state)
addPixmap function (self, pixmap: Any, mode: Any, state: Any) -> None X.addPixmap(pixmap, mode, state)
availableSizes function (self, mode: Any, state: Any) -> tuple X.availableSizes(mode, state) -> tuple
cacheKey function (self) -> int X.cacheKey() -> int
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
fromTheme function (self, name: str, fallback: Any) -> Any X.fromTheme(name, fallback) -> PythonQt.QtGui.QIcon
hasThemeIcon function (self, name: str) -> bool X.hasThemeIcon(name) -> bool
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
isMask function (self) -> bool X.isMask() -> bool
isNull function (self) -> bool X.isNull() -> bool
name function (self) -> str X.name() -> str
paint function (self, painter: Any, x: Any, y: Any, w: Any, h: Any, alignment: Any, mode: Any, state: Any) -> None X.paint(painter, x, y, w, h, alignment, mode, state)
pixmap function (self, window: Any, size: Any, mode: Any, state: Any) -> Any X.pixmap(window, size, mode, state) -> PythonQt.QtGui.QPixmap
readFrom function (self, arg__1: Any) -> Any (shared captured text)
setIsMask function (self, isMask: bool) -> None Indicate that this icon is a mask image(boolean isMask), and hence can potentially be modified based on where it's displayed.
setThemeName function (self, path: str) -> None Sets the current icon theme to name.
setThemeSearchPaths function (self, searchpath: Any) -> None Sets the search paths for icon themes to paths.
swap function (self, other: Any) -> None Swaps icon other with this icon.
themeName function (self) -> str X.themeName() -> str
themeSearchPaths function (self) -> tuple X.themeSearchPaths() -> tuple
writeTo function (self, arg__1: Any) -> Any (shared captured text)