QtBase
v6.3.1
|
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 QIconPrivate * | DataPtr |
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 QDataStream & | operator<< (QDataStream &, const QIcon &) |
Q_GUI_EXPORT QDataStream & | operator>> (QDataStream &, QIcon &) |
Related Functions | |
(Note that these are not member functions.) | |
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 |
|
inlinenoexcept |
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.
|
explicit |
QIcon::~QIcon | ( | ) |
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 909 of file qicon.cpp.
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 1096 of file qicon.cpp.
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 802 of file qicon.cpp.
|
inline |
void QIcon::detach | ( | ) |
|
static |
|
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 1255 of file qicon.cpp.
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 1311 of file qicon.cpp.
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.
Returns true
if there is an icon available for name in the current icon theme, otherwise returns false
.
Definition at line 1362 of file qicon.cpp.
bool QIcon::isDetached | ( | ) | const |
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 1393 of file qicon.cpp.
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.
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 1144 of file qicon.cpp.
QIcon::operator QVariant | ( | ) | const |
|
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 960 of file qicon.cpp.
|
inline |
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 817 of file qicon.cpp.
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 854 of file qicon.cpp.
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 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 1274 of file qicon.cpp.
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 1224 of file qicon.cpp.
|
static |
Sets the search paths for icon themes to paths.
Definition at line 1157 of file qicon.cpp.
|
static |
|
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 1178 of file qicon.cpp.
|
friend |
|
related |
|
friend |
|
related |