pub struct Icon<'a> { /* private fields */ }
Expand description
Notice these docs are heavy WIP and not very relevent yet
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.
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:
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 mode might be
used to draw a different icon.
Note: QIcon needs a QGuiApplication instance before the icon is created.
High DPI Icons
There are two ways that QIcon supports high DPI
icons: via addFile() and fromTheme().
addFile() is useful if you have your own custom directory structure and do not need to use the freedesktop.org Icon Theme Specification . Icons created via this approach use Qt’s “@nx” high DPI syntax
Using fromTheme()
is necessary if you plan on following the Icon Theme
Specification. To make QIcon use the high DPI version of an image, add an
additional entry to the appropriate index.theme
file:
Your icon theme directory would then look something like this:
See also: {fowler}{GUI Design Handbook: Iconic Label} {Icons Example}
Licence
The documentation is an adoption of the original Qt Documentation and provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.
Implementations
sourceimpl<'a> Icon<'a>
impl<'a> Icon<'a>
pub fn new() -> Icon<'a>
sourcepub fn swap<I: IconTrait<'a>>(&self, other: &I) -> &Self
pub fn swap<I: IconTrait<'a>>(&self, other: &I) -> &Self
Swaps icon other with this icon. This operation is very fast and never fails.
sourcepub fn pixmap<S: SizeTrait<'a>>(
&self,
size: &S,
mode: Mode,
state: State
) -> Pixmap<'_>
pub fn pixmap<S: SizeTrait<'a>>(
&self,
size: &S,
mode: Mode,
state: State
) -> Pixmap<'_>
Returns a pixmap with the requested size, mode, and state, generating one if necessary. The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
See also: [actual_size()
]
[paint()
]
Overloads Returns a pixmap of size QSize( w, h). The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
Overloads Returns a pixmap of size QSize( extent, extent). The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
Returns a pixmap with the requested window size, mode, and state, generating one 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 that case it will have a devicePixelRatio larger than 1.
See also: [actual_size()
]
[paint()
]
sourcepub fn pixmap_2(&self, w: i32, h: i32, mode: Mode, state: State) -> Pixmap<'_>
pub fn pixmap_2(&self, w: i32, h: i32, mode: Mode, state: State) -> Pixmap<'_>
Returns a pixmap with the requested size, mode, and state, generating one if necessary. The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
See also: [actual_size()
]
[paint()
]
Overloads Returns a pixmap of size QSize( w, h). The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
Overloads Returns a pixmap of size QSize( extent, extent). The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
Returns a pixmap with the requested window size, mode, and state, generating one 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 that case it will have a devicePixelRatio larger than 1.
See also: [actual_size()
]
[paint()
]
sourcepub fn pixmap_3(&self, extent: i32, mode: Mode, state: State) -> Pixmap<'_>
pub fn pixmap_3(&self, extent: i32, mode: Mode, state: State) -> Pixmap<'_>
Returns a pixmap with the requested size, mode, and state, generating one if necessary. The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
See also: [actual_size()
]
[paint()
]
Overloads Returns a pixmap of size QSize( w, h). The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
Overloads Returns a pixmap of size QSize( extent, extent). The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
Returns a pixmap with the requested window size, mode, and state, generating one 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 that case it will have a devicePixelRatio larger than 1.
See also: [actual_size()
]
[paint()
]
sourcepub fn pixmap_4<S: SizeTrait<'a>, W: WindowTrait<'a>>(
&self,
window: &W,
size: &S,
mode: Mode,
state: State
) -> Pixmap<'_>
pub fn pixmap_4<S: SizeTrait<'a>, W: WindowTrait<'a>>(
&self,
window: &W,
size: &S,
mode: Mode,
state: State
) -> Pixmap<'_>
Returns a pixmap with the requested size, mode, and state, generating one if necessary. The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
See also: [actual_size()
]
[paint()
]
Overloads Returns a pixmap of size QSize( w, h). The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
Overloads Returns a pixmap of size QSize( extent, extent). The pixmap might be smaller than requested, but never larger.
Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this function to return pixmaps that are larger than the requested size. Such images will have a devicePixelRatio larger than 1.
Returns a pixmap with the requested window size, mode, and state, generating one 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 that case it will have a devicePixelRatio larger than 1.
See also: [actual_size()
]
[paint()
]
sourcepub fn actual_size<S: SizeTrait<'a>>(
&self,
size: &S,
mode: Mode,
state: State
) -> Size<'_>
pub fn actual_size<S: SizeTrait<'a>>(
&self,
size: &S,
mode: Mode,
state: State
) -> Size<'_>
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.)
See also: [pixmap()
]
[paint()
]
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: [actual_size()
]
[pixmap()
]
[paint()
]
sourcepub fn actual_size_2<S: SizeTrait<'a>, W: WindowTrait<'a>>(
&self,
window: &W,
size: &S,
mode: Mode,
state: State
) -> Size<'_>
pub fn actual_size_2<S: SizeTrait<'a>, W: WindowTrait<'a>>(
&self,
window: &W,
size: &S,
mode: Mode,
state: State
) -> Size<'_>
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.)
See also: [pixmap()
]
[paint()
]
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: [actual_size()
]
[pixmap()
]
[paint()
]
sourcepub fn name(&self) -> String
pub fn name(&self) -> String
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 QIconEngine::IconNameHook.
See also: [from_theme()
]
[IconEngine
]
sourcepub fn paint<P: PainterTrait<'a>, R: RectTrait<'a>>(
&self,
painter: &P,
rect: &R,
alignment: Alignment,
mode: Mode,
state: State
) -> &Self
pub fn paint<P: PainterTrait<'a>, R: RectTrait<'a>>(
&self,
painter: &P,
rect: &R,
alignment: Alignment,
mode: Mode,
state: State
) -> &Self
Uses the painter to paint the icon with specified alignment, required mode, and state into the rectangle rect.
See also: [actual_size()
]
[pixmap()
]
Mode mode, State state) const
Overloads Paints the icon into the rectangle QRect( x, y, w, h).
sourcepub fn paint_2<P: PainterTrait<'a>>(
&self,
painter: &P,
x: i32,
y: i32,
w: i32,
h: i32,
alignment: Alignment,
mode: Mode,
state: State
) -> &Self
pub fn paint_2<P: PainterTrait<'a>>(
&self,
painter: &P,
x: i32,
y: i32,
w: i32,
h: i32,
alignment: Alignment,
mode: Mode,
state: State
) -> &Self
Uses the painter to paint the icon with specified alignment, required mode, and state into the rectangle rect.
See also: [actual_size()
]
[pixmap()
]
Mode mode, State state) const
Overloads Paints the icon into the rectangle QRect( x, y, w, h).
sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
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.
pub fn is_detached(&self) -> bool
pub fn detach(&self) -> &Self
sourcepub fn cache_key(&self) -> i64
pub fn cache_key(&self) -> i64
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: Pixmap::cache_key
sourcepub fn add_pixmap<P: PixmapTrait<'a>>(
&self,
pixmap: &P,
mode: Mode,
state: State
) -> &Self
pub fn add_pixmap<P: PixmapTrait<'a>>(
&self,
pixmap: &P,
mode: Mode,
state: State
) -> &Self
Adds pixmap to the icon, as a specialization for mode and state.
Custom icon engines are free to ignore additionally added pixmaps.
See also: [add_file()
]
sourcepub fn add_file<S: SizeTrait<'a>>(
&self,
file_name: &str,
size: &S,
mode: Mode,
state: State
) -> &Self
pub fn add_file<S: SizeTrait<'a>>(
&self,
file_name: &str,
size: &S,
mode: Mode,
state: State
) -> &Self
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 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).
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: [add_pixmap()
]
Pixmap::device_pixel_ratio
sourcepub fn set_is_mask(&self, is_mask: bool) -> &Self
pub fn set_is_mask(&self, is_mask: bool) -> &Self
Indicate that this icon is a mask image(boolean isMask), and hence can
potentially be modified based on where it’s displayed.
See also: [is_mask()
]
sourcepub fn is_mask(&self) -> bool
pub fn is_mask(&self) -> bool
Returns true
if this icon has been marked as a mask image.
Certain platforms render mask icons differently (for example,
menu icons on MacOS ).
See also: [set_is_mask()
]
sourcepub fn from_theme(name: &str) -> Icon<'a>
pub fn from_theme(name: &str) -> Icon<'a>
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:
-
http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
-
http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
To fetch an icon from the current icon theme:
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().
Note: Qt will make use of GTK’s icon-theme.cache if present to speed up the lookup. These caches can be generated using gtk-update-icon-cache: https://developer.gnome.org/gtk3/stable/gtk-update-icon-cache.html
Note: If an icon can’t be found in the current theme, then it will be searched in fallbackSearchPaths() as an unthemed icon.
See also: [theme_name()
]
[set_theme_name()
]
[theme_search_paths()
]
[fallback_search_paths()
]
Overloads 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:
sourcepub fn from_theme_2<I: IconTrait<'a>>(name: &str, fallback: &I) -> Icon<'a>
pub fn from_theme_2<I: IconTrait<'a>>(name: &str, fallback: &I) -> Icon<'a>
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:
-
http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
-
http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
To fetch an icon from the current icon theme:
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().
Note: Qt will make use of GTK’s icon-theme.cache if present to speed up the lookup. These caches can be generated using gtk-update-icon-cache: https://developer.gnome.org/gtk3/stable/gtk-update-icon-cache.html
Note: If an icon can’t be found in the current theme, then it will be searched in fallbackSearchPaths() as an unthemed icon.
See also: [theme_name()
]
[set_theme_name()
]
[theme_search_paths()
]
[fallback_search_paths()
]
Overloads 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:
sourcepub fn has_theme_icon(name: &str) -> bool
pub fn has_theme_icon(name: &str) -> bool
Returns true
if there is an icon available for name in the
current icon theme, otherwise returns false.
See also: [theme_search_paths()
]
[from_theme()
]
[set_theme_name()
]
sourcepub fn theme_name() -> String
pub fn theme_name() -> String
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: [set_theme_name()
]
[theme_search_paths()
]
[from_theme()
]
[has_theme_icon()
]
sourcepub fn set_theme_name(path: &str)
pub fn set_theme_name(path: &str)
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.
See also: [theme_search_paths()
]
[theme_name()
]