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
qquicktextmetrics.cpp
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
include
"qquicktextmetrics_p.h"
6
7
#
include
<
QFont
>
8
#
include
<
QTextOption
>
9
10
QT_BEGIN_NAMESPACE
11
12
/*!
13
\qmltype TextMetrics
14
\nativetype QQuickTextMetrics
15
\inqmlmodule QtQuick
16
\since 5.4
17
\ingroup qtquick-text-utility
18
\brief Provides metrics for a given font and text.
19
20
TextMetrics calculates various properties of a given string of text for a
21
particular font.
22
23
It provides a declarative API for the functions in \l QFontMetricsF which
24
take arguments.
25
26
\code
27
TextMetrics {
28
id: textMetrics
29
font.family: "Arial"
30
elide: Text.ElideMiddle
31
elideWidth: 100
32
text: "Hello World"
33
}
34
35
MyItem {
36
text: textMetrics.elidedText
37
}
38
\endcode
39
40
\sa QFontMetricsF, FontMetrics
41
*/
42
QQuickTextMetrics::QQuickTextMetrics(QObject *parent) :
43
QObject(parent),
44
m_metrics(m_font),
45
m_elide(Qt::ElideNone),
46
m_elideWidth(0),
47
m_renderType(QQuickText::QtRendering)
48
{
49
}
50
51
/*!
52
\qmlproperty font QtQuick::TextMetrics::font
53
54
This property holds the font used for the metrics calculations.
55
*/
56
QFont QQuickTextMetrics::font()
const
57
{
58
return
m_font;
59
}
60
61
void
QQuickTextMetrics::setFont(
const
QFont &font)
62
{
63
if
(m_font != font) {
64
m_font = font;
65
m_metrics = QFontMetricsF(m_font);
66
emit fontChanged();
67
emit metricsChanged();
68
}
69
}
70
71
/*!
72
\qmlproperty string QtQuick::TextMetrics::text
73
74
This property holds the text used for the metrics calculations.
75
*/
76
QString QQuickTextMetrics::text()
const
77
{
78
return
m_text;
79
}
80
81
void
QQuickTextMetrics::setText(
const
QString &text)
82
{
83
if
(m_text != text) {
84
m_text = text;
85
emit textChanged();
86
emit metricsChanged();
87
}
88
}
89
90
/*!
91
\qmlproperty enumeration QtQuick::TextMetrics::elide
92
93
This property holds the elide mode of the text. This determines the
94
position in which the string is elided. The possible values are:
95
96
\value Qt::ElideNone No eliding; this is the default value.
97
\value Qt::ElideLeft For example: "...World"
98
\value Qt::ElideMiddle For example: "He...ld"
99
\value Qt::ElideRight For example: "Hello..."
100
101
\sa elideWidth, QFontMetrics::elidedText
102
*/
103
Qt::TextElideMode QQuickTextMetrics::elide()
const
104
{
105
return
m_elide;
106
}
107
108
void
QQuickTextMetrics::setElide(Qt::TextElideMode elide)
109
{
110
if
(m_elide != elide) {
111
m_elide = elide;
112
emit elideChanged();
113
emit metricsChanged();
114
}
115
}
116
117
/*!
118
\qmlproperty real QtQuick::TextMetrics::elideWidth
119
120
This property holds the largest width the text can have (in pixels) before
121
eliding will occur.
122
123
\sa elide, QFontMetrics::elidedText
124
*/
125
qreal QQuickTextMetrics::elideWidth()
const
126
{
127
return
m_elideWidth;
128
}
129
130
void
QQuickTextMetrics::setElideWidth(qreal elideWidth)
131
{
132
if
(m_elideWidth != elideWidth) {
133
m_elideWidth = elideWidth;
134
emit elideWidthChanged();
135
emit metricsChanged();
136
}
137
}
138
139
/*!
140
\qmlproperty real QtQuick::TextMetrics::advanceWidth
141
142
This property holds the advance in pixels of the characters in \l text.
143
This is the distance from the position of the string to where the next
144
string should be drawn.
145
146
\sa {QFontMetricsF::horizontalAdvance()}
147
*/
148
qreal QQuickTextMetrics::advanceWidth()
const
149
{
150
QTextOption option;
151
option.setUseDesignMetrics(m_renderType == QQuickText::QtRendering);
152
return
m_metrics.horizontalAdvance(m_text, option);
153
}
154
155
/*!
156
\qmlproperty rect QtQuick::TextMetrics::boundingRect
157
158
This property holds the bounding rectangle of the characters in the string
159
specified by \l text.
160
161
\sa {QFontMetricsF::boundingRect()}, tightBoundingRect
162
*/
163
QRectF QQuickTextMetrics::boundingRect()
const
164
{
165
QTextOption option;
166
option.setUseDesignMetrics(m_renderType == QQuickText::QtRendering);
167
return
m_metrics.boundingRect(m_text, option);
168
}
169
170
/*!
171
\qmlproperty real QtQuick::TextMetrics::width
172
173
This property holds the width of the bounding rectangle of the characters
174
in the string specified by \l text. It is equivalent to:
175
176
\code
177
textMetrics.boundingRect.width
178
\endcode
179
180
\sa boundingRect
181
*/
182
qreal QQuickTextMetrics::width()
const
183
{
184
return
boundingRect().width();
185
}
186
187
/*!
188
\qmlproperty real QtQuick::TextMetrics::height
189
190
This property holds the height of the bounding rectangle of the characters
191
in the string specified by \l text. It is equivalent to:
192
193
\code
194
textMetrics.boundingRect.height
195
\endcode
196
197
\sa boundingRect
198
*/
199
qreal QQuickTextMetrics::height()
const
200
{
201
return
boundingRect().height();
202
}
203
204
/*!
205
\qmlproperty rect QtQuick::TextMetrics::tightBoundingRect
206
207
This property holds a tight bounding rectangle around the characters in the
208
string specified by \l text.
209
210
\sa {QFontMetricsF::tightBoundingRect()}, boundingRect
211
*/
212
QRectF QQuickTextMetrics::tightBoundingRect()
const
213
{
214
QTextOption option;
215
option.setUseDesignMetrics(m_renderType == QQuickText::QtRendering);
216
return
m_metrics.tightBoundingRect(m_text, option);
217
}
218
219
/*!
220
\qmlproperty string QtQuick::TextMetrics::elidedText
221
222
This property holds an elided version of the string (i.e., a string with
223
"..." in it) if the string \l text is wider than \l elideWidth. If the
224
text is not wider than \l elideWidth, or \l elide is set to
225
\c Qt::ElideNone, this property will be equal to the original string.
226
227
\sa {QFontMetricsF::elidedText()}
228
*/
229
QString QQuickTextMetrics::elidedText()
const
230
{
231
return
m_metrics.elidedText(m_text, m_elide, m_elideWidth);
232
}
233
234
/*!
235
\qmlproperty enumeration QtQuick::TextMetrics::renderType
236
237
Override the default rendering type for this component.
238
239
Supported render types are:
240
241
\value TextEdit.QtRendering Text is rendered using a scalable distance field for each glyph.
242
\value TextEdit.NativeRendering Text is rendered using a platform-specific technique.
243
244
This should match the intended \c renderType where you draw the text.
245
246
\since 6.3
247
\sa {Text::renderType}{Text.renderType}
248
*/
249
QQuickText::RenderType QQuickTextMetrics::renderType()
const
250
{
251
return
m_renderType;
252
}
253
254
void
QQuickTextMetrics::setRenderType(QQuickText::RenderType renderType)
255
{
256
if
(m_renderType == renderType)
257
return
;
258
259
m_renderType = renderType;
260
emit renderTypeChanged();
261
}
262
263
QT_END_NAMESPACE
264
265
#
include
"moc_qquicktextmetrics_p.cpp"
QPlatformGraphicsBufferHelper
\inmodule QtGui
qtdeclarative
src
quick
util
qquicktextmetrics.cpp
Generated on
for Qt by
1.14.0