QIcon Class▲
-
Header: QIcon
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
-
qmake: QT += gui
-
Group: QIcon is part of Painting Classes, shared
Detailed Description▲
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:
QToolButton *
button =
new
QToolButton;
button-&
gt;setIcon(QIcon("open.xpm"
));
To undo a QIcon, simply set a null icon in its place:
button-&
gt;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 Qt.
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:
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-&
gt;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 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:
[Icon Theme]
Name=
Test
Comment=
Test Theme
Directories=
32
x32/
actions,32
x32@2
/
actions
[32
x32/
actions]
Size=
32
Context=
Actions
Type=
Fixed
# High DPI version of the entry above.
[32
x32@2
/
actions]
Size=
32
Scale=
2
Type=
Fixed
Your icon theme directory would then look something like this:
├── 32
x32
│ └── actions
│ └── appointment-
new
.png
├── 32
x32@2
│ └── actions
│ └── appointment-
new
.png
└── index.theme
Member Type Documentation▲
enum QIcon::Mode▲
This enum type describes the mode for which a pixmap is intended to be used. The currently defined modes are:
Constant |
Value |
Description |
---|---|---|
QIcon::Normal |
0 |
Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available. |
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 |
Display the pixmap when the item represented by the icon is selected. |
enum QIcon::State▲
Member Function Documentation▲
QIcon::QIcon()▲
Constructs a null icon.
QIcon::QIcon(const QPixmap &pixmap)▲
Constructs an icon from a pixmap.
[explicit] QIcon::QIcon(const QString &fileName)▲
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 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.
[explicit] QIcon::QIcon(QIconEngine *engine)▲
Creates an icon with a specific icon engine. The icon takes ownership of the engine.
QIcon::QIcon(const QIcon &other)▲
Constructs a copy of other. This is very fast.
QIcon::QIcon(QIcon &&other)▲
Move-constructs a QIcon instance, making it point to the same object that other was pointing to.
QIcon::~QIcon()▲
Destroys the icon.
QSize QIcon::actualSize(const QSize &size, QIcon::Mode mode = Normal, QIcon::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. The returned size is in device-independent pixels (This is relevant for high-dpi pixmaps.)
See Also▲
void QIcon::addFile(const QString &fileName, const QSize &size = QSize(), QIcon::Mode mode = Normal, QIcon::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