QIcon Class

The QIcon class provides scalable icons in different modes and states. More...

Header: #include <QIcon>
qmake: QT += gui

Public Types

enum Mode { Normal, Disabled, Active, Selected }
enum State { Off, On }

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

See also GUI Design Handbook: Iconic Label and Icons Example.

Member Type Documentation

enum QIcon::Mode

This enum type describes the mode for which a pixmap is intended to be used. The currently defined modes are:

ConstantValueDescription
QIcon::Normal0Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available.
QIcon::Disabled1Display the pixmap when the functionality represented by the icon is not available.
QIcon::Active2Display 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::Selected3Display the pixmap when the item represented by the icon is selected.

enum QIcon::State

This enum describes the state for which a pixmap is intended to be used. The state can be:

ConstantValueDescription
QIcon::Off1Display the pixmap when the widget is in an "off" state
QIcon::On0Display the pixmap when the widget is in an "on" state