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

QIcon Class Reference

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

#include <QIcon>

Public Types

Public Functions

Static Public Members


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(Qt::SmallIconSize,
                                       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.

QIcon

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::NormalDisplay the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available.
QIcon::DisabledDisplay the pixmap when the functionality represented by the icon is not available.
QIcon::ActiveDisplay 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::State

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

QIcon::OffDisplay the pixmap when the widget is in an "off" state
QIcon::OnDisplay 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, Qt::IconSize size = Qt::AutomaticIconSize )

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 ( Qt::IconSize size, Mode mode, State state = Off ) const

Returns true if the pixmap used for the icon with the given combination of size, mode, and state is generated from another pixmap; otherwise returns false.

See also pixmap() and setPixmap().

bool QIcon::isNull () const

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

QPixmap QIcon::pixmap ( Qt::IconSize 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 ( Qt::IconSize 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, Qt::IconSize 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, Qt::IconSize 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, Qt::IconSize 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 ( Qt::IconSize 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().


Copyright © 2004 Trolltech Trademarks
Qt 4.0.0-b1