Qt 4.8
|
The QIcon class provides scalable icons in different modes and states. More...
#include <qicon.h>
Public Types | |
typedef QIconPrivate * | DataPtr |
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< QSize > | availableSizes (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... | |
DataPtr & | data_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... | |
QIcon & | operator= (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 | |
QIconPrivate * | d |
Friends | |
Q_GUI_EXPORT QDataStream & | operator<< (QDataStream &, const QIcon &) |
Writes the given icon to the given stream as a PNG image. More... | |
Q_GUI_EXPORT QDataStream & | operator>> (QDataStream &, QIcon &) |
Reads an image, or a set of images, from the given stream into the given icon. More... | |
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:
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 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.
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:
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.
enum QIcon::Mode |
This enum type describes the mode for which a pixmap is intended to be used.
The currently defined modes are:
Enumerator | |
---|---|
Normal | |
Disabled | |
Active | |
Selected |
enum QIcon::State |
QIcon::QIcon | ( | ) |
Constructs a null icon.
Definition at line 528 of file qicon.cpp.
Referenced by fromTheme(), and operator>>().
QIcon::QIcon | ( | const QPixmap & | pixmap | ) |
Constructs an icon from a pixmap.
Definition at line 536 of file qicon.cpp.
QIcon::QIcon | ( | const QIcon & | other | ) |
|
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.
|
explicit |
Creates an icon with a specific icon engine.
The icon takes ownership of the engine.
Definition at line 580 of file qicon.cpp.
|
explicit |
Creates an icon with a specific icon engine.
The icon takes ownership of the engine.
Definition at line 592 of file qicon.cpp.
Returns the actual size of the icon for the requested size, mode, and state.
The result might be smaller than requested, but never larger.
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().
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.
Definition at line 851 of file qicon.cpp.
Referenced by operator>>(), QIcon(), qt_setupActionIcon(), and QCommonStyle::standardIconImplementation().
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 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().
Returns a list of available icon sizes for the specified mode and state.
Definition at line 900 of file qicon.cpp.
Referenced by fromTheme(), qstring_to_xtp(), and QCommonStyle::standardIconImplementation().
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 679 of file qicon.cpp.
Referenced by QWindowsVistaStyle::drawControl(), QWindowsXPStyle::drawControl(), QUrlModel::setUrl(), QCommonStyle::subElementRect(), and QMacStyle::subElementRect().
|
inline |
void QIcon::detach | ( | ) |
Definition at line 783 of file qicon.cpp.
Referenced by addFile(), and addPixmap().
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 latest version of the freedesktop icon specification and naming specification can be obtained here:
To fetch an icon from the current icon theme:
Or 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 1039 of file qicon.cpp.
Referenced by QGtkStylePrivate::getFilesystemIcon(), hasThemeIcon(), QCommonStyle::standardIconImplementation(), and QCommonStyle::standardPixmap().
|
static |
Returns true if there is an icon available for name in the current icon theme, otherwise returns false.
Definition at line 1070 of file qicon.cpp.
bool QIcon::isDetached | ( | ) | const |
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().
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() or icons using a QIconEngine which supports the QIconEngineV2::IconNameHook.
Definition at line 922 of file qicon.cpp.
QIcon::operator QVariant | ( | ) | const |
Returns the icon as a QVariant.
Definition at line 636 of file qicon.cpp.
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 744 of file qicon.cpp.
Referenced by QStyleSheetStyle::drawComplexControl(), QRenderRule::drawImage(), QScriptEdit::extraAreaPaintEvent(), QFontFamilyDelegate::paint(), and QSystemTrayIconSys::paintEvent().
|
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.
Returns a pixmap with the requested size, mode, and state, generating one if necessary.
The pixmap might be smaller than requested, but never larger.
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().
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.
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.
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.
Definition at line 661 of file qicon.cpp.
|
static |
Sets the current icon theme to name.
The name should correspond to a directory name in the themeSearchPath() containing an index.theme file describing it's contents.
Definition at line 982 of file qicon.cpp.
|
static |
Sets the search paths for icon themes to paths.
Definition at line 939 of file qicon.cpp.
|
inline |
|
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 1001 of file qicon.cpp.
Referenced by QCommonStyle::standardIconImplementation(), 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 :
as a fallback. You can use "rcc -project" to generate a resource file from your icon theme.
Definition at line 963 of file qicon.cpp.
Referenced by QIconTheme::QIconTheme().
|
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. Note that writing the stream to a file will not produce a valid image file.
Definition at line 1096 of file qicon.cpp.
|
friend |
Reads an image, or a set of images, from the given stream into the given icon.
Definition at line 1144 of file qicon.cpp.
|
private |
Definition at line 135 of file qicon.h.
Referenced by actualSize(), addFile(), addPixmap(), availableSizes(), cacheKey(), detach(), isDetached(), isNull(), name(), operator<<(), operator=(), operator>>(), paint(), pixmap(), QIcon(), serialNumber(), swap(), and ~QIcon().