Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QIcon Class Reference

The QIcon class provides different versions of an icon. More...

    #include <QIcon>

- List of all members, including inherited members
- Obsolete members
- Compatibility members

### Public Types

- enum Mode { Normal, Disabled, Active }
- enum Size { Automatic, Small, Large }
- enum State { Off, On }

### Public Functions

- QIcon ()
- QIcon ( const QPixmap & pixmap, Size size = Automatic )
- QIcon ( const QPixmap & smallPix, const QPixmap & largePix )
- QIcon ( const QIcon & other )
- ~QIcon ()
- void clearGenerated ()
- bool isGenerated ( Size size, Mode mode, State state = Off ) const
- bool isNull () const
- QPixmap pixmap ( Size size, Mode mode, State state = Off ) const
- QPixmap pixmap () const
- void reset ( const QPixmap & pixmap, Size size )
- void setPixmap ( const QPixmap & pixmap, Size size, Mode mode = Normal, State state = Off )
- void setPixmap ( const QString & fileName, Size size, Mode mode = Normal, State state = Off )
- void setPixmapGeneratorFn ( PixmapGeneratorFn func )
- QIcon & operator= ( const QIcon & other )

### Static Public Members

- PixmapGeneratorFn defaultPixmapGeneratorFn ()
- QSize pixmapSize ( Size which )
- void setDefaultPixmapGeneratorFn ( PixmapGeneratorFn func )
- void setPixmapSize ( Size which, const QSize & size )

## Detailed Description

The QIcon class provides different versions of an icon.

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 and then use it, allowing Qt to work out all the required icon styles and sizes. For example:

        QToolButton *but = new QToolButton(QIcon(QPixmap("open.xpm")), ...);

Using whichever pixmaps you specify as a base, QIcon provides a set of six pixmaps, each with a Size and a Mode: Small Normal, Small Disabled, Small Active, Large Normal, Large Disabled, and Large Active.

An additional set of six pixmaps can be provided for widgets that have an "On" or "Off" state, like checkable menu items or checkable toolbuttons. If you provide pixmaps for the "On" state, but not for the "Off" state, the QIcon will provide the "Off" pixmaps. You may specify pixmaps for both states in you wish.

You can set any of the pixmaps using setPixmap().

When you retrieve a pixmap using pixmap(Size, Mode, State), QIcon will return the pixmap that has been set or previously generated for that size, mode and state combination. If none is available, QIcon will ask the installed pixmap generator function. If the pixmap generator cannot provide any (the default), QIcon generates a pixmap based on the pixmaps it has been given and returns it.

The `Disabled` appearance is computed using the current style. The `Active` appearance is identical to the `Normal` appearance unless you use setPixmap() to set it to something special.

When scaling pixmaps, QIcon uses smooth scaling, which can partially blend the color component of pixmaps. If the results look poor, the best solution is to supply pixmaps in both large and small sizes.

You can use the static function setPixmapSize() to set the preferred size of the generated large/small pixmaps. The default small size is 22 x 22, while the default large size is 32 x 32. These sizes only affect generated pixmaps.

The isGenerated() function returns true if an icon was generated by QIcon or by a pixmap generator function; clearGenerated() clears all cached pixmaps.

### 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 = icons->pixmap(QIcon::Small,
                                           isEnabled() ? QIcon::Normal
                                                       : QIcon::Disabled,
                                           isOn() ? 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.

GUI Design Handbook: Iconic Label

See also QPixmap and QMainWindow::setUsesBigPixmaps().

## 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:

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

### enum QIcon::Size

This enum type describes the size at which a pixmap is intended to be used. The currently defined sizes are:

`QIcon::Automatic` | The size of the pixmap is determined from its pixel size. This is a useful default.
`QIcon::Small` | The pixmap is the smaller of two.
`QIcon::Large` | The pixmap is the larger of two.

If a Small pixmap is not set by setPixmap(), the Large pixmap will be automatically scaled down to the size of a small pixmap to generate the Small pixmap when required. Similarly, a Small pixmap will be automatically scaled up to generate a Large pixmap. The preferred sizes for large/small generated pixmaps can be set using setPixmapSize().

See also setPixmapSize(), pixmapSize(), setPixmap(), pixmap(), and QMainWindow::setUsesBigPixmaps().

### enum QIcon::State

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

`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

See also setPixmap() and pixmap().

## Member Function Documentation

### QIcon::QIcon ()

Constructs a null icon.

See also setPixmap() and reset().

### QIcon::QIcon ( const QPixmap & pixmap, Size size = Automatic )

Constructs an icon for which the Normal pixmap is pixmap, which is assumed to be of size size.

The default for size is `Automatic`, which means that QIcon will determine whether the pixmap is Small or Large from its pixel size. Pixmaps less than the width of a small generated icon are considered to be Small. You can use setPixmapSize() to set the preferred size of a generated icon.

See also setPixmapSize() and reset().

### QIcon::QIcon ( const QPixmap & smallPix, const QPixmap & largePix )

Creates an icon which uses the pixmap smallPix for for displaying a small icon, and the pixmap largePix for displaying a large icon.

### QIcon::QIcon ( const QIcon & other )

Constructs a copy of other. This is very fast.

### QIcon::~QIcon ()

Destroys the icon and frees any allocated resources.

### void QIcon::clearGenerated ()

Clears all cached pixmaps, including those obtained from an eventual QIconFactory.

### PixmapGeneratorFn QIcon::defaultPixmapGeneratorFn () `[static]`

Returns the default icon factory.

See also setPixmapGeneratorFn().

### bool QIcon::isGenerated ( Size size, Mode mode, State state = Off ) const

Returns true if the pixmap with size size, mode mode and state state is generated from other pixmaps; otherwise returns false.

### bool QIcon::isNull () const

Returns true if the icon is empty; otherwise returns false.

### QPixmap QIcon::pixmap ( Size size, Mode mode, State state = Off ) const

Returns a pixmap with the required size, mode, and state, generating one if necessary. Generated pixmaps are cached.

### QPixmap QIcon::pixmap () const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the pixmap originally provided to the constructor or to reset(). This is the Normal pixmap of unspecified Size.

See also reset().

### QSize QIcon::pixmapSize ( Size which ) `[static]`

If which is Small, returns the preferred size of a small generated icon; if which is Large, returns the preferred size of a large generated icon.

See also setPixmapSize().

### void QIcon::reset ( const QPixmap & pixmap, Size size )

Sets this icon to use the given pixmap for the `Normal` pixmap, assuming it to be of the size specified.

This is equivalent to assigning QIcon(pixmap, size) to this icon.

This function does nothing if pixmap is a null pixmap.

### void QIcon::setDefaultPixmapGeneratorFn ( PixmapGeneratorFn func ) `[static]`

Installs func as the application's default icon factory. The icon factory is used to generates pixmaps not set by the user.

If no icon factory is installed, QIconFactory::defaultFactory() is used. Individual QIcon instances can have their own icon factories; see setPixmapGeneratorFn().

### void QIcon::setPixmap ( const QPixmap & pixmap, Size size, Mode mode = Normal, State state = Off )

Sets the pixmap for this icon to be the given pixmap for requests of the same size, mode, and state. The icon may also use pixmap for generating other pixmaps if they are not explicitly set.

The size can be one of `Automatic`, `Large` or `Small`. If `Automatic` is used, QIcon will determine if the pixmap is `Small` or `Large` from its pixel size.

Pixmaps less than the width of a small generated icon are considered to be Small. You can use setPixmapSize() to set the preferred size of a generated icon.

This function does nothing if pixmap is a null pixmap.

See also reset().

### void QIcon::setPixmap ( const QString & fileName, Size size, Mode mode = Normal, State state = Off )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

The pixmap is loaded from fileName when it becomes necessary.

### void QIcon::setPixmapGeneratorFn ( PixmapGeneratorFn func )

Installs func as the pixmap factory for this icon. The pixmap factory is used to generates pixmaps not set by the user.

If no pixmap factory is installed, defaultPixmapGeneratorFn() is used.

See also defaultPixmapGeneratorFn().

### void QIcon::setPixmapSize ( Size which, const QSize & size ) `[static]`

Set the preferred size for all small or large pixmaps that are generated after this call. If which is Small, sets the preferred size of small generated pixmaps to size. Similarly, if which is Large, sets the preferred size of large generated pixmaps to size.

Note that cached pixmaps will not be regenerated, so it is recommended that you set the preferred icon sizes before generating any icon sets. Also note that the preferred icon sizes will be ignored for icon sets that have been created using both small and large pixmaps.

See also pixmapSize().

### QIcon & QIcon::operator= ( const QIcon & other )

Assigns other to this icon and returns a reference to this icon.

See also detach().