source: trunk/src/gui/widgets/qvalidator.cpp@ 855

Last change on this file since 855 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 26.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <qdebug.h>
43
44#include "qvalidator.h"
45#ifndef QT_NO_VALIDATOR
46#include "private/qobject_p.h"
47#include "private/qlocale_p.h"
48
49#include <limits.h>
50#include <math.h>
51
52QT_BEGIN_NAMESPACE
53
54/*!
55 \class QValidator
56 \brief The QValidator class provides validation of input text.
57
58 The class itself is abstract. Two subclasses, \l QIntValidator and
59 \l QDoubleValidator, provide basic numeric-range checking, and \l
60 QRegExpValidator provides general checking using a custom regular
61 expression.
62
63 If the built-in validators aren't sufficient, you can subclass
64 QValidator. The class has two virtual functions: validate() and
65 fixup().
66
67 \l validate() must be implemented by every subclass. It returns
68 \l Invalid, \l Intermediate or \l Acceptable depending on whether
69 its argument is valid (for the subclass's definition of valid).
70
71 These three states require some explanation. An \l Invalid string
72 is \e clearly invalid. \l Intermediate is less obvious: the
73 concept of validity is difficult to apply when the string is
74 incomplete (still being edited). QValidator defines \l Intermediate
75 as the property of a string that is neither clearly invalid nor
76 acceptable as a final result. \l Acceptable means that the string
77 is acceptable as a final result. One might say that any string
78 that is a plausible intermediate state during entry of an \l
79 Acceptable string is \l Intermediate.
80
81 Here are some examples:
82
83 \list
84
85 \i For a line edit that accepts integers from 10 to 1000 inclusive,
86 42 and 123 are \l Acceptable, the empty string and 5 are \l
87 Intermediate, and "asdf" and 1114 is \l Invalid.
88
89 \i For an editable combobox that accepts URLs, any well-formed URL
90 is \l Acceptable, "http://example.com/," is \l Intermediate
91 (it might be a cut and paste action that accidentally took in a
92 comma at the end), the empty string is \l Intermediate (the user
93 might select and delete all of the text in preparation for entering
94 a new URL) and "http:///./" is \l Invalid.
95
96 \i For a spin box that accepts lengths, "11cm" and "1in" are \l
97 Acceptable, "11" and the empty string are \l Intermediate, and
98 "http://example.com" and "hour" are \l Invalid.
99
100 \endlist
101
102 \l fixup() is provided for validators that can repair some user
103 errors. The default implementation does nothing. QLineEdit, for
104 example, will call fixup() if the user presses Enter (or Return)
105 and the content is not currently valid. This allows the fixup()
106 function the opportunity of performing some magic to make an \l
107 Invalid string \l Acceptable.
108
109 A validator has a locale, set with setLocale(). It is typically used
110 to parse localized data. For example, QIntValidator and QDoubleValidator
111 use it to parse localized representations of integers and doubles.
112
113 QValidator is typically used with QLineEdit, QSpinBox and
114 QComboBox.
115
116 \sa QIntValidator, QDoubleValidator, QRegExpValidator, {Line Edits Example}
117*/
118
119
120/*!
121 \enum QValidator::State
122
123 This enum type defines the states in which a validated string can
124 exist.
125
126 \value Invalid The string is \e clearly invalid.
127 \value Intermediate The string is a plausible intermediate value.
128 \value Acceptable The string is acceptable as a final result;
129 i.e. it is valid.
130
131 \omitvalue Valid
132*/
133
134class QValidatorPrivate : public QObjectPrivate{
135 Q_DECLARE_PUBLIC(QValidator)
136public:
137 QValidatorPrivate() : QObjectPrivate()
138 {
139 }
140
141 QLocale locale;
142};
143
144
145/*!
146 Sets up the validator. The \a parent parameter is
147 passed on to the QObject constructor.
148*/
149
150QValidator::QValidator(QObject * parent)
151 : QObject(*new QValidatorPrivate, parent)
152{
153}
154
155#ifdef QT3_SUPPORT
156/*!
157 \obsolete
158 Sets up the validator. The \a parent and \a name parameters are
159 passed on to the QObject constructor.
160*/
161
162QValidator::QValidator(QObject * parent, const char *name)
163 : QObject(*new QValidatorPrivate, parent)
164{
165 setObjectName(QString::fromAscii(name));
166}
167#endif
168
169/*!
170 Destroys the validator, freeing any storage and other resources
171 used.
172*/
173
174QValidator::~QValidator()
175{
176}
177
178/*!
179 Returns the locale for the validator. The locale is by default initialized to the same as QLocale().
180
181 \sa setLocale()
182 \sa QLocale::QLocale()
183*/
184QLocale QValidator::locale() const
185{
186 Q_D(const QValidator);
187 return d->locale;
188}
189
190/*!
191 Sets the \a locale that will be used for the validator. Unless
192 setLocale has been called, the validator will use the default
193 locale set with QLocale::setDefault(). If a default locale has not
194 been set, it is the operating system's locale.
195
196 \sa locale() QLocale::setDefault()
197*/
198void QValidator::setLocale(const QLocale &locale)
199{
200 Q_D(QValidator);
201 d->locale = locale;
202}
203
204/*!
205 \fn QValidator::State QValidator::validate(QString &input, int &pos) const
206
207 This virtual function returns \l Invalid if \a input is invalid
208 according to this validator's rules, \l Intermediate if it
209 is likely that a little more editing will make the input
210 acceptable (e.g. the user types "4" into a widget which accepts
211 integers between 10 and 99), and \l Acceptable if the input is
212 valid.
213
214 The function can change both \a input and \a pos (the cursor position)
215 if required.
216*/
217
218
219/*!
220 \fn void QValidator::fixup(QString & input) const
221
222 This function attempts to change \a input to be valid according to
223 this validator's rules. It need not result in a valid string:
224 callers of this function must re-test afterwards; the default does
225 nothing.
226
227 Reimplementations of this function can change \a input even if
228 they do not produce a valid string. For example, an ISBN validator
229 might want to delete every character except digits and "-", even
230 if the result is still not a valid ISBN; a surname validator might
231 want to remove whitespace from the start and end of the string,
232 even if the resulting string is not in the list of accepted
233 surnames.
234*/
235
236void QValidator::fixup(QString &) const
237{
238}
239
240
241/*!
242 \class QIntValidator
243 \brief The QIntValidator class provides a validator that ensures
244 a string contains a valid integer within a specified range.
245
246 Example of use:
247
248 \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 0
249
250 Below we present some examples of validators. In practice they would
251 normally be associated with a widget as in the example above.
252
253 \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 1
254
255 Notice that the value \c 999 returns Intermediate. Values
256 consisting of a number of digits equal to or less than the max
257 value are considered intermediate. This is intended because the
258 digit that prevents a number to be in range is not necessarily the
259 last digit typed. This also means that an intermediate number can
260 have leading zeros.
261
262 The minimum and maximum values are set in one call with setRange(),
263 or individually with setBottom() and setTop().
264
265 QIntValidator uses its locale() to interpret the number. For example,
266 in Arabic locales, QIntValidator will accept Arabic digits. In addition,
267 QIntValidator is always guaranteed to accept a number formatted according
268 to the "C" locale.
269
270 \sa QDoubleValidator, QRegExpValidator, {Line Edits Example}
271*/
272
273/*!
274 Constructs a validator with a \a parent object that
275 accepts all integers.
276*/
277
278QIntValidator::QIntValidator(QObject * parent)
279 : QValidator(parent)
280{
281 b = INT_MIN;
282 t = INT_MAX;
283}
284
285
286/*!
287 Constructs a validator with a \a parent, that accepts integers
288 from \a minimum to \a maximum inclusive.
289*/
290
291QIntValidator::QIntValidator(int minimum, int maximum,
292 QObject * parent)
293 : QValidator(parent)
294{
295 b = minimum;
296 t = maximum;
297}
298
299
300#ifdef QT3_SUPPORT
301/*!
302 \obsolete
303
304 Constructs a validator with a \a parent object and a \a name that
305 accepts all integers.
306*/
307
308QIntValidator::QIntValidator(QObject * parent, const char *name)
309 : QValidator(parent)
310{
311 setObjectName(QString::fromAscii(name));
312 b = INT_MIN;
313 t = INT_MAX;
314}
315
316
317/*!
318 \obsolete
319
320 Constructs a validator called \a name with a \a parent, that
321 accepts integers from \a minimum to \a maximum inclusive.
322*/
323
324QIntValidator::QIntValidator(int minimum, int maximum,
325 QObject * parent, const char* name)
326 : QValidator(parent)
327{
328 setObjectName(QString::fromAscii(name));
329 b = minimum;
330 t = maximum;
331}
332#endif
333
334/*!
335 Destroys the validator.
336*/
337
338QIntValidator::~QIntValidator()
339{
340 // nothing
341}
342
343
344/*!
345 \fn QValidator::State QIntValidator::validate(QString &input, int &pos) const
346
347 Returns \l Acceptable if the \a input is an integer within the
348 valid range, \l Intermediate if the \a input is a prefix of an integer in the
349 valid range, and \l Invalid otherwise.
350
351 If the valid range consists of just positive integers (e.g., 32 to 100)
352 and \a input is a negative integer, then Invalid is returned. (On the other
353 hand, if the range consists of negative integers (e.g., -100 to -32) and
354 \a input is a positive integer, then Intermediate is returned, because
355 the user might be just about to type the minus (especially for right-to-left
356 languages).
357
358 \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 2
359
360 By default, the \a pos parameter is not used by this validator.
361*/
362
363static int numDigits(qlonglong n)
364{
365 if (n == 0)
366 return 1;
367 return (int)log10(double(n)) + 1;
368}
369
370static qlonglong pow10(int exp)
371{
372 qlonglong result = 1;
373 for (int i = 0; i < exp; ++i)
374 result *= 10;
375 return result;
376}
377
378QValidator::State QIntValidator::validate(QString & input, int&) const
379{
380 QByteArray buff;
381 if (!locale().d()->validateChars(input, QLocalePrivate::IntegerMode, &buff)) {
382 QLocale cl(QLocale::C);
383 if (!cl.d()->validateChars(input, QLocalePrivate::IntegerMode, &buff))
384 return Invalid;
385 }
386
387 if (buff.isEmpty())
388 return Intermediate;
389
390 if (b >= 0 && buff.startsWith('-'))
391 return Invalid;
392
393 if (t < 0 && buff.startsWith('+'))
394 return Invalid;
395
396 if (buff.size() == 1 && (buff.at(0) == '+' || buff.at(0) == '-'))
397 return Intermediate;
398
399 bool ok, overflow;
400 qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
401 if (overflow || !ok)
402 return Invalid;
403 if (entered >= b && entered <= t) {
404 locale().toInt(input, &ok);
405 return ok ? Acceptable : Intermediate;
406 }
407
408 if (entered >= 0) {
409 // the -entered < b condition is necessary to allow people to type
410 // the minus last (e.g. for right-to-left languages)
411 return (entered > t && -entered < b) ? Invalid : Intermediate;
412 } else {
413 return (entered < b) ? Invalid : Intermediate;
414 }
415}
416
417/*! \reimp */
418void QIntValidator::fixup(QString &input) const
419{
420 QByteArray buff;
421 if (!locale().d()->validateChars(input, QLocalePrivate::IntegerMode, &buff)) {
422 QLocale cl(QLocale::C);
423 if (!cl.d()->validateChars(input, QLocalePrivate::IntegerMode, &buff))
424 return;
425 }
426 bool ok, overflow;
427 qlonglong entered = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);
428 if (ok && !overflow)
429 input = locale().toString(entered);
430}
431
432/*!
433 Sets the range of the validator to only accept integers between \a
434 bottom and \a top inclusive.
435*/
436
437void QIntValidator::setRange(int bottom, int top)
438{
439 b = bottom;
440 t = top;
441}
442
443
444/*!
445 \property QIntValidator::bottom
446 \brief the validator's lowest acceptable value
447
448 By default, this property's value is derived from the lowest signed
449 integer available (typically -2147483647).
450
451 \sa setRange()
452*/
453void QIntValidator::setBottom(int bottom)
454{
455 setRange(bottom, top());
456}
457
458/*!
459 \property QIntValidator::top
460 \brief the validator's highest acceptable value
461
462 By default, this property's value is derived from the highest signed
463 integer available (typically 2147483647).
464
465 \sa setRange()
466*/
467void QIntValidator::setTop(int top)
468{
469 setRange(bottom(), top);
470}
471
472
473#ifndef QT_NO_REGEXP
474
475/*!
476 \internal
477*/
478QValidator::QValidator(QObjectPrivate &d, QObject *parent)
479 : QObject(d, parent)
480{
481}
482
483/*!
484 \internal
485*/
486QValidator::QValidator(QValidatorPrivate &d, QObject *parent)
487 : QObject(d, parent)
488{
489}
490
491class QDoubleValidatorPrivate : public QValidatorPrivate
492{
493 Q_DECLARE_PUBLIC(QDoubleValidator)
494public:
495 QDoubleValidatorPrivate()
496 : QValidatorPrivate()
497 , notation(QDoubleValidator::ScientificNotation)
498 {
499 }
500
501 QDoubleValidator::Notation notation;
502
503 QValidator::State validateWithLocale(QString & input, QLocalePrivate::NumberMode numMode, const QLocale &locale) const;
504};
505
506
507/*!
508 \class QDoubleValidator
509
510 \brief The QDoubleValidator class provides range checking of
511 floating-point numbers.
512
513 QDoubleValidator provides an upper bound, a lower bound, and a
514 limit on the number of digits after the decimal point. It does not
515 provide a fixup() function.
516
517 You can set the acceptable range in one call with setRange(), or
518 with setBottom() and setTop(). Set the number of decimal places
519 with setDecimals(). The validate() function returns the validation
520 state.
521
522 QDoubleValidator uses its locale() to interpret the number. For example,
523 in the German locale, "1,234" will be accepted as the fractional number
524 1.234. In Arabic locales, QDoubleValidator will accept Arabic digits.
525
526 In addition, QDoubleValidator is always guaranteed to accept a number
527 formatted according to the "C" locale. QDoubleValidator will not accept
528 numbers with thousand-separators.
529
530 \sa QIntValidator, QRegExpValidator, {Line Edits Example}
531*/
532
533 /*!
534 \enum QDoubleValidator::Notation
535 \since 4.3
536 This enum defines the allowed notations for entering a double.
537
538 \value StandardNotation The string is written as a standard number
539 (i.e. 0.015).
540 \value ScientificNotation The string is written in scientific
541 form. It may have an exponent part(i.e. 1.5E-2).
542*/
543
544/*!
545 Constructs a validator object with a \a parent object
546 that accepts any double.
547*/
548
549QDoubleValidator::QDoubleValidator(QObject * parent)
550 : QValidator(*new QDoubleValidatorPrivate , parent)
551{
552 b = -HUGE_VAL;
553 t = HUGE_VAL;
554 dec = 1000;
555}
556
557
558/*!
559 Constructs a validator object with a \a parent object. This
560 validator will accept doubles from \a bottom to \a top inclusive,
561 with up to \a decimals digits after the decimal point.
562*/
563
564QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals,
565 QObject * parent)
566 : QValidator(*new QDoubleValidatorPrivate , parent)
567{
568 b = bottom;
569 t = top;
570 dec = decimals;
571}
572
573#ifdef QT3_SUPPORT
574/*!
575 \obsolete
576
577 Constructs a validator object with a \a parent object and a \a name
578 that accepts any double.
579*/
580
581QDoubleValidator::QDoubleValidator(QObject * parent, const char *name)
582 : QValidator(*new QDoubleValidatorPrivate , parent)
583{
584 setObjectName(QString::fromAscii(name));
585 b = -HUGE_VAL;
586 t = HUGE_VAL;
587 dec = 1000;
588}
589
590
591/*!
592 \obsolete
593
594 Constructs a validator object with a \a parent object, called \a
595 name. This validator will accept doubles from \a bottom to \a top
596 inclusive, with up to \a decimals digits after the decimal point.
597*/
598
599QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals,
600 QObject * parent, const char* name)
601 : QValidator(*new QDoubleValidatorPrivate, parent)
602{
603 setObjectName(QString::fromAscii(name));
604 b = bottom;
605 t = top;
606 dec = decimals;
607}
608#endif
609
610/*!
611 Destroys the validator.
612*/
613
614QDoubleValidator::~QDoubleValidator()
615{
616}
617
618
619/*!
620 \fn QValidator::State QDoubleValidator::validate(QString &input, int &pos) const
621
622 Returns \l Acceptable if the string \a input contains a double
623 that is within the valid range and is in the correct format.
624
625 Returns \l Intermediate if \a input contains a double that is
626 outside the range or is in the wrong format; e.g. with too many
627 digits after the decimal point or is empty.
628
629 Returns \l Invalid if the \a input is not a double.
630
631 Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0)
632 and \a input is a negative double then \l Invalid is returned. If notation()
633 is set to StandardNotation, and the input contains more digits before the
634 decimal point than a double in the valid range may have, \l Invalid is returned.
635 If notation() is ScientificNotation, and the input is not in the valid range,
636 \l Intermediate is returned. The value may yet become valid by changing the exponent.
637
638 By default, the \a pos parameter is not used by this validator.
639*/
640
641#ifndef LLONG_MAX
642# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff)
643#endif
644
645QValidator::State QDoubleValidator::validate(QString & input, int &) const
646{
647 Q_D(const QDoubleValidator);
648
649 QLocalePrivate::NumberMode numMode = QLocalePrivate::DoubleStandardMode;
650 switch (d->notation) {
651 case StandardNotation:
652 numMode = QLocalePrivate::DoubleStandardMode;
653 break;
654 case ScientificNotation:
655 numMode = QLocalePrivate::DoubleScientificMode;
656 break;
657 }
658
659 State currentLocaleValidation = d->validateWithLocale(input, numMode, locale());
660 if (currentLocaleValidation == Acceptable || locale().language() == QLocale::C)
661 return currentLocaleValidation;
662 State cLocaleValidation = d->validateWithLocale(input, numMode, QLocale(QLocale::C));
663 return qMax(currentLocaleValidation, cLocaleValidation);
664}
665
666QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QLocalePrivate::NumberMode numMode, const QLocale &locale) const
667{
668 Q_Q(const QDoubleValidator);
669 QByteArray buff;
670 if (!locale.d()->validateChars(input, numMode, &buff, q->dec))
671 return QValidator::Invalid;
672
673 if (buff.isEmpty())
674 return QValidator::Intermediate;
675
676 if (q->b >= 0 && buff.startsWith('-'))
677 return QValidator::Invalid;
678
679 if (q->t < 0 && buff.startsWith('+'))
680 return QValidator::Invalid;
681
682 bool ok, overflow;
683 double i = QLocalePrivate::bytearrayToDouble(buff.constData(), &ok, &overflow);
684 if (overflow)
685 return QValidator::Invalid;
686 if (!ok)
687 return QValidator::Intermediate;
688
689 if (i >= q->b && i <= q->t)
690 return QValidator::Acceptable;
691
692 if (notation == QDoubleValidator::StandardNotation) {
693 double max = qMax(qAbs(q->b), qAbs(q->t));
694 if (max < LLONG_MAX) {
695 qlonglong n = pow10(numDigits(qlonglong(max))) - 1;
696 if (qAbs(i) > n)
697 return QValidator::Invalid;
698 }
699 }
700
701 return QValidator::Intermediate;
702}
703
704
705/*!
706 Sets the validator to accept doubles from \a minimum to \a maximum
707 inclusive, with at most \a decimals digits after the decimal
708 point.
709*/
710
711void QDoubleValidator::setRange(double minimum, double maximum, int decimals)
712{
713 b = minimum;
714 t = maximum;
715 dec = decimals;
716}
717
718/*!
719 \property QDoubleValidator::bottom
720 \brief the validator's minimum acceptable value
721
722 By default, this property contains a value of -infinity.
723
724 \sa setRange()
725*/
726
727void QDoubleValidator::setBottom(double bottom)
728{
729 setRange(bottom, top(), decimals());
730}
731
732
733/*!
734 \property QDoubleValidator::top
735 \brief the validator's maximum acceptable value
736
737 By default, this property contains a value of infinity.
738
739 \sa setRange()
740*/
741
742void QDoubleValidator::setTop(double top)
743{
744 setRange(bottom(), top, decimals());
745}
746
747/*!
748 \property QDoubleValidator::decimals
749 \brief the validator's maximum number of digits after the decimal point
750
751 By default, this property contains a value of 1000.
752
753 \sa setRange()
754*/
755
756void QDoubleValidator::setDecimals(int decimals)
757{
758 setRange(bottom(), top(), decimals);
759}
760
761/*!
762 \property QDoubleValidator::notation
763 \since 4.3
764 \brief the notation of how a string can describe a number
765
766 By default, this property is set to ScientificNotation.
767
768 \sa Notation
769*/
770
771void QDoubleValidator::setNotation(Notation newNotation)
772{
773 Q_D(QDoubleValidator);
774 d->notation = newNotation;
775}
776
777QDoubleValidator::Notation QDoubleValidator::notation() const
778{
779 Q_D(const QDoubleValidator);
780 return d->notation;
781}
782
783/*!
784 \class QRegExpValidator
785 \brief The QRegExpValidator class is used to check a string
786 against a regular expression.
787
788 QRegExpValidator uses a regular expression (regexp) to
789 determine whether an input string is \l Acceptable, \l
790 Intermediate, or \l Invalid. The regexp can either be supplied
791 when the QRegExpValidator is constructed, or at a later time.
792
793 When QRegExpValidator determines whether a string is \l Acceptable
794 or not, the regexp is treated as if it begins with the start of string
795 assertion (\bold{^}) and ends with the end of string assertion
796 (\bold{$}); the match is against the entire input string, or from
797 the given position if a start position greater than zero is given.
798
799 If a string is a prefix of an \l Acceptable string, it is considered
800 \l Intermediate. For example, "" and "A" are \l Intermediate for the
801 regexp \bold{[A-Z][0-9]} (whereas "_" would be \l Invalid).
802
803 For a brief introduction to Qt's regexp engine, see \l QRegExp.
804
805 Example of use:
806 \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 3
807
808 Below we present some examples of validators. In practice they would
809 normally be associated with a widget as in the example above.
810
811 \snippet doc/src/snippets/code/src_gui_widgets_qvalidator.cpp 4
812
813 \sa QRegExp, QIntValidator, QDoubleValidator, {Settings Editor Example}
814*/
815
816/*!
817 Constructs a validator with a \a parent object that accepts
818 any string (including an empty one) as valid.
819*/
820
821QRegExpValidator::QRegExpValidator(QObject *parent)
822 : QValidator(parent), r(QString::fromLatin1(".*"))
823{
824}
825
826/*!
827 Constructs a validator with a \a parent object that
828 accepts all strings that match the regular expression \a rx.
829
830 The match is made against the entire string; e.g. if the regexp is
831 \bold{[A-Fa-f0-9]+} it will be treated as \bold{^[A-Fa-f0-9]+$}.
832*/
833
834QRegExpValidator::QRegExpValidator(const QRegExp& rx, QObject *parent)
835 : QValidator(parent), r(rx)
836{
837}
838
839#ifdef QT3_SUPPORT
840/*!
841 \obsolete
842
843 Constructs a validator with a \a parent object and \a name that accepts
844 any string (including an empty one) as valid.
845*/
846
847QRegExpValidator::QRegExpValidator(QObject *parent, const char *name)
848 : QValidator(parent), r(QString::fromLatin1(".*"))
849{
850 setObjectName(QString::fromAscii(name));
851}
852
853/*!
854 \obsolete
855
856 Constructs a validator with a \a parent object and a \a name that
857 accepts all strings that match the regular expression \a rx.
858
859 The match is made against the entire string; e.g. if the regexp is
860 \bold{[A-Fa-f0-9]+} it will be treated as \bold{^[A-Fa-f0-9]+$}.
861*/
862
863QRegExpValidator::QRegExpValidator(const QRegExp& rx, QObject *parent,
864 const char *name)
865 : QValidator(parent), r(rx)
866{
867 setObjectName(QString::fromAscii(name));
868}
869#endif
870
871/*!
872 Destroys the validator.
873*/
874
875QRegExpValidator::~QRegExpValidator()
876{
877}
878
879/*!
880 Returns \l Acceptable if \a input is matched by the regular
881 expression for this validator, \l Intermediate if it has matched
882 partially (i.e. could be a valid match if additional valid
883 characters are added), and \l Invalid if \a input is not matched.
884
885 The \a pos parameter is set to the length of the \a input parameter.
886
887 For example, if the regular expression is \bold{\\w\\d\\d}
888 (word-character, digit, digit) then "A57" is \l Acceptable,
889 "E5" is \l Intermediate, and "+9" is \l Invalid.
890
891 \sa QRegExp::exactMatch()
892*/
893
894QValidator::State QRegExpValidator::validate(QString &input, int& pos) const
895{
896 if (r.exactMatch(input)) {
897 return Acceptable;
898 } else {
899 if (const_cast<QRegExp &>(r).matchedLength() == input.size()) {
900 return Intermediate;
901 } else {
902 pos = input.size();
903 return Invalid;
904 }
905 }
906}
907
908/*!
909 \property QRegExpValidator::regExp
910 \brief the regular expression used for validation
911
912 By default, this property contains a regular expression with the pattern \c{.*}
913 that matches any string.
914*/
915
916void QRegExpValidator::setRegExp(const QRegExp& rx)
917{
918 r = rx;
919}
920
921#endif
922
923QT_END_NAMESPACE
924
925#endif // QT_NO_VALIDATOR
Note: See TracBrowser for help on using the repository browser.