![]() |
Qt 6.x
The Qt SDK
|
The QIcon class provides scalable icons in different modes and states. More...
#include <qicon.h>
Public Types | |
enum | Mode { Normal , Disabled , Active , Selected } |
This enum type describes the mode for which a pixmap is intended to be used. More... | |
enum | State { On , Off } |
This enum describes the state for which a pixmap is intended to be used. More... | |
typedef QIconPrivate * | DataPtr |
Public Member Functions | |
QIcon () noexcept | |
Constructs a null icon. | |
QIcon (const QPixmap &pixmap) | |
Constructs an icon from a pixmap. | |
QIcon (const QIcon &other) | |
Constructs a copy of other. | |
QIcon (QIcon &&other) noexcept | |
Move-constructs a QIcon instance, making it point to the same object that other was pointing to. | |
QIcon (const QString &fileName) | |
Constructs an icon from the file with the given fileName. | |
QIcon (QIconEngine *engine) | |
Creates an icon with a specific icon engine. | |
~QIcon () | |
Destroys the icon. | |
QIcon & | operator= (const QIcon &other) |
Assigns the other icon to this icon and returns a reference to this icon. | |
void | swap (QIcon &other) noexcept |
bool | operator== (const QIcon &) const =delete |
bool | operator!= (const QIcon &) const =delete |
operator QVariant () const | |
Returns the icon as a QVariant. | |
QPixmap | 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. | |
QPixmap | pixmap (int w, int h, 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.Returns a pixmap of size QSize(w, h). | |
QPixmap | pixmap (int extent, 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.Returns a pixmap of size QSize(extent, extent). | |
QPixmap | 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. | |
QSize | 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. | |
QString | name () const |
void | 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. | |
void | paint (QPainter *painter, int x, int y, int w, int h, Qt::Alignment alignment=Qt::AlignCenter, 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.Paints the icon into the rectangle QRect(x, y, w, h). | |
bool | isNull () const |
Returns true if the icon is empty; otherwise returns false . | |
bool | isDetached () const |
void | detach () |
qint64 | cacheKey () const |
Returns a number that identifies the contents of this QIcon object. | |
void | addPixmap (const QPixmap &pixmap, Mode mode=Normal, State state=Off) |
Adds pixmap to the icon, as a specialization for mode and state. | |
void | 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. | |
QList< QSize > | availableSizes (Mode mode=Normal, State state=Off) const |
void | setIsMask (bool isMask) |
bool | isMask () const |
DataPtr & | data_ptr () |
Static Public Member Functions | |
static QIcon | fromTheme (const QString &name) |
static QIcon | fromTheme (const QString &name, const QIcon &fallback) |
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. | |
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 QDataStream & | operator<< (QDataStream &, const QIcon &) |
Q_GUI_EXPORT QDataStream & | operator>> (QDataStream &, QIcon &) |
Related Symbols | |
(Note that these are not member symbols.) | |
QDataStream & | operator<< (QDataStream &stream, const QIcon &icon) |
QDataStream & | operator>> (QDataStream &stream, QIcon &icon) |
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.
enum QIcon::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 |
enum QIcon::State |
|
noexcept |
Constructs a null icon.
Definition at line 648 of file qicon.cpp.
Referenced by fromTheme().
QIcon::QIcon | ( | const QPixmap & | pixmap | ) |
Constructs an icon from a pixmap.
Definition at line 656 of file qicon.cpp.
References addPixmap(), and pixmap.
QIcon::QIcon | ( | const QIcon & | other | ) |
Constructs a copy of other.
This is very fast.
Definition at line 665 of file qicon.cpp.
References QBasicAtomicInteger< T >::ref(), and QIconPrivate::ref.
|
inlinenoexcept |
|
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 697 of file qicon.cpp.
References addFile(), and fileName.
|
explicit |
QIcon::~QIcon | ( | ) |
Destroys the icon.
Definition at line 716 of file qicon.cpp.
References QBasicAtomicInteger< T >::deref(), and QIconPrivate::ref.
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.)
Definition at line 880 of file qicon.cpp.
References actualSize(), QIconEngine::actualSize(), QIconPrivate::engine, QIconPrivate::pixmapDevicePixelRatio(), qApp, and state.
Referenced by actualSize(), QIconLoaderEngine::actualSize(), createHIcon(), QQC2::QWindowsXPStyle::drawControl(), QFontFamilyDelegate::paint(), QCommandLinkButton::paintEvent(), qt_aqua_get_known_size(), QItemDelegate::rect(), QWindowsSystemTrayIcon::showMessage(), QDockWidgetTitleButton::sizeHint(), QQC2_NAMESPACE::QMacStyle::subElementRect(), QQC2::QCommonStyle::subElementRect(), QMacStyle::subElementRect(), QCommonStyle::subElementRect(), QQC2::QMacStylePrivate::tabLayout(), and QQC2::QCommonStylePrivate::tabLayout().
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).
Definition at line 1067 of file qicon.cpp.
References QIconEngine::addFile(), detach(), QIconPrivate::engine, engine, fileName, iconEngineFromSuffix(), info, QString::isEmpty(), QMimeDatabase::mimeTypeForFile(), QMimeType::preferredSuffix, qApp, qt_findAtNxFile(), state, and QFileInfo::suffix().
Referenced by QIcon(), qt_setupActionIcon(), QQC2::QCommonStyle::standardIcon(), and QCommonStyle::standardIcon().
Adds pixmap to the icon, as a specialization for mode and state.
Custom icon engines are free to ignore additionally added pixmaps.
Definition at line 1014 of file qicon.cpp.
References QIconEngine::addPixmap(), detach(), QIconPrivate::engine, pixmap, and state.
Referenced by QIcon(), QAbstractFileIconProviderPrivate::getPlatformThemeIcon(), QCss::Declaration::iconValue(), QQC2::QCommonStyle::standardIcon(), QCommonStyle::standardIcon(), and QWindowsVistaStyle::standardIcon().
Returns a list of available icon sizes for the specified mode and state.
Definition at line 1098 of file qicon.cpp.
References QIconEngine::availableSizes(), QIconPrivate::engine, and state.
Referenced by QIconLoaderEngine::availableSizes(), availableXdgFileIconSizes(), fromTheme(), QXcbWindow::setWindowIcon(), QQC2::QCommonStyle::standardIcon(), QCommonStyle::standardIcon(), and QDBusTrayIcon::updateIcon().
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.
Definition at line 773 of file qicon.cpp.
References QIconPrivate::detach_no, and QIconPrivate::serialNum.
Referenced by QQC2::QWindowsXPStyle::drawControl(), QWindowsVistaStyle::drawControl(), QWindowsMenuItem::setIcon(), QMacStyle::subElementRect(), QCommonStyle::subElementRect(), and QWindowsSystemTrayIcon::updateIcon().
|
inline |
Definition at line 105 of file qicon.h.
References d.
Referenced by ScalableEntry::pixmap().
void QIcon::detach | ( | ) |
Definition at line 987 of file qicon.cpp.
References QIconEngine::clone(), QBasicAtomicInteger< T >::deref(), QIconPrivate::detach_no, QIconPrivate::engine, QIconEngine::isNull(), QBasicAtomicInteger< T >::loadRelaxed(), and QIconPrivate::ref.
Referenced by addFile(), addPixmap(), and setIsMask().
|
static |
Returns the fallback search paths for icons.
The fallback search paths are used to look for standalone icon files if the \l{themeName()}{current icon theme} or \l{fallbackIconTheme()}{fallback icon theme} do not provide results for an icon lookup.
The default value will depend on the platform.
Definition at line 1168 of file qicon.cpp.
References QIconLoader::fallbackSearchPaths(), and QIconLoader::instance().
Referenced by src_gui_image_qicon::wrapper2().
|
static |
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.
Definition at line 1236 of file qicon.cpp.
References QIconLoader::fallbackThemeName(), and QIconLoader::instance().
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:
Definition at line 1296 of file qicon.cpp.
References QIcon(), icon, and QDir::isAbsolutePath().
Referenced by availableXdgFileIconSizes(), QGuiApplicationPrivate::createPlatformIntegration(), fromTheme(), QAbstractFileIconProviderPrivate::getIconThemeIcon(), QAbstractFileIconProviderPrivate::getIconThemeIcon(), hasThemeIcon(), qt_setupActionIcon(), setActionIcon(), QQC2::QCommonStyle::standardIcon(), QCommonStyle::standardIcon(), QQC2::QCommonStyle::standardPixmap(), QCommonStyle::standardPixmap(), QQuickLabsPlatformIconLoader::toQIcon(), and src_gui_image_qicon::wrapper1().
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 1322 of file qicon.cpp.
References availableSizes(), fromTheme(), icon, QList< T >::isEmpty(), and isNull().
|
static |
Returns true
if there is an icon available for name in the current icon theme, otherwise returns false
.
Definition at line 1340 of file qicon.cpp.
References fromTheme(), icon, and name().
bool QIcon::isDetached | ( | ) | const |
Definition at line 980 of file qicon.cpp.
References QBasicAtomicInteger< T >::loadRelaxed(), and QIconPrivate::ref.
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).
Definition at line 1371 of file qicon.cpp.
References QIconPrivate::is_mask.
Referenced by 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.
Definition at line 973 of file qicon.cpp.
References QIconPrivate::engine, and QIconEngine::isNull().
Referenced by QDBusMenuItem::QDBusMenuItem(), QMenuBarPrivate::calcActionRects(), createHIcon(), QFileSystemModel::data(), QUndoModel::data(), QQC2_NAMESPACE::QMacStyle::drawControl(), QQC2::QCommonStyle::drawControl(), QMacStyle::drawControl(), QCommonStyle::drawControl(), QtAndroidMenu::fillMenuItem(), fromTheme(), QFileIconProviderPrivate::getIcon(), QFileIconProvider::icon(), iconToQXdgDBusImageVector(), QCss::Declaration::iconValue(), QHeaderView::initStyleOptionForIndex(), QComboBox::insertItem(), QQC2::QCommonStylePrivate::isViewItemCached(), QtWaylandClient::QWaylandBradientDecoration::paint(), ScalableEntry::pixmap(), qt_aqua_get_known_size(), QQC2_NAMESPACE::qt_aqua_get_known_size(), QHeaderView::sectionSizeFromContents(), setActionIcon(), QTabBar::setTabIcon(), QWindowPrivate::setVisible(), QWidget::setWindowIcon(), QCocoaWindow::setWindowIcon(), QXcbWindow::setWindowIcon(), QQC2::QCommonStyle::sizeFromContents(), QCheckBox::sizeHint(), QRadioButton::sizeHint(), QMessageBoxPrivate::standardIcon(), QQC2::QCommonStyle::standardIcon(), QCommonStyle::standardIcon(), QQC2::QCommonStyle::subElementRect(), QMacStyle::subElementRect(), QCommonStyle::subElementRect(), QQC2::QMacStylePrivate::tabLayout(), QQC2::QCommonStylePrivate::tabLayout(), QTabBar::tabSizeHint(), and QMenuPrivate::updateActionRects().
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().
Definition at line 1115 of file qicon.cpp.
References QIconPrivate::engine, and QIconEngine::iconName().
Referenced by QDBusMenuItem::QDBusMenuItem(), hasThemeIcon(), and QDBusTrayIcon::updateIcon().
QIcon::operator QVariant | ( | ) | const |
Returns the icon as a QVariant.
Definition at line 755 of file qicon.cpp.
References QVariant::fromValue().
|
delete |
Assigns the other icon to this icon and returns a reference to this icon.
Move-assigns other to this QIcon instance.
Definition at line 726 of file qicon.cpp.
References QBasicAtomicInteger< T >::deref(), other(), and QIconPrivate::ref.
|
delete |
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.
Definition at line 931 of file qicon.cpp.
References QIconEngine::actualSize(), Qt::AlignBottom, alignedRect(), Qt::AlignHCenter, alignment, Qt::AlignRight, Qt::AlignVCenter, QIconPrivate::engine, QPainter::layoutDirection(), QIconEngine::paint(), painter, rect, state, and QGuiApplicationPrivate::visualAlignment().
Referenced by QtWaylandClient::QWaylandBradientDecoration::paint(), and QFontFamilyDelegate::paint().
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.
Definition at line 788 of file qicon.cpp.
References dpr(), pixmap, and state.
Referenced by QDBusMenuItem::QDBusMenuItem(), createHIcon(), QQC2::QCommonStyle::drawComplexControl(), QCommonStyle::drawComplexControl(), QQC2::QWindowsXPStyle::drawComplexControl(), QQC2_NAMESPACE::QMacStyle::drawControl(), QQC2::QCommonStyle::drawControl(), QMacStyle::drawControl(), QCommonStyle::drawControl(), QQC2::QWindowsXPStyle::drawControl(), QWindowsVistaStyle::drawControl(), src_gui_image_qicon::MyWidget::drawIcon(), QtAndroidMenu::fillMenuItem(), if(), QWasmWindow::setWindowIcon(), QXcbWindow::setWindowIcon(), QWindowsSystemTrayIcon::showMessage(), QMessageBoxPrivate::standardIcon(), QQC2::QCommonStyle::standardIcon(), QCommonStyle::standardIcon(), QQC2_NAMESPACE::QMacStyle::standardPixmap(), QMacStyle::standardPixmap(), QQC2::QCommonStyle::standardPixmap(), QCommonStyle::standardPixmap(), QQC2::QWindowsXPStyle::standardPixmap(), QWindowsVistaStyle::standardPixmap(), QQC2::QCommonStyle::subElementRect(), and QCommonStyle::subElementRect().
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.
Returns a pixmap with the requested size, devicePixelRatio, mode, and state, generating one if necessary.
Definition at line 825 of file qicon.cpp.
References QIconPrivate::engine, QIconEngine::pixmap(), pixmap, QIconPrivate::pixmapDevicePixelRatio(), qApp, QIconEngine::scaledPixmap(), and state.
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.
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.
|
static |
Sets the fallback search paths for icons to paths.
The fallback search paths are used to look for standalone icon files if the \l{themeName()}{current icon theme} or \l{fallbackIconTheme()}{fallback icon theme} do not provide results for an icon lookup.
Definition at line 1189 of file qicon.cpp.
References QIconLoader::instance(), and QIconLoader::setFallbackSearchPaths().
Referenced by src_gui_image_qicon::wrapper2().
Sets the fallback icon theme to name.
The fallback icon theme is used for last resort lookup of icons not provided by the \l{themeName()}{current icon theme}, or if the \l{themeName()}{current icon theme} does not exist.
The name should correspond to a directory name in the themeSearchPath() containing an index.theme file describing its contents.
Definition at line 1259 of file qicon.cpp.
References QIconLoader::instance(), and QIconLoader::setFallbackThemeName().
void QIcon::setIsMask | ( | bool | isMask | ) |
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.
Definition at line 1205 of file qicon.cpp.
References QIconLoader::instance(), and QIconLoader::setThemeName().
|
static |
Sets the search paths for icon themes to paths.
Definition at line 1128 of file qicon.cpp.
References QIconLoader::instance(), and QIconLoader::setThemeSearchPath().
Swaps icon other with this icon. This operation is very fast and never fails.
Definition at line 36 of file qicon.h.
References d, other(), and qt_ptr_swap().
|
static |
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.
Definition at line 1221 of file qicon.cpp.
References QIconLoader::instance(), and QIconLoader::themeName().
Referenced by QQC2::QCommonStyle::standardIcon(), QCommonStyle::standardIcon(), QQC2::QCommonStyle::standardPixmap(), and QCommonStyle::standardPixmap().
|
static |
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.
Definition at line 1149 of file qicon.cpp.
References QIconLoader::instance(), and QIconLoader::themeSearchPaths().
Referenced by QIconTheme::QIconTheme().
|
friend |
|
related |
|
friend |
|
related |