QIcon 类提供在不同模式和状态下的可伸缩图标。 更多...
头: | #include <QIcon> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
enum | Mode { Normal, Disabled, Active, Selected } |
enum | State { Off, On } |
QIcon () | |
QIcon (const QPixmap & pixmap ) | |
QIcon (const QString & fileName ) | |
QIcon (QIconEngine * engine ) | |
QIcon (const QIcon & other ) | |
QIcon (QIcon && other ) | |
~QIcon () | |
QSize | actualSize (const QSize & size , QIcon::Mode mode = Normal, QIcon::State state = Off) const |
void | addFile (const QString & fileName , const QSize & size = QSize(), QIcon::Mode mode = Normal, QIcon::State state = Off) |
void | addPixmap (const QPixmap & pixmap , QIcon::Mode mode = Normal, QIcon::State state = Off) |
QList<QSize> | availableSizes (QIcon::Mode mode = Normal, QIcon::State state = Off) const |
qint64 | cacheKey () const |
bool | isMask () const |
bool | isNull () const |
QString | name () const |
void | paint (QPainter * painter , const QRect & rect , Qt::Alignment alignment = Qt::AlignCenter, QIcon::Mode mode = Normal, QIcon::State state = Off) const |
void | paint (QPainter * painter , int x , int y , int w , int h , Qt::Alignment alignment = Qt::AlignCenter, QIcon::Mode mode = Normal, QIcon::State state = Off) const |
QPixmap | pixmap (const QSize & size , QIcon::Mode mode = Normal, QIcon::State state = Off) const |
QPixmap | pixmap (int w , int h , QIcon::Mode mode = Normal, QIcon::State state = Off) const |
QPixmap | pixmap (int extent , QIcon::Mode mode = Normal, QIcon::State state = Off) const |
QPixmap | pixmap (const QSize & size , qreal devicePixelRatio , QIcon::Mode mode = Normal, QIcon::State state = Off) const |
void | setIsMask (bool isMask ) |
void | swap (QIcon & other ) |
QVariant | operator QVariant () const |
QIcon & | operator= (const QIcon & other ) |
QIcon & | operator= (QIcon && other ) |
QStringList | fallbackSearchPaths () |
QString | fallbackThemeName () |
QIcon | fromTheme (const QString & name ) |
QIcon | fromTheme (const QString & name , const QIcon & fallback ) |
bool | hasThemeIcon (const QString & name ) |
void | setFallbackSearchPaths (const QStringList & paths ) |
void | setFallbackThemeName (const QString & name ) |
void | setThemeName (const QString & name ) |
void | setThemeSearchPaths (const QStringList & paths ) |
QString | themeName () |
QStringList | themeSearchPaths () |
QDataStream & | operator<< (QDataStream & stream , const QIcon & icon ) |
QDataStream & | operator>> (QDataStream & stream , QIcon & icon ) |
QIcon 可以从给定像素图集,生成更小、更大、活动及禁用的像素图。Qt 小部件使用这种像素图来展示表示特定动作的图标。
QIcon 的最简单用法是创建一个从 QPixmap 文件或资源,然后使用它,允许 Qt 算出所有要求图标的样式和大小。例如:
QToolButton *button = new QToolButton; button->setIcon(QIcon("open.xpm"));
要撤消 QIcon,只需将其设为 null 图标:
button->setIcon(QIcon());
使用 QImageReader::supportedImageFormats () 和 QImageWriter::supportedImageFormats () 函数来检索支持文件格式的完整列表。
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 () 或 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.
注意: Since Qt 4.2, an icon engine that supports SVG is included.
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 : 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 需要 QGuiApplication 实例在创建图标之前。
There are two ways that QIcon supports high DPI 图标:凭借 addFile () 和 fromTheme ().
addFile () is useful if you have your own custom directory structure and do not need to use the Freedesktop Icon Theme Specification . Icons created via this approach use Qt's "@nx" high DPI syntax .
使用
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
文件:
[Icon Theme] Name=Test Comment=Test Theme Directories=32x32/actions,32x32@2/actions [32x32/actions] Size=32 Context=Actions Type=Fixed # High DPI version of the entry above. [32x32@2/actions] Size=32 Scale=2 Type=Fixed
Your icon theme directory would then look something like this:
├── 32x32 │ └── actions │ └── appointment-new.png ├── 32x32@2 │ └── actions │ └── appointment-new.png └── index.theme
此枚举类型描述旨在使用的像素图模式。目前定义的模式:
常量 | 值 | 描述 |
---|---|---|
QIcon::Normal
|
0
|
显示像素图当用户未与图标交互时,但由图标表示的功能是可用的。 |
QIcon::Disabled
|
1
|
Display the pixmap when the functionality represented by the icon is not available. |
QIcon::Active
|
2
|
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. |
QIcon::Selected
|
3
|
显示像素图当由图标表示的项被选中时。 |
此枚举描述旨在使用的像素图状态。 state 可以是:
常量 | 值 | 描述 |
---|---|---|
QIcon::Off
|
1
|
Display the pixmap when the widget is in an "off" state |
QIcon::On
|
0
|
Display the pixmap when the widget is in an "on" state |
[noexcept]
QIcon::
QIcon
()
构造 null 图标。
构造图标从 pixmap .
[explicit]
QIcon::
QIcon
(const
QString
&
fileName
)
构造图标从文件采用给定 fileName 。将按需加载文件。
若 fileName 包含相对路径 (如:仅文件名),必须相对运行时工作目录查找相关文件。
文件名可以引用实际磁盘文件或应用程序嵌入资源之一。见 资源系统 概述,了解如何在应用程序可执行文件中嵌入图像及其它资源文件的细节。
使用 QImageReader::supportedImageFormats () 和 QImageWriter::supportedImageFormats () 函数来检索支持文件格式的完整列表。
[explicit]
QIcon::
QIcon
(
QIconEngine
*
engine
)
创建图标采用特定图标 engine 。图标拥有引擎的所有权。
构造副本为 other 。这非常快。
[noexcept]
QIcon::
QIcon
(
QIcon
&&
other
)
移动构造 QIcon 实例,使它指向同一对象 other 所指向的。
[noexcept]
QIcon::
~QIcon
()
销毁图标。
Returns the actual size of the icon for the requested size , mode ,和 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.)
Adds an image from the file with the given fileName 到图标,作为特殊化为 size , mode and state . The file will be loaded on demand. Note: custom icon engines are free to ignore additionally added pixmaps.
若 fileName 包含相对路径 (如:仅文件名),必须相对运行时工作目录查找相关文件。
文件名可以引用实际磁盘文件或应用程序嵌入资源之一。见 资源系统 概述,了解如何在应用程序可执行文件中嵌入图像及其它资源文件的细节。
使用 QImageReader::supportedImageFormats () 和 QImageWriter::supportedImageFormats () 函数来检索支持文件格式的完整列表。
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
设备像素比率
set to a value of 2. This can be disabled by setting the environment variable
QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING
(见
QImageReader
).
注意: 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.
另请参阅 addPixmap () 和 QPixmap::devicePixelRatio ().
添加 pixmap 到图标,作为特殊化为 mode and state .
自定义图标引擎可自由忽略额外添加的像素图。
另请参阅 addFile ().
返回可用图标大小的列表为指定 mode and state .
返回的数字标识内容对于此 QIcon 对象。截然不同的 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 () 或 addFile ().
Cache keys are mostly useful in conjunction with caching.
另请参阅 QPixmap::cacheKey ().
[static]
QStringList
QIcon::
fallbackSearchPaths
()
返回用于图标的回退搜索路径。
The fallback search paths are consulted for standalone icon files if the current icon theme or fallback icon theme do not provide results for an icon lookup.
If not set, the fallback search paths will be defined by the platform.
另请参阅 setFallbackSearchPaths () 和 themeSearchPaths ().
[static]
QString
QIcon::
fallbackThemeName
()
返回回退图标主题的名称。
If not set, the fallback icon theme will be defined by the platform.
注意: Platform fallback icon themes are only implemented on Freedesktop based systems at the moment, and the icon theme depends on your desktop settings.
另请参阅 setFallbackThemeName () 和 themeName ().
[static]
QIcon
QIcon::
fromTheme
(const
QString
&
name
)
返回 QIcon 对应 name 在 current icon theme .
If the current theme does not provide an icon for name , fallback icon theme is consulted, before falling back to looking up standalone icon files in the fallback icon search path .
To fetch an icon from the current icon theme:
QIcon undoicon = QIcon::fromTheme("edit-undo");
If an icon theme has not been explicitly set via setThemeName () a platform defined icon theme will be used.
注意: Platform icon themes is only implemented on Freedesktop based systems at the moment, following the Freedesktop Icon Naming Specification . In order to use themed icons on other platforms, you will have to bundle a compliant theme in one of your themeSearchPaths (), and set the appropriate themeName ().
另请参阅 themeName (), fallbackThemeName (), setThemeName (), themeSearchPaths (), fallbackSearchPaths (),和 Freedesktop Icon Naming Specification .
[static]
QIcon
QIcon::
fromTheme
(const
QString
&
name
, const
QIcon
&
fallback
)
这是重载函数。
返回 QIcon 对应 name 在 current icon theme .
If the current theme does not provide an icon for name , fallback icon theme is consulted, before falling back to looking up standalone icon files in the fallback icon search path .
If no icon is found fallback 被返回。
This is useful to provide a guaranteed fallback, regardless of whether the current set of icon themes and fallbacks paths support the requested icon.
例如:
QIcon undoicon = QIcon::fromTheme("edit-undo", QIcon(":/undo.png"));
另请参阅 fallbackThemeName () 和 fallbackSearchPaths ().
[static]
bool
QIcon::
hasThemeIcon
(const
QString
&
name
)
返回
true
if there is an icon available for
name
in the current icon theme or any of the fallbacks, as described by
fromTheme
(),否则返回
false
.
另请参阅 themeSearchPaths (), fromTheme (),和 setThemeName ().
返回
true
if this icon has been marked as a mask image. Certain platforms render mask icons differently (for example, menu icons on macOS).
另请参阅 setIsMask ().
返回
true
若图标为空;否则返回
false
.
图标为空,若既没有像素图也没有文件名。
注意:即使非 null 图标也可能无法创建有效像素图 (如若文件不存在,或无法读取)。
返回用于创建图标的名称,若可用。
从属图标的创建方式,它可能有关联名称。这是用于创建图标的情况采用 fromTheme ().
另请参阅 fromTheme () 和 QIconEngine::iconName ().
使用 painter 来描绘图标采用指定 alignment ,要求 mode ,和 state 到矩形 rect .
另请参阅 actualSize () 和 pixmap ().
这是重载函数。
将图标描绘到矩形 QRect ( x , y , w , h ).
Returns a pixmap with the requested size , mode ,和 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.
另请参阅 actualSize () 和 paint ().
这是重载函数。
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.
这是重载函数。
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.
[since 6.0]
QPixmap
QIcon::
pixmap
(const
QSize
&
size
,
qreal
devicePixelRatio
,
QIcon::Mode
mode
= Normal,
QIcon::State
state
= Off) const
这是重载函数。
Returns a pixmap with the requested size , devicePixelRatio , mode ,和 state , generating one if necessary.
该函数在 Qt 6.0 引入。
另请参阅 actualSize () 和 paint ().
[static]
void
QIcon::
setFallbackSearchPaths
(const
QStringList
&
paths
)
Sets the fallback search paths for icons to paths .
The fallback search paths are consulted for standalone icon files if the current icon theme or fallback icon theme do not provide results for an icon lookup.
例如:
QIcon::setFallbackSearchPaths(QIcon::fallbackSearchPaths() << "my/search/path");
另请参阅 fallbackSearchPaths () 和 setThemeSearchPaths ().
[static]
void
QIcon::
setFallbackThemeName
(const
QString
&
name
)
将回退图标主题设为 name .
The fallback icon theme is consulted for icons not provided by the current icon theme , or if the current icon theme does not exist.
The name should correspond to theme in the same format as documented by setThemeName (), and will be looked up in themeSearchPaths ().
注意: Fallback icon themes should be set before creating QGuiApplication , to ensure correct initialization.
另请参阅 fallbackThemeName (), themeSearchPaths (),和 themeName ().
Indicate that this icon is a mask image(boolean isMask ), and hence can potentially be modified based on where it's displayed.
另请参阅 isMask ().
[static]
void
QIcon::
setThemeName
(const
QString
&
name
)
将当前图标主题设为 name .
The theme will be will be looked up in themeSearchPaths ().
At the moment the only supported icon theme format is the
Freedesktop Icon Theme Specification
。
name
should correspond to a directory name in the themeSearchPath() containing an
index.theme
file describing its contents.
另请参阅 themeSearchPaths (), themeName (),和 Freedesktop Icon Theme Specification .
[static]
void
QIcon::
setThemeSearchPaths
(const
QStringList
&
paths
)
Sets the search paths for icon themes to paths .
The content of paths should follow the theme format documented by setThemeName ().
另请参阅 themeSearchPaths (), fromTheme (),和 setThemeName ().
[noexcept]
void
QIcon::
swap
(
QIcon
&
other
)
交换图标 other 与此图标。此操作非常快且从不失败。
[static]
QString
QIcon::
themeName
()
返回当前图标主题的名称。
If not set, the current icon theme will be defined by the platform.
注意: Platform icon themes are only implemented on Freedesktop based systems at the moment, and the icon theme depends on your desktop settings.
另请参阅 setThemeName (), themeSearchPaths (), fromTheme (),和 hasThemeIcon ().
[static]
QStringList
QIcon::
themeSearchPaths
()
返回图标主题的搜索路径。
The default search paths will be defined by the platform. All platforms will also have the resource directory
:\icons
as a fallback.
另请参阅 setThemeSearchPaths (), fromTheme (),和 setThemeName ().
Returns the icon as a QVariant .
赋值 other 图标到此图标并返回此图标的引用。
[noexcept]
QIcon
&QIcon::
operator=
(
QIcon
&&
other
)
移动赋值 other 到此 QIcon 实例。
写入给定 icon 到给定 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.
读取图像 (或一组图像) 从给定 stream 进给定 icon .