![]() |
Home · Overviews · Examples | ![]() |
The QIcon class provides scalable icons in different modes and states. More...
The QIcon class provides scalable icons in different modes and states.
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 QIconEngineV2. 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 QIconEnginePluginV2 it is possible to register different icon engines for different file suffixes, so you could provide a SVG icon engine or any other scalable format.
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, 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.
See also GUI Design Handbook: Iconic Label and Icons Example.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.3.4_01 |