Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtquickcontrols-icons.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2017 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtquickcontrols-icons.html
6
\keyword Icons in Qt Quick Controls 2
7
\title Icons in Qt Quick Controls
8
9
In Qt Quick Controls, buttons, item delegates, and menu items are capable
10
of presenting an icon in addition to a text label.
11
12
\section1 Using Icons
13
14
\l {AbstractButton::icon}{AbstractButton} and \l {Action::icon}{Action} provide
15
the following properties through which icons can be set:
16
\list
17
\li \c icon.name
18
\li \c icon.source
19
\li \c icon.width
20
\li \c icon.height
21
\li \c icon.color
22
\li \c icon.cache
23
\endlist
24
25
Theme icons are referenced by a name, and regular icons by a source URL. Both
26
\c icon.name and \c icon.source can be set to ensure that an icon will always
27
be found. If the icon is found in the theme, it will always be used; even if
28
\c icon.source is also set. If the icon is not found in the theme, \c icon.source
29
will be used instead.
30
31
\code
32
Button {
33
icon.name: "edit-cut"
34
icon.source: "images/cut.png"
35
}
36
\endcode
37
38
Each \l {Styling Qt Quick Controls}{Qt Quick Controls 2 style} requests a
39
default icon size and color according to their guidelines, but it is possible
40
to override these by setting the \c icon.width, \c icon.height, and \c icon.color
41
properties.
42
43
The image that is loaded by an icon whose \c width and \c height are not set
44
depends on the type of icon in use. For theme icons, the closest available size
45
will be chosen. For regular icons, the behavior is the same as the \l {Image::}
46
{sourceSize} property of \l Image.
47
48
The icon color is specified by default so that it matches the text color in
49
different states. In order to use an icon with the original colors, set the
50
color to \c "transparent".
51
52
\code
53
Button {
54
icon.color: "transparent"
55
icon.source: "images/logo.png"
56
}
57
\endcode
58
59
For buttons, the \l {AbstractButton::}{display} property can be used to control
60
how the icon and text are displayed within the button.
61
62
The \c icon.cache property controls whether or not the icon image is cached.
63
For more information, see \l {Image::}{cache}.
64
65
\section1 Icon Themes
66
67
Compliant icon themes must follow the freedesktop icon theme specification,
68
which can be obtained here: \l {http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html}.
69
70
Traditionally, only Linux and UNIX support icon themes on the platform level,
71
but it is possible to bundle a compliant icon theme in an application to use
72
themed icons on any platform.
73
74
The default \l {QIcon::themeSearchPaths()}{icon theme search paths} depend on
75
the platform. On Linux and UNIX, the search path will use the \c XDG_DATA_DIRS
76
environment variable if available. All platforms have the resource directory
77
\c :/icons as a fallback. Custom icon theme search paths can be set with
78
\l QIcon::setThemeSearchPaths().
79
80
The following example bundles an icon theme called \e mytheme into the application's
81
resources using \l {The Qt Resource System}{Qt's resource system}.
82
83
\badcode
84
<RCC>
85
<qresource prefix="/">
86
<file>icons/mytheme/index.theme</file>
87
<file>icons/mytheme/32x32/myicon.png</file>
88
<file>icons/mytheme/32x32@2/myicon.png</file>
89
</qresource>
90
</RCC>
91
\endcode
92
93
The \c index.theme file describes the general attributes of the icon theme, and
94
lists the available theme icon directories:
95
96
\badcode
97
[Icon Theme]
98
Name=mytheme
99
Comment=My Icon Theme
100
101
Directories=32x32,32x32@2
102
103
[32x32]
104
Size=32
105
Type=Fixed
106
107
[32x32@2]
108
Size=32
109
Scale=2
110
Type=Fixed
111
\endcode
112
113
In order to use the bundled icon theme, an application should call \l QIcon::setThemeName()
114
before loading the main QML file:
115
116
\code
117
#include <QGuiApplication>
118
#include <QQmlApplicationEngine>
119
#include <QIcon>
120
121
int main(int argc, char *argv[])
122
{
123
QGuiApplication app(argc, argv);
124
125
QIcon::setThemeName("mytheme"); // <--
126
127
QQmlApplicationEngine engine;
128
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
129
return app.exec();
130
}
131
\endcode
132
133
Now it is possible to use named icons from the bundled icon theme without having
134
to specify any fallback source:
135
136
\code
137
Button {
138
icon.name: "myicon"
139
}
140
\endcode
141
142
The \l {Qt Quick Controls - Gallery}{Gallery example} and \l {Qt Quick Controls 2 - Wearable Demo}
143
{Wearable Demo} provide complete runnable applications with a bundled icon theme.
144
*/
qtdeclarative
src
quickcontrols
doc
src
qtquickcontrols-icons.qdoc
Generated on
for Qt by
1.16.1