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
qquickvalidator.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
6
7QT_BEGIN_NAMESPACE
8
9#if QT_CONFIG(validator)
10
11/*!
12 \qmltype IntValidator
13 \nativetype QIntValidator
14 \inqmlmodule QtQuick
15 \ingroup qtquick-text-utility
16 \ingroup qtquick-text-validators
17 \brief Defines a validator for integer values.
18
19 The IntValidator type provides a validator for integer values.
20
21 If no \l locale is set IntValidator uses the \l {QLocale::setDefault()}{default locale} to
22 interpret the number and will accept locale specific digits, group separators, and positive
23 and negative signs. In addition, IntValidator is always guaranteed to accept a number
24 formatted according to the "C" locale.
25
26 The following example shows a TextInput object with an IntValidator to check
27 that the user has input an integer within the specified range, updating the
28 text color to highlight invalid input:
29
30 \snippet qml/intvalidator.qml 0
31
32 The validator will prevent the submission of text which can't possibly be
33 valid. However, while editing, only easily identified invalidity will be
34 blocked, for example sign conflicts or too many non-zero digits. This can
35 sometimes be surprising. The validator in the example above will for
36 instance allow entering "999", as values consisting of a number of digits
37 equal to or less than the max value are considered "intermediate" --
38 meaning that they are in a state where they are not valid, but could be
39 adjusted to be so. This is intended because the digit that prevents a
40 number from being in range is not necessarily the last digit typed. This
41 also means that an intermediate number can have leading zeros.
42
43 Adding a visual indicator based on TextInput's \l{TextInput::acceptableInput}{acceptableInput}
44 property can make clear to the user whether what they've typed will actually be accepted.
45
46 \sa DoubleValidator, RegularExpressionValidator, {Validating Input Text}
47*/
48
49QQuickIntValidator::QQuickIntValidator(QObject *parent)
50 : QIntValidator(parent)
51{
52}
53
54/*!
55 \qmlproperty string QtQuick::IntValidator::locale
56
57 This property holds the name of the locale used to interpret the number.
58
59 \sa {QtQml::Qt::locale()}{Qt.locale()}
60*/
61
62QString QQuickIntValidator::localeName() const
63{
64 return locale().name();
65}
66
67void QQuickIntValidator::setLocaleName(const QString &name)
68{
69 if (locale().name() != name) {
70 setLocale(QLocale(name));
71 emit localeNameChanged();
72 }
73}
74
75void QQuickIntValidator::resetLocaleName()
76{
77 QLocale defaultLocale;
78 if (locale() != defaultLocale) {
79 setLocale(defaultLocale);
80 emit localeNameChanged();
81 }
82}
83
84/*!
85 \qmlproperty int QtQuick::IntValidator::top
86
87 This property holds the validator's highest acceptable value.
88 By default, this property's value is derived from the highest signed integer available (typically 2147483647).
89*/
90/*!
91 \qmlproperty int QtQuick::IntValidator::bottom
92
93 This property holds the validator's lowest acceptable value.
94 By default, this property's value is derived from the lowest signed integer available (typically -2147483647).
95*/
96
97/*!
98 \qmltype DoubleValidator
99 \nativetype QDoubleValidator
100 \inqmlmodule QtQuick
101 \ingroup qtquick-text-utility
102 \ingroup qtquick-text-validators
103 \brief Defines a validator for non-integer numbers.
104
105 The DoubleValidator type provides a validator for non-integer numbers.
106
107 \list
108 \li Accepted Input: Input is accepted if it contains a double that is within
109 the valid range and is in the correct format.
110 \li Accepted but Invalid Input: Input is accepted but considered invalid if
111 it contains a double that is outside the valid range or is in the wrong
112 format (for example, too many digits after the decimal point or empty).
113 \li Rejected Input: Input is rejected if it is not a double.
114 \endlist
115
116 \note If the valid range consists of only positive doubles (for example,
117 0.0 to 100.0) and the input is a negative double, it is rejected. If
118 \l notation is set to \c DoubleValidator.StandardNotation and the input
119 contains more digits before the decimal point than a double in the valid
120 range may have, it is also rejected. If \l notation is
121 \c DoubleValidator.ScientificNotation and the input is not in the valid
122 range, it is accepted but invalid. The value may become valid by changing
123 the exponent.
124
125 The following example shows a TextInput object with a DoubleValidator to
126 check that the user has input a double within the specified range,
127 updating the text color to highlight invalid input:
128
129 \snippet qml/doublevalidator.qml 0
130
131 \sa IntValidator, RegularExpressionValidator, {Validating Input Text}
132*/
133
134QQuickDoubleValidator::QQuickDoubleValidator(QObject *parent)
135 : QDoubleValidator(parent)
136{
137}
138
139/*!
140 \qmlproperty string QtQuick::DoubleValidator::locale
141
142 This property holds the name of the locale used to interpret the number.
143
144 \sa {QtQml::Qt::locale()}{Qt.locale()}
145*/
146
147QString QQuickDoubleValidator::localeName() const
148{
149 return locale().name();
150}
151
152void QQuickDoubleValidator::setLocaleName(const QString &name)
153{
154 if (locale().name() != name) {
155 setLocale(QLocale(name));
156 emit localeNameChanged();
157 }
158}
159
160void QQuickDoubleValidator::resetLocaleName()
161{
162 QLocale defaultLocale;
163 if (locale() != defaultLocale) {
164 setLocale(defaultLocale);
165 emit localeNameChanged();
166 }
167}
168
169/*!
170 \qmlproperty real QtQuick::DoubleValidator::top
171
172 This property holds the validator's maximum acceptable value.
173 By default, this property contains a value of infinity.
174*/
175/*!
176 \qmlproperty real QtQuick::DoubleValidator::bottom
177
178 This property holds the validator's minimum acceptable value.
179 By default, this property contains a value of -infinity.
180*/
181/*!
182 \qmlproperty int QtQuick::DoubleValidator::decimals
183
184 This property holds the validator's maximum number of digits after the decimal point.
185 By default, this property contains a value of 1000.
186*/
187/*!
188 \qmlproperty enumeration QtQuick::DoubleValidator::notation
189 This property holds the notation of how a string can describe a number.
190
191 The possible values for this property are:
192
193 \value DoubleValidator.StandardNotation only decimal numbers with optional sign (e.g. \c -0.015)
194 \value DoubleValidator.ScientificNotation (default) the written number may have an exponent part (e.g. \c 1.5E-2)
195*/
196
197/*!
198 \qmltype RegularExpressionValidator
199 \nativetype QRegularExpressionValidator
200 \inqmlmodule QtQuick
201 \ingroup qtquick-text-utility
202 \ingroup qtquick-text-validators
203 \brief Provides a string validator.
204 \since 5.14
205
206 The RegularExpressionValidator type provides a validator, that counts as valid any string which
207 matches a specified regular expression.
208
209 \sa IntValidator, DoubleValidator, {Validating Input Text}
210*/
211/*!
212 \qmlproperty regularExpression QtQuick::RegularExpressionValidator::regularExpression
213
214 This property holds the regular expression used for validation.
215
216 Note that this property should be a regular expression in JS syntax, e.g /a/ for the regular
217 expression matching "a".
218
219 By default, this property contains a regular expression with the pattern \c{.*} that matches any
220 string.
221
222 Below you can find an example of a \l TextInput object with a RegularExpressionValidator
223 specified:
224
225 \snippet qml/regularexpression.qml 0
226
227 Some more examples of regular expressions:
228
229 \list
230 \li A list of numbers with one to three positions separated by a comma:
231 \badcode
232 /\d{1,3}(?:,\d{1,3})+$/
233 \endcode
234
235 \li An amount consisting of up to 3 numbers before the decimal point, and
236 1 to 2 after the decimal point:
237 \badcode
238 /(\d{1,3})([.,]\d{1,2})?$/
239 \endcode
240 \endlist
241*/
242
243#endif // validator
244
245QT_END_NAMESPACE
246
247#include "moc_qquickvalidator_p.cpp"