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 { On, Off }
(since 6.7) enum class ThemeIcon { AddressBookNew, ApplicationExit, AppointmentNew, CallStart, CallStop, …, WeatherStorm }

Public Functions

QIcon()
QIcon(QIconEngine *engine)
QIcon(const QPixmap &pixmap)
QIcon(const QString &fileName)
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 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
QPixmap pixmap(int w, int h, QIcon::Mode mode = Normal, QIcon::State state = Off) const
void setIsMask(bool isMask)
void swap(QIcon &other)
QVariant operator QVariant() const
QIcon &operator=(QIcon &&other)
QIcon &operator=(const QIcon &other)

Static Public Members

QStringList fallbackSearchPaths()
QString fallbackThemeName()
QIcon fromTheme(const QString &name)
(since 6.7) QIcon fromTheme(QIcon::ThemeIcon icon)
(since 6.7) QIcon fromTheme(QIcon::ThemeIcon icon, const QIcon &fallback)
QIcon fromTheme(const QString &name, 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.

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.

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);

Since Qt 6.9, Qt can generate icons from named glyphs in an available icon font. Set the theme name to the family name of the font, and use fromTheme() with the name of the glyph.

QIcon::setThemeName("Material Symbols Outlined");
QIcon muteIcon = QIcon::fromTheme(u"volume_off"_s);

The icon font can be installed on the system, or bundled as an application font.

Icon Engines

Internally, QIcon instantiates an icon engine backend to handle and render the icon images. The type of icon engine is determined by the first file or pixmap or theme added to a QIcon object. Additional files or pixmaps will then be handled by the same engine.

Icon engines differ in the way they handle and render icons. The default pixmap-based engine only deals with fixed images, while the QtSvg module provides an icon engine that can re-render the provided vector graphics files at the requested size for better quality. The theme icon engines will typically only provide images from native platform icon library, and ignore any added files or pixmaps.

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.

Using QIcon in the User Interface

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.

QIcons generated from the native icon library, or from an icon font, use the same glyph for both the On and Off states of the icon. Applications can change the icon depending on the state of the respective UI control or action. In a Qt Quick application, this can be done with a binding.

ToolButton {
    id: muteButton
    checkable: true
    icon.name: checked ? "volume_off" : "volume_up"
}

QIcon

Note: QIcon needs a QGuiApplication instance before the icon is created.

High DPI Icons

Icons that are provided by the native icon library, or generated from the glyph in an icon font, 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::On0Display the pixmap when the widget is in an "on" state
QIcon::Off1Display the pixmap when the widget is in an "off" state

[since 6.7] enum class QIcon::ThemeIcon

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

ConstantValueDescription
QIcon::ThemeIcon::AddressBookNew0The icon for the action to create a new address book.
QIcon::ThemeIcon::ApplicationExit1The icon for exiting an application.
QIcon::ThemeIcon::AppointmentNew2The icon for the action to create a new appointment.
QIcon::ThemeIcon::CallStart3The icon for initiating or accepting a call.
QIcon::ThemeIcon::CallStop4The icon for stopping a current call.
QIcon::ThemeIcon::ContactNew5The icon for the action to create a new contact.
QIcon::ThemeIcon::DocumentNew6The icon for the action to create a new document.
QIcon::ThemeIcon::DocumentOpen7The icon for the action to open a document.
QIcon::ThemeIcon::DocumentOpenRecent8The icon for the action to open a document that was recently opened.
QIcon::ThemeIcon::DocumentPageSetup9The icon for the page setup action.
QIcon::ThemeIcon::DocumentPrint10The icon for the print action.
QIcon::ThemeIcon::DocumentPrintPreview11The icon for the print preview action.
QIcon::ThemeIcon::DocumentProperties12The icon for the action to view the properties of a document.
QIcon::ThemeIcon::DocumentRevert13The icon for the action of reverting to a previous version of a document.
QIcon::ThemeIcon::DocumentSave14The icon for the save action.
QIcon::ThemeIcon::DocumentSaveAs15The icon for the save as action.
QIcon::ThemeIcon::DocumentSend16The icon for the send action.
QIcon::ThemeIcon::EditClear17The icon for the clear action.
QIcon::ThemeIcon::EditCopy18The icon for the copy action.
QIcon::ThemeIcon::EditCut19The icon for the cut action.
QIcon::ThemeIcon::EditDelete20The icon for the delete action.
QIcon::ThemeIcon::EditFind21The icon for the find action.
QIcon::ThemeIcon::EditPaste22The icon for the paste action.
QIcon::ThemeIcon::EditRedo23The icon for the redo action.
QIcon::ThemeIcon::EditSelectAll24The icon for the select all action.
QIcon::ThemeIcon::EditUndo25The icon for the undo action.
QIcon::ThemeIcon::FolderNew26The icon for creating a new folder.
QIcon::ThemeIcon::FormatIndentLess27The icon for the decrease indent formatting action.
QIcon::ThemeIcon::FormatIndentMore28The icon for the increase indent formatting action.
QIcon::ThemeIcon::FormatJustifyCenter29The icon for the center justification formatting action.
QIcon::ThemeIcon::FormatJustifyFill30The icon for the fill justification formatting action.
QIcon::ThemeIcon::FormatJustifyLeft31The icon for the left justification formatting action.
QIcon::ThemeIcon::FormatJustifyRight32The icon for the right justification action.
QIcon::ThemeIcon::FormatTextDirectionLtr33The icon for the left-to-right text formatting action.
QIcon::ThemeIcon::FormatTextDirectionRtl34The icon for the right-to-left formatting action.
QIcon::ThemeIcon::FormatTextBold35The icon for the bold text formatting action.
QIcon::ThemeIcon::FormatTextItalic36The icon for the italic text formatting action.
QIcon::ThemeIcon::FormatTextUnderline37The icon for the underlined text formatting action.
QIcon::ThemeIcon::FormatTextStrikethrough38The icon for the strikethrough text formatting action.
QIcon::ThemeIcon::GoDown39The icon for the go down in a list action.
QIcon::ThemeIcon::GoHome40The icon for the go to home location action.
QIcon::ThemeIcon::GoNext41The icon for the go to the next item in a list action.
QIcon::ThemeIcon::GoPrevious42The icon for the go to the previous item in a list action.
QIcon::ThemeIcon::GoUp43The icon for the go up in a list action.
QIcon::ThemeIcon::HelpAbout44The icon for the About item in the Help menu.
QIcon::ThemeIcon::HelpFaq45The icon for the FAQ item in the Help menu.
QIcon::ThemeIcon::InsertImage46The icon for the insert image action of an application.
QIcon::ThemeIcon::InsertLink47The icon for the insert link action of an application.
QIcon::ThemeIcon::InsertText48The icon for the insert text action of an application.
QIcon::ThemeIcon::ListAdd49The icon for the add to list action.
QIcon::ThemeIcon::ListRemove50The icon for the remove from list action.
QIcon::ThemeIcon::MailForward51The icon for the forward action.
QIcon::ThemeIcon::MailMarkImportant52The icon for the mark as important action.
QIcon::ThemeIcon::MailMarkRead53The icon for the mark as read action.
QIcon::ThemeIcon::MailMarkUnread54The icon for the mark as unread action.
QIcon::ThemeIcon::MailMessageNew55The icon for the compose new mail action.
QIcon::ThemeIcon::MailReplyAll56The icon for the reply to all action.
QIcon::ThemeIcon::MailReplySender57The icon for the reply to sender action.
QIcon::ThemeIcon::MailSend58The icon for the send action.
QIcon::ThemeIcon::MediaEject59The icon for the eject action of a media player or file manager.
QIcon::ThemeIcon::MediaPlaybackPause60The icon for the pause action of a media player.
QIcon::ThemeIcon::MediaPlaybackStart61The icon for the start playback action of a media player.
QIcon::ThemeIcon::MediaPlaybackStop62The icon for the stop action of a media player.
QIcon::ThemeIcon::MediaRecord63The icon for the record action of a media application.
QIcon::ThemeIcon::MediaSeekBackward64The icon for the seek backward action of a media player.
QIcon::ThemeIcon::MediaSeekForward65The icon for the seek forward action of a media player.
QIcon::ThemeIcon::MediaSkipBackward66The icon for the skip backward action of a media player.
QIcon::ThemeIcon::MediaSkipForward67The icon for the skip forward action of a media player.
QIcon::ThemeIcon::ObjectRotateLeft68The icon for the rotate left action performed on an object.
QIcon::ThemeIcon::ObjectRotateRight69The icon for the rotate right action performed on an object.
QIcon::ThemeIcon::ProcessStop70The icon for the stop action in applications with actions that may take a while to process, such as web page loading in a browser.
QIcon::ThemeIcon::SystemLockScreen71The icon for the lock screen action.
QIcon::ThemeIcon::SystemLogOut72The icon for the log out action.
QIcon::ThemeIcon::SystemSearch73The icon for the search action.
QIcon::ThemeIcon::SystemReboot74The icon for the reboot action.
QIcon::ThemeIcon::SystemShutdown75The icon for the shutdown action.
QIcon::ThemeIcon::ToolsCheckSpelling76The icon for the check spelling action.
QIcon::ThemeIcon::ViewFullscreen77The icon for the fullscreen action.
QIcon::ThemeIcon::ViewRefresh78The icon for the refresh action.
QIcon::ThemeIcon::ViewRestore79The icon for leaving the fullscreen view.
QIcon::ThemeIcon::WindowClose80The icon for the close window action.
QIcon::ThemeIcon::WindowNew81The icon for the new window action.
QIcon::ThemeIcon::ZoomFitBest82The icon for the best fit action.
QIcon::ThemeIcon::ZoomIn83The icon for the zoom in action.
QIcon::ThemeIcon::ZoomOut84The icon for the zoom out action.
QIcon::ThemeIcon::AudioCard85The icon for the audio rendering device.
QIcon::ThemeIcon::AudioInputMicrophone86The icon for the microphone audio input device.
QIcon::ThemeIcon::Battery87The icon for the system battery device.
QIcon::ThemeIcon::CameraPhoto88The icon for a digital still camera devices.
QIcon::ThemeIcon::CameraVideo89The icon for a video camera device.
QIcon::ThemeIcon::CameraWeb90The icon for a web camera device.
QIcon::ThemeIcon::Computer91The icon for the computing device as a whole.
QIcon::ThemeIcon::DriveHarddisk92The icon for hard disk drives.
QIcon::ThemeIcon::DriveOptical93The icon for optical media drives such as CD and DVD.
QIcon::ThemeIcon::InputGaming94The icon for the gaming input device.
QIcon::ThemeIcon::InputKeyboard95The icon for the keyboard input device.
QIcon::ThemeIcon::InputMouse96The icon for the mousing input device.
QIcon::ThemeIcon::InputTablet97The icon for graphics tablet input devices.
QIcon::ThemeIcon::MediaFlash98The icon for flash media, such as a memory stick.
QIcon::ThemeIcon::MediaOptical99The icon for physical optical media such as CD and DVD.
QIcon::ThemeIcon::MediaTape100The icon for generic physical tape media.
QIcon::ThemeIcon::MultimediaPlayer101The icon for generic multimedia playing devices.
QIcon::ThemeIcon::NetworkWired102The icon for wired network connections.
QIcon::ThemeIcon::NetworkWireless103The icon for wireless network connections.
QIcon::ThemeIcon::Phone104The icon for phone devices.
QIcon::ThemeIcon::Printer105The icon for a printer device.
QIcon::ThemeIcon::Scanner106The icon for a scanner device.
QIcon::ThemeIcon::VideoDisplay107The icon for the monitor that video gets displayed on.
QIcon::ThemeIcon::AppointmentMissed108The icon for when an appointment was missed.
QIcon::ThemeIcon::AppointmentSoon109The icon for when an appointment will occur soon.
QIcon::ThemeIcon::AudioVolumeHigh110The icon used to indicate high audio volume.
QIcon::ThemeIcon::AudioVolumeLow111The icon used to indicate low audio volume.
QIcon::ThemeIcon::AudioVolumeMedium112The icon used to indicate medium audio volume.
QIcon::ThemeIcon::AudioVolumeMuted113The icon used to indicate the muted state for audio playback.
QIcon::ThemeIcon::BatteryCaution114The icon used when the battery is below 40%.
QIcon::ThemeIcon::BatteryLow115The icon used when the battery is below 20%.
QIcon::ThemeIcon::DialogError116The icon used when a dialog is opened to explain an error condition to the user.
QIcon::ThemeIcon::DialogInformation117The icon used when a dialog is opened to give information to the user that may be pertinent to the requested action.
QIcon::ThemeIcon::DialogPassword118The icon used when a dialog requesting the authentication credentials for a user is opened.
QIcon::ThemeIcon::DialogQuestion119The icon used when a dialog is opened to ask a simple question to the user.
QIcon::ThemeIcon::DialogWarning120The icon used when a dialog is opened to warn the user of impending issues with the requested action.
QIcon::ThemeIcon::FolderDragAccept121The icon used for a folder while an acceptable object is being dragged onto it.
QIcon::ThemeIcon::FolderOpen122The icon used for folders, while their contents are being displayed within the same window.
QIcon::ThemeIcon::FolderVisiting123The icon used for folders, while their contents are being displayed in another window.
QIcon::ThemeIcon::ImageLoading124The icon used while another image is being loaded.
QIcon::ThemeIcon::ImageMissing125The icon used when another image could not be loaded.
QIcon::ThemeIcon::MailAttachment126The icon for a message that contains attachments.
QIcon::ThemeIcon::MailUnread127The icon for an unread message.
QIcon::ThemeIcon::MailRead128The icon for a read message.
QIcon::ThemeIcon::MailReplied129The icon for a message that has been replied to.
QIcon::ThemeIcon::MediaPlaylistRepeat130The icon for the repeat mode of a media player.
QIcon::ThemeIcon::MediaPlaylistShuffle131The icon for the shuffle mode of a media player.
QIcon::ThemeIcon::NetworkOffline132The icon used to indicate that the device is not connected to the network.
QIcon::ThemeIcon::PrinterPrinting133The icon used while a print job is successfully being spooled to a printing device.
QIcon::ThemeIcon::SecurityHigh134The icon used to indicate that the security level of an item is known to be high.
QIcon::ThemeIcon::SecurityLow135The icon used to indicate that the security level of an item is known to be low.
QIcon::ThemeIcon::SoftwareUpdateAvailable136The icon used to indicate that an update is available.
QIcon::ThemeIcon::SoftwareUpdateUrgent137The icon used to indicate that an urgent update is available.
QIcon::ThemeIcon::SyncError138The icon used when an error occurs while attempting to synchronize data across devices.
QIcon::ThemeIcon::SyncSynchronizing139The icon used while data is successfully synchronizing across devices.
QIcon::ThemeIcon::UserAvailable140The icon used to indicate that a user is available.
QIcon::ThemeIcon::UserOffline141The icon used to indicate that a user is not available.
QIcon::ThemeIcon::WeatherClear142The icon used to indicate that the sky is clear.
QIcon::ThemeIcon::WeatherClearNight143The icon used to indicate that the sky is clear during the night.
QIcon::ThemeIcon::WeatherFewClouds144The icon used to indicate that the sky is partly cloudy.
QIcon::ThemeIcon::WeatherFewCloudsNight145The icon used to indicate that the sky is partly cloudy during the night.
QIcon::ThemeIcon::WeatherFog146The icon used to indicate that the weather is foggy.
QIcon::ThemeIcon::WeatherShowers147The icon used to indicate that rain showers are occurring.
QIcon::ThemeIcon::WeatherSnow148The icon used to indicate that snow is falling.
QIcon::ThemeIcon::WeatherStorm149The icon used to indicate that the weather is stormy.

This enum was introduced in Qt 6.7.

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.

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

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

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.

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 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 with the given mode and state 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.

Note: Prior to Qt 6.8 this function wronlgy passed the device dependent pixmap size to QIconEngine::scaledPixmap(), since Qt 6.8 it's the device independent size (not scaled with the devicePixelRatio).

This function was introduced in Qt 6.0.

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.

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

If the theme matches the name of an installed font that provides named glyphs, then QIcon::fromTheme calls that match one of the glyphs will produce an icon for that glyph.

Otherwise, the theme 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 this icon with other. 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.

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

Move-assigns other to this QIcon instance.

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

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

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.

© 2024 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 the GNU 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.