CopperSpice API  1.9.2
QIcon Class Reference

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

Public Types

enum  Mode
 
enum  State
 

Public Methods

 QIcon ()
 
 QIcon (const QIcon &other)
 
 QIcon (const QPixmap &pixmap)
 
 QIcon (const QString &fileName)
 
 QIcon (QIcon &&other)
 
 QIcon (QIconEngine *engine)
 
 ~QIcon ()
 
QSize actualSize (const QSize &size, Mode mode=Normal, State state=Off) const
 
QSize actualSize (QWindow *window, const QSize &size, Mode mode=Normal, State state=Off) const
 
void addFile (const QString &fileName, const QSize &size=QSize (), Mode mode=Normal, State state=Off)
 
void addPixmap (const QPixmap &pixmap, Mode mode=Normal, State state=Off)
 
QList< QSizeavailableSizes (Mode mode=Normal, State state=Off) const
 
qint64 cacheKey () const
 
bool isMask () const
 
bool isNull () const
 
QString name () const
 
 operator QVariant () const
 
QIcon & operator= (const QIcon &other)
 
QIcon & operator= (QIcon &&other)
 
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
 
QPixmap pixmap (const QSize &size, Mode mode=Normal, State state=Off) const
 
QPixmap pixmap (int extent, Mode mode=Normal, State state=Off) const
 
QPixmap pixmap (int w, int h, Mode mode=Normal, State state=Off) const
 
QPixmap pixmap (QWindow *window, const QSize &size, Mode mode=Normal, State state=Off) const
 
void setIsMask (bool isMask)
 
void swap (QIcon &other)
 

Static Public Methods

static QIcon fromTheme (const QString &name, const QIcon &fallback=QIcon ())
 
static bool hasThemeIcon (const QString &name)
 
static void setThemeName (const QString &theme)
 
static void setThemeSearchPaths (const QStringList &paths)
 
static QString themeName ()
 
static QStringList themeSearchPaths ()
 

Friends

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

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 CopperSpice 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 CopperSpice to work out all the required icon styles and sizes.

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

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

void MyWidget::drawIcon(QPainter *painter, QPoint pos) {
QPixmap pixmap = icon.pixmap(QSize(22, 22),
isEnabled() ? QIcon::Normal : QIcon::Disabled, isChecked() ? QIcon::On : QIcon::Off);
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.

QIcon
See also
pixmap(), paint()

Member Enumeration Documentation

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.

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

ConstantValueDescription
QIcon::Off1 Display the pixmap when the widget is in an "off" state
QIcon::On0 Display the pixmap when the widget is in an "on" state

Constructor & Destructor Documentation

QIcon::QIcon ( )

Constructs a null icon.

QIcon::QIcon ( const QPixmap pixmap)

Constructs an icon from a pixmap.

QIcon::QIcon ( const QIcon &  other)

Copy constructs a new QIcon from other.

QIcon::QIcon ( QIcon &&  other)
inline

Move constructs a new QIcon from other.

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. Refer to 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 ( QIconEngine engine)
explicit

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

QIcon::~QIcon ( )

Destroys the icon.

Method Documentation

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()
QSize QIcon::actualSize ( QWindow window,
const QSize size,
Mode  mode = Normal,
State  state = Off 
) const

Returns the actual size of the icon for the requested window, size, mode, and state. The pixmap can be smaller than the requested size. The returned size is in device independent pixels. This is relevant for high DPI pixmaps.

See also
actualSize(), pixmap(), paint()
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. Refer to 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. When you add a non-empty filename to a QIcon, the icon becomes non-null, even if the file does not exist or points to a corrupt file.

See also
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()
QList< QSize > QIcon::availableSizes ( Mode  mode = Normal,
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()
QIcon QIcon::fromTheme ( const QString name,
const QIcon &  fallback = QIcon() 
)
static

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. The following example fetches an icon from the current theme.

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

If you want to provide a fallback for platforms which do not support theme icons specify the second argument.

QIcon undoicon = QIcon::fromTheme("edit-undo", QIcon(":/undo.png"));

The latest version of the freedesktop icon specification and naming specification can be obtained on the following websites.

http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html

http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html

See also
themeName(), setThemeName(), themeSearchPaths()
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.

See also
themeSearchPaths(), fromTheme(), setThemeName()
bool QIcon::isMask ( ) const

Returns true if the current icon has been marked as a mask image. Certain platforms render mask icons differently.

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.

It is worth noting, a non-null icon might not be able to create valid pixmaps. For example, if the file does not exist or can not be read.

QString QIcon::name ( ) const

Returns the name used to create the icon if it is available. 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 QIconEngine::IconNameHook.

See also
fromTheme(), QIconEngine
QIcon::operator QVariant ( ) const

Returns the icon as a QVariant.

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

Copy assigns from other and returns a reference to this object.

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

Move assigns from other and returns a reference to this object.

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

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

See also
actualSize(), pixmap()
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

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

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()
QPixmap QIcon::pixmap ( int  extent,
Mode  mode = Normal,
State  state = Off 
) const
inline

Returns a pixmap of size QSize(extent, extent). The pixmap might be smaller than requested, but never larger.

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

Returns a pixmap of size QSize(w, h). The pixmap might be smaller than requested, but never larger.

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

Returns a pixmap with the requested window, size, mode, and state, creating a QPixmap if necessary. The pixmap can be smaller than the requested size. If window is on a high DPI display the pixmap can be larger. In this case it will have a devicePixelRatio larger than 1.

See also
actualSize(), paint()
void QIcon::setIsMask ( bool  isMask)

If isMask is true this indicates the current icon is a mask image.

See also
isMask()
void QIcon::setThemeName ( const QString theme)
static

Sets the current icon to theme. The theme should correspond to a directory in the themeSearchPath(). The directory must contain an index.theme file.

See also
themeSearchPaths(), themeName()
void QIcon::setThemeSearchPaths ( const QStringList paths)
static

Sets the search paths for icon themes to paths.

See also
themeSearchPaths(), fromTheme(), setThemeName()
void QIcon::swap ( QIcon &  other)
inline

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

QString QIcon::themeName ( )
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.

See also
setThemeName(), themeSearchPaths(), fromTheme(), hasThemeIcon()
QStringList QIcon::themeSearchPaths ( )
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.

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

Friends And Related Function Documentation

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

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. Writing the stream to a file will not produce a valid image file.

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

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