Qt 4.8
Public Types | Public Functions | Static Public Functions | Properties | Friends | List of all members
QIcon Class Reference

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

#include <qicon.h>

Public Types

typedef QIconPrivateDataPtr
 
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...
 

Public Functions

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. More...
 
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. More...
 
void addPixmap (const QPixmap &pixmap, Mode mode=Normal, State state=Off)
 Adds pixmap to the icon, as a specialization for mode and state. More...
 
QList< QSizeavailableSizes (Mode mode=Normal, State state=Off) const
 Returns a list of available icon sizes for the specified mode and state. More...
 
qint64 cacheKey () const
 Returns a number that identifies the contents of this QIcon object. More...
 
DataPtrdata_ptr ()
 
void detach ()
 
bool isDetached () const
 
bool isNull () const
 Returns true if the icon is empty; otherwise returns false. More...
 
QString name () const
 Returns the name used to create the icon, if available. More...
 
 operator QVariant () const
 Returns the icon as a QVariant. More...
 
QIconoperator= (const QIcon &other)
 Assigns the other icon to this icon and returns a reference to this icon. More...
 
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. More...
 
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). More...
 
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. More...
 
QPixmap pixmap (int w, int h, Mode mode=Normal, State state=Off) const
 Returns a pixmap of size QSize(w, h). More...
 
QPixmap pixmap (int extent, Mode mode=Normal, State state=Off) const
 Returns a pixmap of size QSize(extent, extent). More...
 
 QIcon ()
 Constructs a null icon. More...
 
 QIcon (const QPixmap &pixmap)
 Constructs an icon from a pixmap. More...
 
 QIcon (const QIcon &other)
 Constructs a copy of other. More...
 
 QIcon (const QString &fileName)
 Constructs an icon from the file with the given fileName. More...
 
 QIcon (QIconEngine *engine)
 Creates an icon with a specific icon engine. More...
 
 QIcon (QIconEngineV2 *engine)
 Creates an icon with a specific icon engine. More...
 
int serialNumber () const
 Returns a number that identifies the contents of this QIcon object. More...
 
void swap (QIcon &other)
 Swaps icon other with this icon. More...
 
 ~QIcon ()
 Destroys the icon. More...
 

Static Public Functions

static QIcon fromTheme (const QString &name, const QIcon &fallback=QIcon())
 Returns the QIcon corresponding to name in the current icon theme. More...
 
static bool hasThemeIcon (const QString &name)
 Returns true if there is an icon available for name in the current icon theme, otherwise returns false. More...
 
static void setThemeName (const QString &path)
 Sets the current icon theme to name. More...
 
static void setThemeSearchPaths (const QStringList &searchpath)
 Sets the search paths for icon themes to paths. More...
 
static QString themeName ()
 Returns the name of the current icon theme. More...
 
static QStringList themeSearchPaths ()
 Returns the search paths for icon themes. More...
 

Properties

QIconPrivated
 

Friends

Q_GUI_EXPORT QDataStreamoperator<< (QDataStream &, const QIcon &)
 Writes the given icon to the given stream as a PNG image. More...
 
Q_GUI_EXPORT QDataStreamoperator>> (QDataStream &, QIcon &)
 Reads an image, or a set of images, from the given stream into the given icon. More...
 

Detailed Description

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

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:

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

To undo a QIcon, 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.

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 QIconEngineV2. 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 QIconEnginePluginV2 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.

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 when you draw the icon, choose whichever pixmap is appropriate for the current state of your widget. For example:

void MyWidget::drawIcon(QPainter *painter, QPoint pos)
{
QPixmap pixmap = icon.pixmap(QSize(22, 22),
isEnabled() ? QIcon::Normal
isChecked() ? QIcon::On
painter->drawPixmap(pos, pixmap);
}

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.

icon.png
QIcon
See also
{fowler}{GUI Design Handbook: Iconic Label}, {Icons Example}

Definition at line 60 of file qicon.h.

Typedefs

◆ DataPtr

Warning
This function is not part of the public interface.

Definition at line 142 of file qicon.h.

Enumerations

◆ Mode

This enum type describes the mode for which a pixmap is intended to be used.

The currently defined modes are:

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

Definition at line 63 of file qicon.h.

◆ State

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

The state can be:

  • Off Display the pixmap when the widget is in an "off" state
  • On Display the pixmap when the widget is in an "on" state
Enumerator
On 
Off 

Definition at line 64 of file qicon.h.

64 { On, Off };

Constructors and Destructors

◆ QIcon() [1/6]

QIcon::QIcon ( )

Constructs a null icon.

Definition at line 528 of file qicon.cpp.

Referenced by fromTheme(), and operator>>().

529  : d(0)
530 {
531 }
QIconPrivate * d
Definition: qicon.h:135

◆ QIcon() [2/6]

QIcon::QIcon ( const QPixmap pixmap)

Constructs an icon from a pixmap.

Definition at line 536 of file qicon.cpp.

537  :d(0)
538 {
539  addPixmap(pixmap);
540 }
void addPixmap(const QPixmap &pixmap, Mode mode=Normal, State state=Off)
Adds pixmap to the icon, as a specialization for mode and state.
Definition: qicon.cpp:814
QIconPrivate * d
Definition: qicon.h:135

◆ QIcon() [3/6]

QIcon::QIcon ( const QIcon other)

Constructs a copy of other.

This is very fast.

Definition at line 545 of file qicon.cpp.

546  :d(other.d)
547 {
548  if (d)
549  d->ref.ref();
550 }
QIconPrivate * d
Definition: qicon.h:135
QAtomicInt ref
Definition: qicon_p.h:84
bool ref()
Atomically increments the value of this QAtomicInt.

◆ QIcon() [4/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 be either 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.

Definition at line 569 of file qicon.cpp.

570  : d(0)
571 {
572  addFile(fileName);
573 }
QIconPrivate * d
Definition: qicon.h:135
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...
Definition: qicon.cpp:851

◆ QIcon() [5/6]

QIcon::QIcon ( QIconEngine engine)
explicit

Creates an icon with a specific icon engine.

The icon takes ownership of the engine.

Definition at line 580 of file qicon.cpp.

581  :d(new QIconPrivate)
582 {
583  d->engine_version = 1;
584  d->engine = engine;
585  d->v1RefCount = new QAtomicInt(1);
586 }
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
QIconPrivate * d
Definition: qicon.h:135
QAtomicInt * v1RefCount
Definition: qicon_p.h:89
int engine_version
Definition: qicon_p.h:87
QIconEngine * engine
Definition: qicon_p.h:82

◆ QIcon() [6/6]

QIcon::QIcon ( QIconEngineV2 engine)
explicit

Creates an icon with a specific icon engine.

The icon takes ownership of the engine.

Definition at line 592 of file qicon.cpp.

593  :d(new QIconPrivate)
594 {
595  d->engine_version = 2;
596  d->engine = engine;
597 }
QIconPrivate * d
Definition: qicon.h:135
int engine_version
Definition: qicon_p.h:87
QIconEngine * engine
Definition: qicon_p.h:82

◆ ~QIcon()

QIcon::~QIcon ( )

Destroys the icon.

Definition at line 602 of file qicon.cpp.

603 {
604  if (d && !d->ref.deref())
605  delete d;
606 }
QIconPrivate * d
Definition: qicon.h:135
QAtomicInt ref
Definition: qicon_p.h:84
bool deref()
Atomically decrements the value of this QAtomicInt.

Functions

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

See also
pixmap(), paint()

Definition at line 730 of file qicon.cpp.

Referenced by QMessageBox::about(), QSystemTrayIconSys::createIcon(), QWindowsVistaStyle::drawControl(), QWindowsXPStyle::drawControl(), QCommandLinkButton::heightForWidth(), QWorkspaceTitleBar::initStyleOption(), QCommandLinkButton::minimumSizeHint(), QFontFamilyDelegate::paint(), QCommandLinkButton::paintEvent(), qt_aqua_get_known_size(), qt_createIcon(), QItemDelegate::rect(), QUrlModel::setUrl(), QApplication::setWindowIcon(), QDockWidgetTitleButton::sizeHint(), QCommonStyle::subElementRect(), QMacStyle::subElementRect(), and QCommonStylePrivate::tabLayout().

731 {
732  if (!d)
733  return QSize();
734  return d->engine->actualSize(size, mode, state);
735 }
virtual QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
Returns the actual size of the icon the engine provides for the requested size, mode and state...
Definition: qiconengine.cpp:87
QIconPrivate * d
Definition: qicon.h:135
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QIconEngine * engine
Definition: qicon_p.h:82

◆ 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 be either 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.

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

Definition at line 851 of file qicon.cpp.

Referenced by operator>>(), QIcon(), qt_setupActionIcon(), and QCommonStyle::standardIconImplementation().

852 {
853  if (fileName.isEmpty())
854  return;
855  if (!d) {
856 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
857  QFileInfo info(fileName);
858  QString suffix = info.suffix();
859  if (!suffix.isEmpty()) {
860  // first try version 2 engines..
861  if (QIconEngineFactoryInterfaceV2 *factory = qobject_cast<QIconEngineFactoryInterfaceV2*>(loaderV2()->instance(suffix))) {
862  if (QIconEngine *engine = factory->create(fileName)) {
863  d = new QIconPrivate;
864  d->engine = engine;
865  }
866  }
867  // ..then fall back and try to load version 1 engines
868  if (!d) {
869  if (QIconEngineFactoryInterface *factory = qobject_cast<QIconEngineFactoryInterface*>(loader()->instance(suffix))) {
870  if (QIconEngine *engine = factory->create(fileName)) {
871  d = new QIconPrivate;
872  d->engine = engine;
873  d->engine_version = 1;
874  d->v1RefCount = new QAtomicInt(1);
875  }
876  }
877  }
878  }
879 #endif
880  // ...then fall back to the default engine
881  if (!d) {
882  d = new QIconPrivate;
883  d->engine = new QPixmapIconEngine;
884  }
885  } else {
886  detach();
887  }
888  d->engine->addFile(fileName, size, mode, state);
889 }
static mach_timebase_info_data_t info
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
QIconPrivate * d
Definition: qicon.h:135
QAtomicInt * v1RefCount
Definition: qicon_p.h:89
The QString class provides a Unicode character string.
Definition: qstring.h:83
int engine_version
Definition: qicon_p.h:87
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
void detach()
Definition: qicon.cpp:783
QByteArray suffix
The QIconEngine class provides an abstract base class for QIcon renderers.
Definition: qiconengine.h:55
loaderV2
Definition: qicon.cpp:451
QIconEngine * engine
Definition: qicon_p.h:82
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
virtual void addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state)
Called by QIcon::addFile().

◆ 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 814 of file qicon.cpp.

Referenced by QScriptDebugger::continueAction(), QScriptDebugger::findInScriptAction(), QFileIconProviderPrivate::getWinIcon(), QCss::Declaration::iconValue(), QScriptDebugger::interruptAction(), operator>>(), QIcon(), QScriptBreakpointsWidget::QScriptBreakpointsWidget(), qt_mac_constructQIconFromIconRef(), QScriptDebugger::runToCursorAction(), QScriptDebugger::runToNewScriptAction(), QUrlModel::setUrl(), QWindowsXPStyle::standardIconImplementation(), QWindowsStyle::standardIconImplementation(), QCommonStyle::standardIconImplementation(), QScriptDebugger::stepIntoAction(), QScriptDebugger::stepOutAction(), and QScriptDebugger::stepOverAction().

815 {
816  if (pixmap.isNull())
817  return;
818  if (!d) {
819  d = new QIconPrivate;
820  d->engine = new QPixmapIconEngine;
821  } else {
822  detach();
823  }
824  d->engine->addPixmap(pixmap, mode, state);
825 }
QIconPrivate * d
Definition: qicon.h:135
virtual void addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state)
Called by QIcon::addPixmap().
void detach()
Definition: qicon.cpp:783
QIconEngine * engine
Definition: qicon_p.h:82
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615

◆ availableSizes()

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

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

Since
4.5

Definition at line 900 of file qicon.cpp.

Referenced by fromTheme(), qstring_to_xtp(), and QCommonStyle::standardIconImplementation().

901 {
902  if (!d || !d->engine || d->engine_version < 2)
903  return QList<QSize>();
904  QIconEngineV2 *engine = static_cast<QIconEngineV2*>(d->engine);
905  return engine->availableSizes(mode, state);
906 }
QIconPrivate * d
Definition: qicon.h:135
int engine_version
Definition: qicon_p.h:87
The QIconEngineV2 class provides an abstract base class for QIcon renderers.
Definition: qiconengine.h:73
QIconEngine * engine
Definition: qicon_p.h:82

◆ 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 679 of file qicon.cpp.

Referenced by QWindowsVistaStyle::drawControl(), QWindowsXPStyle::drawControl(), QUrlModel::setUrl(), QCommonStyle::subElementRect(), and QMacStyle::subElementRect().

680 {
681  if (!d)
682  return 0;
683  return (((qint64) d->serialNum) << 32) | ((qint64) (d->detach_no));
684 }
int serialNum
Definition: qicon_p.h:85
QIconPrivate * d
Definition: qicon.h:135
__int64 qint64
Definition: qglobal.h:942
int detach_no
Definition: qicon_p.h:86

◆ data_ptr()

DataPtr & QIcon::data_ptr ( )
inline
Warning
This function is not part of the public interface.

Definition at line 143 of file qicon.h.

143 { return d; }
QIconPrivate * d
Definition: qicon.h:135

◆ detach()

void QIcon::detach ( )
Warning
This function is not part of the public interface.

Definition at line 783 of file qicon.cpp.

Referenced by addFile(), and addPixmap().

784 {
785  if (d) {
786  if (d->ref != 1) {
787  QIconPrivate *x = new QIconPrivate;
788  if (d->engine_version > 1) {
789  QIconEngineV2 *engine = static_cast<QIconEngineV2 *>(d->engine);
790  x->engine = engine->clone();
791  } else {
792  x->engine = d->engine;
793  x->v1RefCount = d->v1RefCount;
794  x->v1RefCount->ref();
795  }
797  if (!d->ref.deref())
798  delete d;
799  d = x;
800  }
801  ++d->detach_no;
802  }
803 }
QIconPrivate * d
Definition: qicon.h:135
QAtomicInt * v1RefCount
Definition: qicon_p.h:89
QAtomicInt ref
Definition: qicon_p.h:84
bool ref()
Atomically increments the value of this QAtomicInt.
int engine_version
Definition: qicon_p.h:87
bool deref()
Atomically decrements the value of this QAtomicInt.
virtual QIconEngineV2 * clone() const
Returns a clone of this icon engine.
The QIconEngineV2 class provides an abstract base class for QIcon renderers.
Definition: qiconengine.h:73
int detach_no
Definition: qicon_p.h:86
QIconEngine * engine
Definition: qicon_p.h:82

◆ fromTheme()

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

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

Since
4.6

If no such icon is found in the current theme fallback is returned instead.

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

To fetch an icon from the current icon theme:

QIcon undoicon = QIcon::fromTheme("edit-undo");

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

QIcon undoicon = QIcon::fromTheme("edit-undo", QIcon(":/undo.png"));
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().
See also
themeName(), setThemeName(), themeSearchPaths()

Definition at line 1039 of file qicon.cpp.

Referenced by QGtkStylePrivate::getFilesystemIcon(), hasThemeIcon(), QCommonStyle::standardIconImplementation(), and QCommonStyle::standardPixmap().

1040 {
1041  QIcon icon;
1042 
1043  if (qtIconCache()->contains(name)) {
1044  icon = *qtIconCache()->object(name);
1045  } else {
1046  QIcon *cachedIcon = new QIcon(new QIconLoaderEngine(name));
1047  qtIconCache()->insert(name, cachedIcon);
1048  icon = *cachedIcon;
1049  }
1050 
1051  // Note the qapp check is to allow lazy loading of static icons
1052  // Supporting fallbacks will not work for this case.
1053  if (qApp && icon.availableSizes().isEmpty())
1054  return fallback;
1055 
1056  return icon;
1057 }
QList< QSize > availableSizes(Mode mode=Normal, State state=Off) const
Returns a list of available icon sizes for the specified mode and state.
Definition: qicon.cpp:900
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
QIcon()
Constructs a null icon.
Definition: qicon.cpp:528
#define qApp
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60

◆ hasThemeIcon()

bool QIcon::hasThemeIcon ( const QString name)
static

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

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

Definition at line 1070 of file qicon.cpp.

1071 {
1072  QIcon icon = fromTheme(name);
1073 
1074  return !icon.isNull();
1075 }
static QIcon fromTheme(const QString &name, const QIcon &fallback=QIcon())
Returns the QIcon corresponding to name in the current icon theme.
Definition: qicon.cpp:1039
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition: qicon.cpp:769
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60

◆ isDetached()

bool QIcon::isDetached ( ) const
Warning
This function is not part of the public interface.

Definition at line 776 of file qicon.cpp.

777 {
778  return !d || d->ref == 1;
779 }
QIconPrivate * d
Definition: qicon.h:135
QAtomicInt ref
Definition: qicon_p.h:84

◆ 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 769 of file qicon.cpp.

Referenced by QMenuBarPrivate::calcActionRects(), QSystemTrayIconSys::createIcon(), QUndoModel::data(), QFileSystemModel::data(), QCommonStyle::drawControl(), QStyleSheetStyle::drawControl(), QWindowsVistaStyle::drawPrimitive(), QVistaHelper::drawTitleBar(), QDirModel::fileIcon(), QSystemTrayIconSys::findTrayGeometry(), QFileIconProviderPrivate::getIcon(), hasThemeIcon(), QFileIconProvider::icon(), QCss::Declaration::iconValue(), QComboBox::insertItem(), QCommonStylePrivate::isViewItemCached(), operator<<(), QHeaderView::paintSection(), QDecorationDefault::pixmapFor(), QMacStylePrivate::pushButtonSizeFromContents(), QBalloonTip::QBalloonTip(), qstring_to_xtp(), qt_aqua_get_known_size(), qt_createIcon(), QWorkspaceChild::QWorkspaceChild(), QHeaderView::sectionSizeFromContents(), QTabBar::setTabIcon(), QWidget::setWindowIcon(), QWidgetPrivate::setWindowIcon_sys(), QRadioButton::sizeHint(), QCheckBox::sizeHint(), QMessageBoxPrivate::standardIcon(), QWindowsStyle::standardIconImplementation(), QWindowsXPStyle::standardIconImplementation(), QCommonStyle::standardIconImplementation(), QCommonStyle::subElementRect(), QMacStyle::subElementRect(), QMenuPrivate::QMacMenuPrivate::syncAction(), QCommonStylePrivate::tabLayout(), QTabBar::tabSizeHint(), QVistaHelper::titleOffset(), QMenuPrivate::updateActionRects(), and QMdi::ControlLabel::updateWindowIcon().

770 {
771  return !d;
772 }
QIconPrivate * d
Definition: qicon.h:135

◆ name()

QString QIcon::name ( ) const

Returns the name used to create the icon, if available.

Since
4.7

Depending on the way the icon was created, it may have an associated name. This is the case for icons created with fromTheme() or icons using a QIconEngine which supports the QIconEngineV2::IconNameHook.

See also
fromTheme(), QIconEngine

Definition at line 922 of file qicon.cpp.

923 {
924  if (!d || !d->engine || d->engine_version < 2)
925  return QString();
926  QIconEngineV2 *engine = static_cast<QIconEngineV2*>(d->engine);
927  return engine->iconName();
928 }
QString iconName()
Returns the name used to create the engine, if available.
QIconPrivate * d
Definition: qicon.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
int engine_version
Definition: qicon_p.h:87
The QIconEngineV2 class provides an abstract base class for QIcon renderers.
Definition: qiconengine.h:73
QIconEngine * engine
Definition: qicon_p.h:82

◆ operator QVariant()

QIcon::operator QVariant ( ) const

Returns the icon as a QVariant.

Definition at line 636 of file qicon.cpp.

637 {
638  return QVariant(QVariant::Icon, this);
639 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92

◆ operator=()

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

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

Definition at line 612 of file qicon.cpp.

613 {
614  if (other.d)
615  other.d->ref.ref();
616  if (d && !d->ref.deref())
617  delete d;
618  d = other.d;
619  return *this;
620 }
QIconPrivate * d
Definition: qicon.h:135
QAtomicInt ref
Definition: qicon_p.h:84
bool ref()
Atomically increments the value of this QAtomicInt.
bool deref()
Atomically decrements the value of this QAtomicInt.

◆ 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 744 of file qicon.cpp.

Referenced by QStyleSheetStyle::drawComplexControl(), QRenderRule::drawImage(), QScriptEdit::extraAreaPaintEvent(), QFontFamilyDelegate::paint(), and QSystemTrayIconSys::paintEvent().

745 {
746  if (!d || !painter)
747  return;
748  QRect alignedRect = QStyle::alignedRect(painter->layoutDirection(), alignment, d->engine->actualSize(rect.size(), mode, state), rect);
749  d->engine->paint(painter, alignedRect, mode, state);
750 }
static QRect alignedRect(Qt::LayoutDirection direction, Qt::Alignment alignment, const QSize &size, const QRect &rectangle)
Returns a new rectangle of the specified size that is aligned to the given rectangle according to the...
Definition: qstyle.cpp:2120
virtual QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
Returns the actual size of the icon the engine provides for the requested size, mode and state...
Definition: qiconengine.cpp:87
QIconPrivate * d
Definition: qicon.h:135
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
Qt::LayoutDirection layoutDirection() const
Returns the layout direction used by the painter when drawing text.
Definition: qpainter.cpp:8729
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
QIconEngine * engine
Definition: qicon_p.h:82
virtual void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)=0
Uses the given painter to paint the icon with the required mode and state into the rectangle rect...

◆ 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 93 of file qicon.h.

94  { paint(painter, QRect(x, y, w, h), alignment, mode, state); }
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
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 rectan...
Definition: qicon.cpp:744

◆ pixmap() [1/3]

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.

See also
actualSize(), paint()

Definition at line 693 of file qicon.cpp.

Referenced by QMessageBox::about(), QSystemTrayIconSys::createIcon(), QCommonStyle::drawComplexControl(), QCleanlooksStyle::drawComplexControl(), QWindowsXPStyle::drawComplexControl(), QStyleSheetStyle::drawComplexControl(), QCommonStyle::drawControl(), QWindowsXPStyle::drawControl(), QStyleSheetStyle::drawControl(), QVistaHelper::drawTitleBar(), QWorkspaceChild::eventFilter(), QSystemTrayIconSys::findTrayGeometry(), QFileIconProviderPrivate::getWinIcon(), QWorkspaceTitleBar::initStyleOption(), operator<<(), operator>>(), QCommandLinkButton::paintEvent(), ScalableEntry::pixmap(), QDecorationDefault::pixmapFor(), QBalloonTip::QBalloonTip(), qstring_to_xtp(), qt_createIcon(), QUrlModel::setUrl(), QWidgetPrivate::setWindowIcon_sys(), QWorkspacePrivate::showMaximizeControls(), QMessageBoxPrivate::standardIcon(), QCommonStyle::standardIconImplementation(), QCommonStyle::standardPixmap(), QWindowsXPStyle::standardPixmap(), QStyleSheetStyle::standardPixmap(), QMacStyle::standardPixmap(), QMenuPrivate::QMacMenuPrivate::syncAction(), and QMdi::ControlLabel::updateWindowIcon().

694 {
695  if (!d)
696  return QPixmap();
697  return d->engine->pixmap(size, mode, state);
698 }
QIconPrivate * d
Definition: qicon.h:135
virtual QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
Returns the icon as a pixmap with the required size, mode, and state.
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
QIconEngine * engine
Definition: qicon_p.h:82

◆ pixmap() [2/3]

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

Returns a pixmap of size QSize(w, h).

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

The pixmap might be smaller than requested, but never larger.

Definition at line 83 of file qicon.h.

84  { return pixmap(QSize(w, h), mode, state); }
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.
Definition: qicon.cpp:693
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53

◆ pixmap() [3/3]

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

Returns a pixmap of size QSize(extent, extent).

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

The pixmap might be smaller than requested, but never larger.

Definition at line 85 of file qicon.h.

86  { return pixmap(QSize(extent, extent), mode, state); }
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.
Definition: qicon.cpp:693
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53

◆ serialNumber()

int QIcon::serialNumber ( ) const

Returns a number that identifies the contents of this QIcon object.

Distinct QIcon objects can have the same serial number if they refer to the same contents (but they don't have to). Also, the serial number of a QIcon object may change during its lifetime.

Use cacheKey() instead.

A null icon always has a serial number of 0.

Serial numbers are mostly useful in conjunction with caching.

See also
QPixmap::serialNumber()

Definition at line 661 of file qicon.cpp.

662 {
663  return d ? d->serialNum : 0;
664 }
int serialNum
Definition: qicon_p.h:85
QIconPrivate * d
Definition: qicon.h:135

◆ setThemeName()

void QIcon::setThemeName ( const QString name)
static

Sets the current icon theme to name.

Since
4.6

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

See also
themeSearchPaths(), themeName()

Definition at line 982 of file qicon.cpp.

983 {
985 }
void setThemeName(const QString &themeName)
static QIconLoader * instance()

◆ setThemeSearchPaths()

void QIcon::setThemeSearchPaths ( const QStringList paths)
static

Sets the search paths for icon themes to paths.

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

Definition at line 939 of file qicon.cpp.

940 {
942 }
void setThemeSearchPath(const QStringList &searchPaths)
static QIconLoader * instance()

◆ swap()

void QIcon::swap ( QIcon other)
inline

Swaps icon other with this icon.

Since
4.8

This operation is very fast and never fails.

Definition at line 78 of file qicon.h.

78 { qSwap(d, other.d); }
QIconPrivate * d
Definition: qicon.h:135
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181

◆ themeName()

QString QIcon::themeName ( )
static

Returns the name of the current icon theme.

Since
4.6

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 1001 of file qicon.cpp.

Referenced by QCommonStyle::standardIconImplementation(), and QCommonStyle::standardPixmap().

1002 {
1003  return QIconLoader::instance()->themeName();
1004 }
static QIconLoader * instance()
QString themeName() const

◆ themeSearchPaths()

QStringList QIcon::themeSearchPaths ( )
static

Returns the search paths for icon themes.

Since
4.6

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 : 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 963 of file qicon.cpp.

Referenced by QIconTheme::QIconTheme().

964 {
966 }
QStringList themeSearchPaths() const
static QIconLoader * instance()

Friends and Related Functions

◆ operator<<

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

Writes the given icon to the given stream as a PNG image.

Since
4.2

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 1096 of file qicon.cpp.

1097 {
1098  if (s.version() >= QDataStream::Qt_4_3) {
1099  if (icon.isNull()) {
1100  s << QString();
1101  } else {
1102  if (icon.d->engine_version > 1) {
1103  QIconEngineV2 *engine = static_cast<QIconEngineV2 *>(icon.d->engine);
1104  s << engine->key();
1105  engine->write(s);
1106  } else {
1107  // not really supported
1108  qWarning("QIcon: Cannot stream QIconEngine. Use QIconEngineV2 instead.");
1109  }
1110  }
1111  } else if (s.version() == QDataStream::Qt_4_2) {
1112  if (icon.isNull()) {
1113  s << 0;
1114  } else {
1115  QPixmapIconEngine *engine = static_cast<QPixmapIconEngine *>(icon.d->engine);
1116  int num_entries = engine->pixmaps.size();
1117  s << num_entries;
1118  for (int i=0; i < num_entries; ++i) {
1119  s << engine->pixmaps.at(i).pixmap;
1120  s << engine->pixmaps.at(i).fileName;
1121  s << engine->pixmaps.at(i).size;
1122  s << (uint) engine->pixmaps.at(i).mode;
1123  s << (uint) engine->pixmaps.at(i).state;
1124  }
1125  }
1126  } else {
1127  s << QPixmap(icon.pixmap(22,22));
1128  }
1129  return s;
1130 }
virtual QString key() const
Returns a key that identifies this icon engine.
QVector< QPixmapIconEngineEntry > pixmaps
Definition: qicon_p.h:131
QIconPrivate * d
Definition: qicon.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
int engine_version
Definition: qicon_p.h:87
QIcon::State state
Definition: qicon_p.h:104
QIcon::Mode mode
Definition: qicon_p.h:103
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition: qicon.cpp:769
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
The QIconEngineV2 class provides an abstract base class for QIcon renderers.
Definition: qiconengine.h:73
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
virtual bool write(QDataStream &out) const
Writes the contents of this engine to the QDataStream out.
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
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.
Definition: qicon.cpp:693
QIconEngine * engine
Definition: qicon_p.h:82
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ operator>>

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

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

Since
4.2

Definition at line 1144 of file qicon.cpp.

1145 {
1146  if (s.version() >= QDataStream::Qt_4_3) {
1147  icon = QIcon();
1148  QString key;
1149  s >> key;
1150  if (key == QLatin1String("QPixmapIconEngine")) {
1151  icon.d = new QIconPrivate;
1152  QIconEngineV2 *engine = new QPixmapIconEngine;
1153  icon.d->engine = engine;
1154  engine->read(s);
1155  } else if (key == QLatin1String("QIconLoaderEngine")) {
1156  icon.d = new QIconPrivate;
1157  QIconEngineV2 *engine = new QIconLoaderEngine();
1158  icon.d->engine = engine;
1159  engine->read(s);
1160 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
1161  } else if (QIconEngineFactoryInterfaceV2 *factory = qobject_cast<QIconEngineFactoryInterfaceV2*>(loaderV2()->instance(key))) {
1162  if (QIconEngineV2 *engine= factory->create()) {
1163  icon.d = new QIconPrivate;
1164  icon.d->engine = engine;
1165  engine->read(s);
1166  }
1167 #endif
1168  }
1169  } else if (s.version() == QDataStream::Qt_4_2) {
1170  icon = QIcon();
1171  int num_entries;
1172  QPixmap pm;
1173  QString fileName;
1174  QSize sz;
1175  uint mode;
1176  uint state;
1177 
1178  s >> num_entries;
1179  for (int i=0; i < num_entries; ++i) {
1180  s >> pm;
1181  s >> fileName;
1182  s >> sz;
1183  s >> mode;
1184  s >> state;
1185  if (pm.isNull())
1186  icon.addFile(fileName, sz, QIcon::Mode(mode), QIcon::State(state));
1187  else
1188  icon.addPixmap(pm, QIcon::Mode(mode), QIcon::State(state));
1189  }
1190  } else {
1191  QPixmap pm;
1192  s >> pm;
1193  icon.addPixmap(pm);
1194  }
1195  return s;
1196 }
void addPixmap(const QPixmap &pixmap, Mode mode=Normal, State state=Off)
Adds pixmap to the icon, as a specialization for mode and state.
Definition: qicon.cpp:814
QIconPrivate * d
Definition: qicon.h:135
State
This enum describes the state for which a pixmap is intended to be used.
Definition: qicon.h:64
The QString class provides a Unicode character string.
Definition: qstring.h:83
QLatin1String("/iconengines")
QIcon()
Constructs a null icon.
Definition: qicon.cpp:528
Mode
This enum type describes the mode for which a pixmap is intended to be used.
Definition: qicon.h:63
unsigned int uint
Definition: qglobal.h:996
virtual bool read(QDataStream &in)
Reads icon engine contents from the QDataStream in.
The QIconEngineV2 class provides an abstract base class for QIcon renderers.
Definition: qiconengine.h:73
int key
loaderV2
Definition: qicon.cpp:451
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QIconEngine * engine
Definition: qicon_p.h:82
static QString fileName(const QString &fileUrl)
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...
Definition: qicon.cpp:851

Properties

◆ d

QIconPrivate* QIcon::d
private

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