QtBase  v6.3.1
Public Types | Public Member Functions | Static Public Member Functions | Friends | Related Functions | List of all members
QIcon Class Reference

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

#include <qicon.h>

Public Types

enum  Mode { Normal , Disabled , Active , Selected }
 
enum  State { On , Off }
 
typedef QIconPrivateDataPtr
 

Public Member Functions

 QIcon () noexcept
 
 QIcon (const QPixmap &pixmap)
 
 QIcon (const QIcon &other)
 
 QIcon (QIcon &&other) noexcept
 
 QIcon (const QString &fileName)
 
 QIcon (QIconEngine *engine)
 
 ~QIcon ()
 
QIconoperator= (const QIcon &other)
 
void swap (QIcon &other) noexcept
 
bool operator== (const QIcon &) const =delete
 
bool operator!= (const QIcon &) const =delete
 
 operator QVariant () 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
 
QPixmap pixmap (const QSize &size, qreal devicePixelRatio, Mode mode=Normal, State state=Off) const
 
QSize actualSize (const QSize &size, Mode mode=Normal, State state=Off) 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
 
bool isNull () const
 
bool isDetached () const
 
void detach ()
 
qint64 cacheKey () const
 
void addPixmap (const QPixmap &pixmap, Mode mode=Normal, State state=Off)
 
void addFile (const QString &fileName, const QSize &size=QSize(), Mode mode=Normal, State state=Off)
 
QList< QSizeavailableSizes (Mode mode=Normal, State state=Off) const
 
void setIsMask (bool isMask)
 
bool isMask () const
 
DataPtrdata_ptr ()
 

Static Public Member Functions

static QIcon fromTheme (const QString &name)
 
static QIcon fromTheme (const QString &name, const QIcon &fallback)
 
static bool hasThemeIcon (const QString &name)
 
static QStringList themeSearchPaths ()
 
static void setThemeSearchPaths (const QStringList &searchpath)
 
static QStringList fallbackSearchPaths ()
 
static void setFallbackSearchPaths (const QStringList &paths)
 
static QString themeName ()
 
static void setThemeName (const QString &path)
 
static QString fallbackThemeName ()
 
static void setFallbackThemeName (const QString &name)
 

Friends

Q_GUI_EXPORT QDataStreamoperator<< (QDataStream &, const QIcon &)
 
Q_GUI_EXPORT QDataStreamoperator>> (QDataStream &, QIcon &)
 

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &stream, const QIcon &icon)
 
QDataStreamoperator>> (QDataStream &stream, QIcon &icon)
 

Detailed Description

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

\inmodule QtGui

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:

To undo a QIcon, simply set a null icon in its place:

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

Note
Since Qt 4.2, an icon engine that supports SVG is included.

Definition at line 55 of file qicon.h.

Member Typedef Documentation

◆ DataPtr

Definition at line 142 of file qicon.h.

Member Enumeration Documentation

◆ Mode

This enum type describes the mode for which a pixmap is intended to be used. The currently defined modes are:

\value Normal Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available. \value Disabled Display the pixmap when the functionality represented by the icon is not available. \value Active 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. \value Selected Display the pixmap when the item represented by the icon is selected.

Enumerator
Normal 
Disabled 
Active 
Selected 

Definition at line 58 of file qicon.h.

◆ State

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

\value Off Display the pixmap when the widget is in an "off" state \value On Display the pixmap when the widget is in an "on" state

Enumerator
On 
Off 

Definition at line 59 of file qicon.h.

Constructor & Destructor Documentation

◆ QIcon() [1/6]

QIcon::QIcon ( )
noexcept

Constructs a null icon.

Definition at line 677 of file qicon.cpp.

Here is the caller graph for this function:

◆ QIcon() [2/6]

QIcon::QIcon ( const QPixmap pixmap)

Constructs an icon from a pixmap.

Definition at line 685 of file qicon.cpp.

Here is the call graph for this function:

◆ QIcon() [3/6]

QIcon::QIcon ( const QIcon other)

Constructs a copy of other. This is very fast.

Definition at line 694 of file qicon.cpp.

Here is the call graph for this function:

◆ QIcon() [4/6]

QIcon::QIcon ( QIcon &&  other)
inlinenoexcept

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

Definition at line 64 of file qicon.h.

◆ QIcon() [5/6]

QIcon::QIcon ( const QString fileName)
explicit

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 \l{resources.html}{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.

Definition at line 726 of file qicon.cpp.

Here is the call graph for this function:

◆ QIcon() [6/6]

QIcon::QIcon ( QIconEngine engine)
explicit

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

Definition at line 737 of file qicon.cpp.

◆ ~QIcon()

QIcon::~QIcon ( )

Destroys the icon.

Definition at line 745 of file qicon.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ actualSize()

QSize QIcon::actualSize ( const QSize size,
Mode  mode = Normal,
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(), paint()

Definition at line 909 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addFile()

void QIcon::addFile ( const QString fileName,
const QSize size = QSize(),
Mode  mode = Normal,
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 \l{resources.html}{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(), QPixmap::devicePixelRatio()

Definition at line 1096 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addPixmap()

void QIcon::addPixmap ( const QPixmap pixmap,
Mode  mode = Normal,
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()

Definition at line 1043 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ availableSizes()

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

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

Definition at line 1127 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cacheKey()

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.

Since
4.3

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

Definition at line 802 of file qicon.cpp.

Here is the caller graph for this function:

◆ data_ptr()

DataPtr & QIcon::data_ptr ( )
inline

Definition at line 143 of file qicon.h.

Here is the caller graph for this function:

◆ detach()

void QIcon::detach ( )

Definition at line 1016 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fallbackSearchPaths()

QStringList QIcon::fallbackSearchPaths ( )
static
Since
5.11

Returns the fallback search paths for icons.

The default value will depend on the platform.

See also
setFallbackSearchPaths(), themeSearchPaths()

Definition at line 1192 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fallbackThemeName()

QString QIcon::fallbackThemeName ( )
static
Since
5.12

Returns the name of the fallback icon theme.

On X11, if not set, the fallback icon theme depends on your desktop settings. On other platforms it is not set by default.

See also
setFallbackThemeName(), themeName()

Definition at line 1255 of file qicon.cpp.

Here is the call graph for this function:

◆ fromTheme() [1/2]

QIcon QIcon::fromTheme ( const QString name)
static
Since
4.6

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

The latest version of the freedesktop icon specification and naming specification can be obtained here:

\list

To fetch an icon from the current icon theme:

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 your themeSearchPaths() and set the appropriate themeName().
Qt will make use of GTK's icon-theme.cache if present to speed up the lookup. These caches can be generated using gtk-update-icon-cache: \l{https://developer.gnome.org/gtk3/stable/gtk-update-icon-cache.html}.
If an icon can't be found in the current theme, then it will be searched in fallbackSearchPaths() as an unthemed icon.
See also
themeName(), setThemeName(), themeSearchPaths(), fallbackSearchPaths()

Definition at line 1311 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromTheme() [2/2]

QIcon QIcon::fromTheme ( const QString name,
const QIcon fallback 
)
static

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns the QIcon corresponding to name in the current icon theme. If no such icon is found in the current theme fallback is returned instead.

If you want to provide a guaranteed fallback for platforms that do not support theme icons, you can use the second argument:

Definition at line 1344 of file qicon.cpp.

Here is the call graph for this function:

◆ hasThemeIcon()

bool QIcon::hasThemeIcon ( const QString name)
static
Since
4.6

Returns true if there is an icon available for name in the current icon theme, otherwise returns false.

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

Definition at line 1362 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isDetached()

bool QIcon::isDetached ( ) const

Definition at line 1009 of file qicon.cpp.

Here is the call graph for this function:

◆ isMask()

bool QIcon::isMask ( ) const
Since
5.6

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

Definition at line 1393 of file qicon.cpp.

Here is the caller graph for this function:

◆ isNull()

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.

Definition at line 1002 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ name()

QString QIcon::name ( ) const
Since
4.7

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(), QIconEngine::iconName()

Definition at line 1144 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator QVariant()

QIcon::operator QVariant ( ) const

Returns the icon as a QVariant.

Definition at line 784 of file qicon.cpp.

Here is the call graph for this function:

◆ operator!=()

bool QIcon::operator!= ( const QIcon ) const
delete

◆ operator=()

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

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

Move-assigns other to this QIcon instance.

Since
5.2

Definition at line 755 of file qicon.cpp.

Here is the call graph for this function:

◆ operator==()

bool QIcon::operator== ( const QIcon ) const
delete

◆ paint() [1/2]

void QIcon::paint ( QPainter painter,
const QRect rect,
Qt::Alignment  alignment = Qt::AlignCenter,
Mode  mode = Normal,
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(), pixmap()

Definition at line 960 of file qicon.cpp.

Here is the call graph for this function:

◆ paint() [2/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

Definition at line 99 of file qicon.h.

◆ pixmap() [1/4]

QPixmap QIcon::pixmap ( const QSize size,
Mode  mode = Normal,
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(), paint()

Definition at line 817 of file qicon.cpp.

Here is the caller graph for this function:

◆ pixmap() [2/4]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
6.0

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

See also
actualSize(), paint()

Definition at line 854 of file qicon.cpp.

Here is the call graph for this function:

◆ pixmap() [3/4]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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.

Definition at line 82 of file qicon.h.

◆ pixmap() [4/4]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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.

Definition at line 80 of file qicon.h.

◆ setFallbackSearchPaths()

void QIcon::setFallbackSearchPaths ( const QStringList paths)
static
Since
5.11

Sets the fallback search paths for icons to paths.

Note
To add some path without replacing existing ones:
See also
fallbackSearchPaths(), setThemeSearchPaths()

Definition at line 1208 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setFallbackThemeName()

void QIcon::setFallbackThemeName ( const QString name)
static
Since
5.12

Sets the fallback icon theme to name.

The name should correspond to a directory name in the themeSearchPath() containing an index.theme file describing its contents.

Note
This should be done before creating \l QGuiApplication, to ensure correct initialization.
See also
fallbackThemeName(), themeSearchPaths(), themeName()

Definition at line 1274 of file qicon.cpp.

Here is the call graph for this function:

◆ setIsMask()

void QIcon::setIsMask ( bool  isMask)
Since
5.6

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

Definition at line 1376 of file qicon.cpp.

Here is the call graph for this function:

◆ setThemeName()

void QIcon::setThemeName ( const QString name)
static
Since
4.6

Sets the current icon theme to name.

The name should correspond to a directory name in the themeSearchPath() containing an index.theme file describing its contents.

See also
themeSearchPaths(), themeName()

Definition at line 1224 of file qicon.cpp.

Here is the call graph for this function:

◆ setThemeSearchPaths()

void QIcon::setThemeSearchPaths ( const QStringList paths)
static
Since
4.6

Sets the search paths for icon themes to paths.

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

Definition at line 1157 of file qicon.cpp.

Here is the call graph for this function:

◆ swap()

void QIcon::swap ( QIcon other)
inlinenoexcept
Since
4.8

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

Definition at line 72 of file qicon.h.

Here is the call graph for this function:

◆ themeName()

QString QIcon::themeName ( )
static
Since
4.6

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.

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

Definition at line 1240 of file qicon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ themeSearchPaths()

QStringList QIcon::themeSearchPaths ( )
static
Since
4.6

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.

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

Definition at line 1178 of file qicon.cpp.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator<< [1/2]

QDataStream & operator<< ( QDataStream stream,
const QIcon icon 
)
friend
Since
4.2

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.

Definition at line 1415 of file qicon.cpp.

◆ operator<<() [2/2]

QDataStream & operator<< ( QDataStream stream,
const QIcon icon 
)
related
Since
4.2

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.

Definition at line 1415 of file qicon.cpp.

◆ operator>> [1/2]

QDataStream & operator>> ( QDataStream stream,
QIcon icon 
)
friend
Since
4.2

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

Definition at line 1454 of file qicon.cpp.

◆ operator>>() [2/2]

QDataStream & operator>> ( QDataStream stream,
QIcon icon 
)
related
Since
4.2

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

Definition at line 1454 of file qicon.cpp.


The documentation for this class was generated from the following files: