QIcon Class

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

Header: #include <QIcon>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

Public Types

enum Mode { Normal, Disabled, Active, Selected }
enum State { Off, On }
enum class ThemeIcon { AddressBookNew, ApplicationExit, AppointmentNew, CallStart, CallStop, …, WeatherStorm }

Public Functions

QIcon()
QIcon(const QPixmap &pixmap)
QIcon(const QString &fileName)
QIcon(QIconEngine *engine)
QIcon(const QIcon &other)
QIcon(QIcon &&other)
~QIcon()
QSize actualSize(const QSize &size, QIcon::Mode mode = Normal, QIcon::State state = Off) const
void addFile(const QString &fileName, const QSize &size = QSize(), QIcon::Mode mode = Normal, QIcon::State state = Off)
void addPixmap(const QPixmap &pixmap, QIcon::Mode mode = Normal, QIcon::State state = Off)
QList<QSize> availableSizes(QIcon::Mode mode = Normal, QIcon::State state = Off) const
qint64 cacheKey() const
bool isMask() const
bool isNull() const
QString name() const
void paint(QPainter *painter, const QRect &rect, Qt::Alignment alignment = Qt::AlignCenter, QIcon::Mode mode = Normal, QIcon::State state = Off) const
void paint(QPainter *painter, int x, int y, int w, int h, Qt::Alignment alignment = Qt::AlignCenter, QIcon::Mode mode = Normal, QIcon::State state = Off) const
QPixmap pixmap(const QSize &size, QIcon::Mode mode = Normal, QIcon::State state = Off) const
QPixmap pixmap(int w, int h, QIcon::Mode mode = Normal, QIcon::State state = Off) const
QPixmap pixmap(int extent, QIcon::Mode mode = Normal, QIcon::State state = Off) const
(since 6.0) QPixmap pixmap(const QSize &size, qreal devicePixelRatio, QIcon::Mode mode = Normal, QIcon::State state = Off) const
void setIsMask(bool isMask)
void swap(QIcon &other)
QVariant operator QVariant() const
QIcon &operator=(const QIcon &other)
QIcon &operator=(QIcon &&other)

Static Public Members

QStringList fallbackSearchPaths()
QString fallbackThemeName()
QIcon fromTheme(const QString &name)
QIcon fromTheme(const QString &name, const QIcon &fallback)
(since 6.7) QIcon fromTheme(QIcon::ThemeIcon icon)
(since 6.7) QIcon fromTheme(QIcon::ThemeIcon icon, const QIcon &fallback)
bool hasThemeIcon(const QString &name)
(since 6.7) bool hasThemeIcon(QIcon::ThemeIcon icon)
void setFallbackSearchPaths(const QStringList &paths)
void setFallbackThemeName(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)

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 UI components to show an icon representing a particular action.

Creating an icon from image files

The simplest way to construct a QIcon is to create one from one or several image files or resources. For example:

 QToolButton *button = new QToolButton;
 button->setIcon(QIcon("open.png"));

QIcon can store several images for different states, and Qt will select the image that is the closest match for the action's current state.

 QIcon openIcon("open.png");
 openIcon.addFile("open-disabled.png", QIcon::Disabled);

Qt will generate the required icon styles and sizes when needed, e.g. the pixmap for the QIcon::Disabled state might be generated by graying out one of the provided pixmaps.

To clear the icon, 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.

Creating an icon from a theme or icon library

The most convenient way to construct an icon is by using the fromTheme() factory function. Qt implements access to the native icon library on platforms that support the Freedesktop Icon Theme Specification. Since Qt 6.7, Qt also provides access to the native icon library on macOS, iOS, and Windows 10 and 11. On Android, Qt can access icons from the Material design system as long as the MaterialIcons-Regular font is available on the system, or bundled as a resource at :/qt-project.org/icons/MaterialIcons-Regular.ttf with the application.

 QIcon undoicon = QIcon::fromTheme(QIcon::ThemeIcon::EditUndo);

Applications can use the same theming specification to provide their own icon library. See below for an example theme description and the corresponding directory structure for the image files. Icons from an application-provided theme take precedence over the native icon library.

In addition, it is possible to provide custom icon engines. This allows applications to 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.

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 paint the QIcon with paint, choosing the appropriate parameters based on the current state of your widget. For example:

 void MyWidget::drawIcon(QPainter *painter, const QRect &rect)
 {
     icon.paint(painter, rect, Qt::AlignCenter, isEnabled() ? QIcon::Normal
                                                            : QIcon::Disabled,
                                                isChecked() ? QIcon::On
                                                            : QIcon::Off);
 }

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.

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

Icons that are provided by the native icon library are usually based on vector graphics, and will automatically be rendered in the appropriate resolution.

When providing your own image files via addFile(), then QIcon will use Qt's "@nx" high DPI syntax. This is useful if you have your own custom directory structure and do not use follow Freedesktop Icon Theme Specification.

When providing an application theme, then you need to follow the Icon Theme Specification to specify which files to use for different resolutions. 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

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

enum class QIcon::ThemeIcon

This enum provides access to icons that are provided by most icon theme implementations.

ConstantValue
QIcon::ThemeIcon::AddressBookNew0
QIcon::ThemeIcon::ApplicationExit1
QIcon::ThemeIcon::AppointmentNew2
QIcon::ThemeIcon::CallStart3
QIcon::ThemeIcon::CallStop4
QIcon::ThemeIcon::ContactNew5
QIcon::ThemeIcon::DocumentNew6
QIcon::ThemeIcon::DocumentOpen7
QIcon::ThemeIcon::DocumentOpenRecent8
QIcon::ThemeIcon::DocumentPageSetup9
QIcon::ThemeIcon::DocumentPrint10
QIcon::ThemeIcon::DocumentPrintPreview11
QIcon::ThemeIcon::DocumentProperties12
QIcon::ThemeIcon::DocumentRevert13
QIcon::ThemeIcon::DocumentSave14
QIcon::ThemeIcon::DocumentSaveAs15
QIcon::ThemeIcon::DocumentSend16
QIcon::ThemeIcon::EditClear17
QIcon::ThemeIcon::EditCopy18
QIcon::ThemeIcon::EditCut19
QIcon::ThemeIcon::EditDelete20
QIcon::ThemeIcon::EditFind21
QIcon::ThemeIcon::EditFindReplace22
QIcon::ThemeIcon::EditPaste23
QIcon::ThemeIcon::EditRedo24
QIcon::ThemeIcon::EditSelectAll25
QIcon::ThemeIcon::EditUndo26
QIcon::ThemeIcon::FolderNew27
QIcon::ThemeIcon::FormatIndentLess28
QIcon::ThemeIcon::FormatIndentMore29
QIcon::ThemeIcon::FormatJustifyCenter30
QIcon::ThemeIcon::FormatJustifyFill31
QIcon::ThemeIcon::FormatJustifyLeft32
QIcon::ThemeIcon::FormatJustifyRight33
QIcon::ThemeIcon::FormatTextDirectionLtr34
QIcon::ThemeIcon::FormatTextDirectionRtl35
QIcon::ThemeIcon::FormatTextBold36
QIcon::ThemeIcon::FormatTextItalic37
QIcon::ThemeIcon::FormatTextUnderline38
QIcon::ThemeIcon::FormatTextStrikethrough39
QIcon::ThemeIcon::GoBottom40
QIcon::ThemeIcon::GoDown41
QIcon::ThemeIcon::GoFirst42
QIcon::ThemeIcon::GoHome43
QIcon::ThemeIcon::GoJump44
QIcon::ThemeIcon::GoLast45
QIcon::ThemeIcon::GoNext46
QIcon::ThemeIcon::GoPrevious47
QIcon::ThemeIcon::GoTop48
QIcon::ThemeIcon::GoUp49
QIcon::ThemeIcon::HelpAbout50
QIcon::ThemeIcon::HelpContents51
QIcon::ThemeIcon::HelpFaq52
QIcon::ThemeIcon::InsertImage53
QIcon::ThemeIcon::InsertLink54
QIcon::ThemeIcon::InsertObject55
QIcon::ThemeIcon::InsertText56
QIcon::ThemeIcon::ListAdd57
QIcon::ThemeIcon::ListRemove58
QIcon::ThemeIcon::MailForward59
QIcon::ThemeIcon::MailMarkImportant60
QIcon::ThemeIcon::MailMarkJunk61
QIcon::ThemeIcon::MailMarkNotjunk62
QIcon::ThemeIcon::MailMarkRead63
QIcon::ThemeIcon::MailMarkUnread64
QIcon::ThemeIcon::MailMessageNew65
QIcon::ThemeIcon::MailReplyAll66
QIcon::ThemeIcon::MailReplySender67
QIcon::ThemeIcon::MailSend68
QIcon::ThemeIcon::MailSendReceive69
QIcon::ThemeIcon::MediaEject70
QIcon::ThemeIcon::MediaPlaybackPause71
QIcon::ThemeIcon::MediaPlaybackStart72
QIcon::ThemeIcon::MediaPlaybackStop73
QIcon::ThemeIcon::MediaRecord74
QIcon::ThemeIcon::MediaSeekBackward75
QIcon::ThemeIcon::MediaSeekForward76
QIcon::ThemeIcon::MediaSkipBackward77
QIcon::ThemeIcon::MediaSkipForward78
QIcon::ThemeIcon::ObjectFlipHorizontal79
QIcon::ThemeIcon::ObjectFlipVertical80
QIcon::ThemeIcon::ObjectRotateLeft81
QIcon::ThemeIcon::ObjectRotateRight82
QIcon::ThemeIcon::ProcessStop83
QIcon::ThemeIcon::SystemLockScreen84
QIcon::ThemeIcon::SystemLogOut85
QIcon::ThemeIcon::SystemRun86
QIcon::ThemeIcon::SystemSearch87
QIcon::ThemeIcon::SystemReboot88
QIcon::ThemeIcon::SystemShutdown89
QIcon::ThemeIcon::ToolsCheckSpelling90
QIcon::ThemeIcon::ViewFullscreen91
QIcon::ThemeIcon::ViewRefresh92
QIcon::ThemeIcon::ViewRestore93
QIcon::ThemeIcon::ViewSortAscending94
QIcon::ThemeIcon::ViewSortDescending95
QIcon::ThemeIcon::WindowClose96
QIcon::ThemeIcon::WindowNew97
QIcon::ThemeIcon::ZoomFitBest98
QIcon::ThemeIcon::ZoomIn99
QIcon::ThemeIcon::ZoomOriginal100
QIcon::ThemeIcon::ZoomOut101
QIcon::ThemeIcon::ProcessWorking102
QIcon::ThemeIcon::AccessoriesCalculator103
QIcon::ThemeIcon::AccessoriesCharacterMap104
QIcon::ThemeIcon::AccessoriesDictionary105
QIcon::ThemeIcon::AccessoriesTextEditor106
QIcon::ThemeIcon::HelpBrowser107
QIcon::ThemeIcon::MultimediaVolumeControl108
QIcon::ThemeIcon::PreferencesDesktopAccessibility109
QIcon::ThemeIcon::PreferencesDesktopFont110
QIcon::ThemeIcon::PreferencesDesktopKeyboard111
QIcon::ThemeIcon::PreferencesDesktopLocale112
QIcon::ThemeIcon::PreferencesDesktopMultimedia113
QIcon::ThemeIcon::PreferencesDesktopScreensaver114
QIcon::ThemeIcon::PreferencesDesktopTheme115
QIcon::ThemeIcon::PreferencesDesktopWallpaper116
QIcon::ThemeIcon::SystemFileManager117
QIcon::ThemeIcon::SystemSoftwareInstall118
QIcon::ThemeIcon::SystemSoftwareUpdate119
QIcon::ThemeIcon::UtilitiesSystemMonitor120
QIcon::ThemeIcon::UtilitiesTerminal121
QIcon::ThemeIcon::ApplicationsAccessories122
QIcon::ThemeIcon::ApplicationsDevelopment123
QIcon::ThemeIcon::ApplicationsEngineering124
QIcon::ThemeIcon::ApplicationsGames125
QIcon::ThemeIcon::ApplicationsGraphics126
QIcon::ThemeIcon::ApplicationsInternet127
QIcon::ThemeIcon::ApplicationsMultimedia128
QIcon::ThemeIcon::ApplicationsOffice129
QIcon::ThemeIcon::ApplicationsOther130
QIcon::ThemeIcon::ApplicationsScience131
QIcon::ThemeIcon::ApplicationsSystem132
QIcon::ThemeIcon::ApplicationsUtilities133
QIcon::ThemeIcon::PreferencesDesktop134
QIcon::ThemeIcon::PreferencesDesktopPeripherals135
QIcon::ThemeIcon::PreferencesDesktopPersonal136
QIcon::ThemeIcon::PreferencesOther137
QIcon::ThemeIcon::PreferencesSystem138
QIcon::ThemeIcon::PreferencesSystemNetwork139
QIcon::ThemeIcon::SystemHelp140
QIcon::ThemeIcon::AudioCard141
QIcon::ThemeIcon::AudioInputMicrophone142
QIcon::ThemeIcon::Battery143
QIcon::ThemeIcon::CameraPhoto144
QIcon::ThemeIcon::CameraVideo145
QIcon::ThemeIcon::CameraWeb146
QIcon::ThemeIcon::Computer147
QIcon::ThemeIcon::DriveHarddisk148
QIcon::ThemeIcon::DriveOptical149
QIcon::ThemeIcon::DriveRemovableMedia150
QIcon::ThemeIcon::InputGaming151
QIcon::ThemeIcon::InputKeyboard152
QIcon::ThemeIcon::InputMouse153
QIcon::ThemeIcon::InputTablet154
QIcon::ThemeIcon::MediaFlash155
QIcon::ThemeIcon::MediaFloppy156
QIcon::ThemeIcon::MediaOptical157
QIcon::ThemeIcon::MediaTape158
QIcon::ThemeIcon::Modem159
QIcon::ThemeIcon::MultimediaPlayer160
QIcon::ThemeIcon::NetworkWired161
QIcon::ThemeIcon::NetworkWireless162
QIcon::ThemeIcon::Pda163
QIcon::ThemeIcon::Phone164
QIcon::ThemeIcon::Printer165
QIcon::ThemeIcon::Scanner166
QIcon::ThemeIcon::VideoDisplay167
QIcon::ThemeIcon::EmblemDefault168
QIcon::ThemeIcon::EmblemDocuments169
QIcon::ThemeIcon::EmblemDownloads170
QIcon::ThemeIcon::EmblemFavorite171
QIcon::ThemeIcon::EmblemImportant172
QIcon::ThemeIcon::EmblemMail173
QIcon::ThemeIcon::EmblemPhotos174
QIcon::ThemeIcon::EmblemReadonly175
QIcon::ThemeIcon::EmblemShared176
QIcon::ThemeIcon::EmblemSymbolicLink177
QIcon::ThemeIcon::EmblemSynchronized178
QIcon::ThemeIcon::EmblemSystem179
QIcon::ThemeIcon::EmblemUnreadable180
QIcon::ThemeIcon::AppointmentMissed181
QIcon::ThemeIcon::AppointmentSoon182
QIcon::ThemeIcon::AudioVolumeHigh183
QIcon::ThemeIcon::AudioVolumeLow184
QIcon::ThemeIcon::AudioVolumeMedium185
QIcon::ThemeIcon::AudioVolumeMuted186
QIcon::ThemeIcon::BatteryCaution187
QIcon::ThemeIcon::BatteryLow188
QIcon::ThemeIcon::DialogError189
QIcon::ThemeIcon::DialogInformation190
QIcon::ThemeIcon::DialogPassword191
QIcon::ThemeIcon::DialogQuestion192
QIcon::ThemeIcon::DialogWarning193
QIcon::ThemeIcon::FolderDragAccept194
QIcon::ThemeIcon::FolderOpen195
QIcon::ThemeIcon::FolderVisiting196
QIcon::ThemeIcon::ImageLoading197
QIcon::ThemeIcon::ImageMissing198
QIcon::ThemeIcon::MailAttachment199
QIcon::ThemeIcon::MailUnread200
QIcon::ThemeIcon::MailRead201
QIcon::ThemeIcon::MailReplied202
QIcon::ThemeIcon::MailSigned203
QIcon::ThemeIcon::MailSignedVerified204
QIcon::ThemeIcon::MediaPlaylistRepeat205
QIcon::ThemeIcon::MediaPlaylistShuffle206
QIcon::ThemeIcon::NetworkError207
QIcon::ThemeIcon::NetworkIdle208
QIcon::ThemeIcon::NetworkOffline209
QIcon::ThemeIcon::NetworkReceive210
QIcon::ThemeIcon::NetworkTransmit211
QIcon::ThemeIcon::NetworkTransmitReceive212
QIcon::ThemeIcon::PrinterError213
QIcon::ThemeIcon::PrinterPrinting214
QIcon::ThemeIcon::SecurityHigh215
QIcon::ThemeIcon::SecurityMedium216
QIcon::ThemeIcon::SecurityLow217
QIcon::ThemeIcon::SoftwareUpdateAvailable218
QIcon::ThemeIcon::SoftwareUpdateUrgent219
QIcon::ThemeIcon::SyncError220
QIcon::ThemeIcon::SyncSynchronizing221
QIcon::ThemeIcon::TaskDue222
QIcon::ThemeIcon::TaskPastDue223
QIcon::ThemeIcon::UserAvailable224
QIcon::ThemeIcon::UserAway225
QIcon::ThemeIcon::UserIdle226
QIcon::ThemeIcon::UserOffline227
QIcon::ThemeIcon::UserTrashFull228
QIcon::ThemeIcon::WeatherClear229
QIcon::ThemeIcon::WeatherClearNight230
QIcon::ThemeIcon::WeatherFewClouds231
QIcon::ThemeIcon::WeatherFewCloudsNight232
QIcon::ThemeIcon::WeatherFog233
QIcon::ThemeIcon::WeatherOvercast234
QIcon::ThemeIcon::WeatherSevereAlert235
QIcon::ThemeIcon::WeatherShowers236
QIcon::ThemeIcon::WeatherShowersScattered237
QIcon::ThemeIcon::WeatherSnow238
QIcon::ThemeIcon::WeatherStorm239

See also QIcon#Creating an icon from a theme or icon library and fromTheme().

Member Function Documentation

[static, since 6.7] QIcon QIcon::fromTheme(QIcon::ThemeIcon icon)

[static, since 6.7] QIcon QIcon::fromTheme(QIcon::ThemeIcon icon, const QIcon &fallback)

This is an overloaded function.

Returns the QIcon corresponding to icon in the current icon theme.

If the current theme does not provide an icon for icon, the fallback icon theme is consulted, before falling back to looking up standalone icon files in the fallback icon search path. Finally, the platform's native icon library is consulted.

If no icon is found and a fallback is provided, fallback is returned. This is useful to provide a guaranteed fallback, regardless of whether the current set of icon themes and fallbacks paths support the requested icon.

If no icon is found and no fallback is provided, a default constructed, empty QIcon is returned.

This function was introduced in Qt 6.7.

[noexcept] QIcon::QIcon()

Constructs a null icon.

QIcon::QIcon(const QPixmap &pixmap)

Constructs an icon from a pixmap.

[explicit] QIcon::QIcon(const QString &fileName)

Constructs an icon from the file with the given fileName. The file will be loaded on demand.

If fileName 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 refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

Use the QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormats() functions to retrieve a complete list of the supported file formats.

[explicit] QIcon::QIcon(QIconEngine *engine)

Creates an icon with a specific icon engine. The icon takes ownership of the engine.

QIcon::QIcon(const QIcon &other)

Constructs a copy of other. This is very fast.

[noexcept] QIcon::QIcon(QIcon &&other)

Move-constructs a QIcon instance, making it point to the same object that other was pointing to.

[noexcept] QIcon::~QIcon()

Destroys the icon.

QSize QIcon::actualSize(const QSize &size, QIcon::Mode mode = Normal, QIcon::State state = Off) const

Returns the actual size of the icon for the requested size, mode, and state. The result might be smaller than requested, but never larger. The returned size is in device-independent pixels (This is relevant for high-dpi pixmaps.)

See also pixmap() and paint().

void QIcon::addFile(const QString &fileName, const QSize &size = QSize(), QIcon::Mode mode = Normal, QIcon::State state = Off)

Adds an image from the file with the given fileName to the icon, as a specialization for size, mode and state. The file will be loaded on demand. Note: custom icon engines are free to ignore additionally added pixmaps.

If fileName 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 refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

Use the QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormats() functions to retrieve a complete list of the supported file formats.

If a high resolution version of the image exists (identified by the suffix @2x on the base name), it is automatically loaded and added with the device pixel ratio set to a value of 2. This can be disabled by setting the environment variable QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING (see QImageReader).

Note: When you add a non-empty filename to a QIcon, the icon becomes non-null, even if the file doesn't exist or points to a corrupt file.

See also addPixmap() and QPixmap::devicePixelRatio().

void QIcon::addPixmap(const QPixmap &pixmap, QIcon::Mode mode = Normal, QIcon::State state = Off)

Adds pixmap to the icon, as a specialization for mode and state.

Custom icon engines are free to ignore additionally added pixmaps.

See also addFile().

QList<QSize> QIcon::availableSizes(QIcon::Mode mode = Normal, QIcon::State state = Off) const

Returns a list of available icon sizes for the specified mode and state.

qint64 QIcon::cacheKey() const

Returns a number that identifies the contents of this QIcon object. Distinct QIcon objects can have the same key if they refer to the same contents.

The cacheKey() will change when the icon is altered via addPixmap() or addFile().

Cache keys are mostly useful in conjunction with caching.

See also QPixmap::cacheKey().

[static] QStringList QIcon::fallbackSearchPaths()

Returns the fallback search paths for icons.

The fallback search paths are consulted for standalone icon files if the current icon theme or fallback icon theme do not provide results for an icon lookup.

If not set, the fallback search paths will be defined by the platform.

See also setFallbackSearchPaths() and themeSearchPaths().

[static] QString QIcon::fallbackThemeName()

Returns the name of the fallback icon theme.

If not set, the fallback icon theme will be defined by the platform.

Note: Platform fallback icon themes are only implemented on Freedesktop based systems at the moment, and the icon theme depends on your desktop settings.

See also setFallbackThemeName() and themeName().

[static] QIcon QIcon::fromTheme(const QString &name)

Returns the QIcon corresponding to name in the current icon theme.

If the current theme does not provide an icon for name, the fallback icon theme is consulted, before falling back to looking up standalone icon files in the fallback icon search path. Finally, the platform's native icon library is consulted.

To fetch an icon from the current icon theme:

 QIcon undoicon = QIcon::fromTheme(QIcon::ThemeIcon::EditUndo);

If an icon theme has not been explicitly set via setThemeName() a platform defined icon theme will be used.

See also themeName(), fallbackThemeName(), setThemeName(), themeSearchPaths(), fallbackSearchPaths(), and Freedesktop Icon Naming Specification.

[static] QIcon QIcon::fromTheme(const QString &name, const QIcon &fallback)

This is an overloaded function.

Returns the QIcon corresponding to name in the current icon theme.

If the current theme does not provide an icon for name, the fallback icon theme is consulted, before falling back to looking up standalone icon files in the fallback icon search path. Finally, the platform's native icon library is consulted.

If no icon is found fallback is returned.

This is useful to provide a guaranteed fallback, regardless of whether the current set of icon themes and fallbacks paths support the requested icon.

For example:

 QIcon undoicon = QIcon::fromTheme(QIcon::ThemeIcon::EditUndo, QIcon(":/undo.png"));

See also fallbackThemeName() and fallbackSearchPaths().

[static] bool QIcon::hasThemeIcon(const QString &name)

Returns true if there is an icon available for name in the current icon theme or any of the fallbacks, as described by fromTheme(), otherwise returns false.

See also themeSearchPaths(), fromTheme(), and setThemeName().

[static, since 6.7] bool QIcon::hasThemeIcon(QIcon::ThemeIcon icon)

This is an overloaded function.

Returns true if there is an icon available for icon in the current icon theme or any of the fallbacks, as described by fromTheme(), otherwise returns false.

This function was introduced in Qt 6.7.

See also fromTheme().

bool QIcon::isMask() const

Returns true if this icon has been marked as a mask image. Certain platforms render mask icons differently (for example, menu icons on macOS).

See also setIsMask().

bool QIcon::isNull() const

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.

QString QIcon::name() const

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 with fromTheme().

See also fromTheme() and QIconEngine::iconName().

void QIcon::paint(QPainter *painter, const QRect &rect, Qt::Alignment alignment = Qt::AlignCenter, QIcon::Mode mode = Normal, QIcon::State state = Off) const

Uses the painter to paint the icon with specified alignment, required mode, and state into the rectangle rect.

See also actualSize() and pixmap().

void QIcon::paint(QPainter *painter, int x, int y, int w, int h, Qt::Alignment alignment = Qt::AlignCenter, QIcon::Mode mode = Normal, QIcon::State state = Off) const

This is an overloaded function.

Paints the icon into the rectangle QRect(x, y, w, h).

QPixmap QIcon::pixmap(const QSize &size, QIcon::Mode mode = Normal, QIcon::State state = Off) const

Returns a pixmap with the requested size, mode, and state, generating one if necessary. The pixmap might be smaller than requested, but never larger, unless the device-pixel ratio of the returned pixmap is larger than 1.

See also actualSize() and paint().

QPixmap QIcon::pixmap(int w, int h, QIcon::Mode mode = Normal, QIcon::State state = Off) const

This is an overloaded function.

Returns a pixmap of size QSize(w, h). The pixmap might be smaller than requested, but never larger, unless the device-pixel ratio of the returned pixmap is larger than 1.

QPixmap QIcon::pixmap(int extent, QIcon::Mode mode = Normal, QIcon::State state = Off) const

This is an overloaded function.

Returns a pixmap of size QSize(extent, extent). The pixmap might be smaller than requested, but never larger, unless the device-pixel ratio of the returned pixmap is larger than 1.

[since 6.0] QPixmap QIcon::pixmap(const QSize &size, qreal devicePixelRatio, QIcon::Mode mode = Normal, QIcon::State state = Off) const

This is an overloaded function.

Returns a pixmap with the requested size, devicePixelRatio, mode, and state, generating one if necessary.

This function was introduced in Qt 6.0.

See also actualSize() and paint().

[static] void QIcon::setFallbackSearchPaths(const QStringList &paths)

Sets the fallback search paths for icons to paths.

The fallback search paths are consulted for standalone icon files if the current icon theme or fallback icon theme do not provide results for an icon lookup.

For example:

 QIcon::setFallbackSearchPaths(QIcon::fallbackSearchPaths() << "my/search/path");

See also fallbackSearchPaths() and setThemeSearchPaths().

[static] void QIcon::setFallbackThemeName(const QString &name)

Sets the fallback icon theme to name.

The fallback icon theme is consulted for icons not provided by the current icon theme, or if the current icon theme does not exist.

The name should correspond to theme in the same format as documented by setThemeName(), and will be looked up in themeSearchPaths().

Note: Fallback icon themes should be set before creating QGuiApplication, to ensure correct initialization.

See also fallbackThemeName(), themeSearchPaths(), and themeName().

void QIcon::setIsMask(bool isMask)

Indicate that this icon is a mask image(boolean isMask), and hence can potentially be modified based on where it's displayed.

See also isMask().

[static] void QIcon::setThemeName(const QString &name)

Sets the current icon theme to name.

The theme will be will be looked up in themeSearchPaths().

At the moment the only supported icon theme format is the Freedesktop Icon Theme Specification. The name should correspond to a directory name in the themeSearchPath() containing an index.theme file describing its contents.

See also themeSearchPaths(), themeName(), and Freedesktop Icon Theme Specification.

[static] void QIcon::setThemeSearchPaths(const QStringList &paths)

Sets the search paths for icon themes to paths.

The content of paths should follow the theme format documented by setThemeName().

See also themeSearchPaths(), fromTheme(), and setThemeName().

[noexcept] void QIcon::swap(QIcon &other)

Swaps icon other with this icon. This operation is very fast and never fails.

[static] QString QIcon::themeName()

Returns the name of the current icon theme.

If not set, the current icon theme will be defined by the platform.

Note: Platform icon themes are only implemented on Freedesktop based systems at the moment, and the icon theme depends on your desktop settings.

See also setThemeName(), themeSearchPaths(), fromTheme(), and hasThemeIcon().

[static] QStringList QIcon::themeSearchPaths()

Returns the search paths for icon themes.

The default search paths will be defined by the platform. All platforms will also have the resource directory :\icons as a fallback.

See also setThemeSearchPaths(), fromTheme(), and setThemeName().

QVariant QIcon::operator QVariant() const

Returns the icon as a QVariant.

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

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

[noexcept] QIcon &QIcon::operator=(QIcon &&other)

Move-assigns other to this QIcon instance.

Related Non-Members

QDataStream &operator<<(QDataStream &stream, const QIcon &icon)

Writes the given icon to the given stream 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.

QDataStream &operator>>(QDataStream &stream, QIcon &icon)

Reads an image, or a set of images, from the given stream into the given icon.