
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQIcon class provides scalable icons in different modes and states.More...
| Header: | #include <QIcon> |
| QIcon() | |
| QIcon(const QPixmap & pixmap) | |
| QIcon(const QIcon & other) | |
| QIcon(const QString & fileName) | |
| QIcon(QIconEngine * engine) | |
| QIcon(QIconEngineV2 * engine) | |
| ~QIcon() | |
| QSize | actualSize(const QSize & size, Mode mode = Normal, State state = Off) const |
| void | addFile(const QString & fileName, const QSize & size = QSize(), Mode mode = Normal, State state = Off) |
| void | addPixmap(const QPixmap & pixmap, Mode mode = Normal, State state = Off) |
| QList<QSize> | availableSizes(Mode mode = Normal, State state = Off) const |
| qint64 | cacheKey() const |
| bool | isNull() const |
| QString | name() const |
| void | paint(QPainter * painter, const QRect & rect, Qt::Alignment alignment = Qt::AlignCenter, Mode mode = Normal, State state = Off) const |
| void | paint(QPainter * painter, int x, int y, int w, int h, Qt::Alignment alignment = Qt::AlignCenter, Mode mode = Normal, State state = Off) const |
| QPixmap | pixmap(const QSize & size, Mode mode = Normal, State state = Off) const |
| QPixmap | pixmap(int w, int h, Mode mode = Normal, State state = Off) const |
| QPixmap | pixmap(int extent, Mode mode = Normal, State state = Off) const |
| void | swap(QIcon & other) |
| operator QVariant() const | |
| QIcon & | operator=(const QIcon & other) |
| QIcon & | operator=(QIcon && other) |
| QIcon | fromTheme(const QString & name, const QIcon & fallback = QIcon()) |
| bool | hasThemeIcon(const QString & name) |
| void | setThemeName(const QString & name) |
| void | setThemeSearchPaths(const QStringList & paths) |
| QString | themeName() |
| QStringList | themeSearchPaths() |
| QDataStream & | operator<<(QDataStream & stream, const QIcon & icon) |
| QDataStream & | operator>>(QDataStream & stream, QIcon & icon) |
TheQIcon class provides scalable icons in different modes and states.
AQIcon 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 ofQIcon is to create one from aQPixmap file or resource, and then use it, allowing Qt to work out all the required icon styles and sizes. For example:
QToolButton*button=newQToolButton;button->setIcon(QIcon("open.xpm"));
To undo aQIcon, simply set a null icon in its place:
button->setIcon(QIcon());
Use theQImageReader::supportedImageFormats() andQImageWriter::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 withaddFile() oraddPixmap(), thenQIcon will generate one on the fly. This pixmap generation happens in aQIconEngineV2. 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. WithQIconEnginePluginV2 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.
If you write your own widgets that have an option to set a small pixmap, consider allowing aQIcon to be set for that pixmap. The Qt classQToolButton is an example of such a widget.
Provide a method to set aQIcon, 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 theActive mode, perhaps making your widgetActive when the mouse is over the widget (seeQWidget::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 alsoGUI Design Handbook: Iconic Label andIcons Example.
This enum type describes the mode for which a pixmap is intended to be used. The currently defined modes are:
| Constant | Value | Description |
|---|---|---|
QIcon::Normal | 0 | Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available. |
QIcon::Disabled | 1 | Display the pixmap when the functionality represented by the icon is not available. |
QIcon::Active | 2 | 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. |
QIcon::Selected | 3 | Display the pixmap when the item represented by the icon is selected. |
This enum describes the state for which a pixmap is intended to be used. Thestate can be:
| Constant | Value | Description |
|---|---|---|
QIcon::Off | 1 | Display the pixmap when the widget is in an "off" state |
QIcon::On | 0 | Display the pixmap when the widget is in an "on" state |
Constructs a null icon.
Constructs an icon from apixmap.
Constructs a copy ofother. This is very fast.
Constructs an icon from the file with the givenfileName. The file will be loaded on demand.
IffileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See theResource System overview for details on how to embed images and other resource files in the application's executable.
Use theQImageReader::supportedImageFormats() andQImageWriter::supportedImageFormats() functions to retrieve a complete list of the supported file formats.
Creates an icon with a specific iconengine. The icon takes ownership of the engine.
Creates an icon with a specific iconengine. The icon takes ownership of the engine.
Destroys the icon.
Returns the actual size of the icon for the requestedsize,mode, andstate. The result might be smaller than requested, but never larger.
Adds an image from the file with the givenfileName to the icon, as a specialization forsize,mode andstate. The file will be loaded on demand. Note: custom icon engines are free to ignore additionally added pixmaps.
IffileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See theResource System overview for details on how to embed images and other resource files in the application's executable.
Use theQImageReader::supportedImageFormats() andQImageWriter::supportedImageFormats() functions to retrieve a complete list of the supported file formats.
Note: When you add a non-empty filename to aQIcon, the icon becomes non-null, even if the file doesn't exist or points to a corrupt file.
See alsoaddPixmap().
Addspixmap to the icon, as a specialization formode andstate.
Custom icon engines are free to ignore additionally added pixmaps.
See alsoaddFile().
Returns a list of available icon sizes for the specifiedmode andstate.
This function was introduced in Qt 4.5.
Returns a number that identifies the contents of thisQIcon object. DistinctQIcon objects can have the same key if they refer to the same contents.
The cacheKey() will change when the icon is altered viaaddPixmap() oraddFile().
Cache keys are mostly useful in conjunction with caching.
This function was introduced in Qt 4.3.
See alsoQPixmap::cacheKey().
[static]QIcon QIcon::fromTheme(constQString & name, constQIcon & fallback = QIcon())Returns theQIcon corresponding toname in the current icon theme. If no such icon is found in the current themefallback is returned instead.
The latest version of the freedesktop icon specification and naming specification can be obtained here:
To fetch an icon from the current icon theme:
Or if you want to provide a guaranteed fallback for platforms that do not support theme icons, you can use the second argument:
Note:By default, only X11 will support themed icons. In order to use themed icons on Mac and Windows, you will have to bundle a compliant theme in one of yourthemeSearchPaths() and set the appropriatethemeName().
This function was introduced in Qt 4.6.
See alsothemeName(),setThemeName(), andthemeSearchPaths().
[static]bool QIcon::hasThemeIcon(constQString & name)Returns true if there is an icon available forname in the current icon theme, otherwise returns false.
This function was introduced in Qt 4.6.
See alsothemeSearchPaths(),fromTheme(), andsetThemeName().
Returns true if the icon is empty; otherwise returns false.
An icon is empty if it has neither a pixmap nor a filename.
Note: Even a non-null icon might not be able to create valid pixmaps, eg. if the file does not exist or cannot be read.
Returns the name used to create the icon, if available.
Depending on the way the icon was created, it may have an associated name. This is the case for icons created withfromTheme() or icons using aQIconEngine which supports theQIconEngineV2::IconNameHook.
This function was introduced in Qt 4.7.
See alsofromTheme() andQIconEngine.
Uses thepainter to paint the icon with specifiedalignment, requiredmode, andstate into the rectanglerect.
See alsoactualSize() andpixmap().
This is an overloaded function.
Paints the icon into the rectangleQRect(x,y,w,h).
Returns a pixmap with the requestedsize,mode, andstate, generating one if necessary. The pixmap might be smaller than requested, but never larger.
See alsosetPixmap(),actualSize(), andpaint().
This is an overloaded function.
Returns a pixmap of sizeQSize(w,h). The pixmap might be smaller than requested, but never larger.
This is an overloaded function.
Returns a pixmap of sizeQSize(extent,extent). The pixmap might be smaller than requested, but never larger.
[static]void QIcon::setThemeName(constQString & name)Sets the current icon theme toname.
Thename should correspond to a directory name in the themeSearchPath() containing an index.theme file describing it's contents.
This function was introduced in Qt 4.6.
See alsothemeSearchPaths() andthemeName().
[static]void QIcon::setThemeSearchPaths(constQStringList & paths)Sets the search paths for icon themes topaths.
This function was introduced in Qt 4.6.
See alsothemeSearchPaths(),fromTheme(), andsetThemeName().
Swaps iconother with this icon. This operation is very fast and never fails.
This function was introduced in Qt 4.8.
[static]QString QIcon::themeName()Returns the name of the current icon theme.
On X11, the current icon theme depends on your desktop settings. On other platforms it is not set by default.
This function was introduced in Qt 4.6.
See alsosetThemeName(),themeSearchPaths(),fromTheme(), andhasThemeIcon().
[static]QStringList QIcon::themeSearchPaths()Returns the search paths for icon themes.
The default value will depend on the platform:
On X11, the search path will use the XDG_DATA_DIRS environment variable if available.
By default all platforms will have the resource directory:\icons as a fallback. You can use "rcc -project" to generate a resource file from your icon theme.
This function was introduced in Qt 4.6.
See alsosetThemeSearchPaths(),fromTheme(), andsetThemeName().
Returns the icon as aQVariant.
Assigns theother icon to this icon and returns a reference to this icon.
Writes the givenicon to the givenstream as a PNG image. If the icon contains more than one image, all images will be written to the stream. Note that writing the stream to a file will not produce a valid image file.
This function was introduced in Qt 4.2.
Reads an image, or a set of images, from the givenstream into the givenicon.
This function was introduced in Qt 4.2.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.