📄 qvalidator.cpp
字号:
#ifndef QT_NO_REGEXP/*! \internal*/QValidator::QValidator(QObjectPrivate &d, QObject *parent) : QObject(d, parent){}/*! \internal*/QValidator::QValidator(QValidatorPrivate &d, QObject *parent) : QObject(d, parent){}class QDoubleValidatorPrivate : public QValidatorPrivate{ Q_DECLARE_PUBLIC(QDoubleValidator)public: QDoubleValidatorPrivate() : QValidatorPrivate() , notation(QDoubleValidator::ScientificNotation) { } QDoubleValidator::Notation notation;};/*! \class QDoubleValidator \brief The QDoubleValidator class provides range checking of floating-point numbers. \ingroup misc QDoubleValidator provides an upper bound, a lower bound, and a limit on the number of digits after the decimal point. It does not provide a fixup() function. You can set the acceptable range in one call with setRange(), or with setBottom() and setTop(). Set the number of decimal places with setDecimals(). The validate() function returns the validation state. QDoubleValidator uses its locale() to interpret the number. For example, in the German locale, "1,234" will be accepted as the fractional number 1.234. In Arabic locales, QDoubleValidator will accept Arabic digits. In addition, QDoubleValidator is always guaranteed to accept a number formatted according to the "C" locale. QDoubleValidator will not accept numbers with thousand-seperators. \sa QIntValidator, QRegExpValidator, {Line Edits Example}*/ /*! \enum QDoubleValidator::Notation \since 4.3 This enum defines the allowed notations for entering a double. \value StandardNotation The string is written as a standard number (i.e. 0.015). \value ScientificNotation The string is written in scientific form. It may have an exponent part(i.e. 1.5E-2).*//*! Constructs a validator object with a \a parent object that accepts any double.*/QDoubleValidator::QDoubleValidator(QObject * parent) : QValidator(*new QDoubleValidatorPrivate , parent){ b = -HUGE_VAL; t = HUGE_VAL; dec = 1000;}/*! Constructs a validator object with a \a parent object. This validator will accept doubles from \a bottom to \a top inclusive, with up to \a decimals digits after the decimal point.*/QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals, QObject * parent) : QValidator(*new QDoubleValidatorPrivate , parent){ b = bottom; t = top; dec = decimals;}#ifdef QT3_SUPPORT/*! \obsolete Constructs a validator object with a \a parent object and a \a name that accepts any double.*/QDoubleValidator::QDoubleValidator(QObject * parent, const char *name) : QValidator(*new QDoubleValidatorPrivate , parent){ setObjectName(QString::fromAscii(name)); b = -HUGE_VAL; t = HUGE_VAL; dec = 1000;}/*! \obsolete Constructs a validator object with a \a parent object, called \a name. This validator will accept doubles from \a bottom to \a top inclusive, with up to \a decimals digits after the decimal point.*/QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals, QObject * parent, const char* name) : QValidator(*new QDoubleValidatorPrivate, parent){ setObjectName(QString::fromAscii(name)); b = bottom; t = top; dec = decimals;}#endif/*! Destroys the validator.*/QDoubleValidator::~QDoubleValidator(){}/*! \fn QValidator::State QDoubleValidator::validate(QString &input, int &pos) const Returns \l Acceptable if the string \a input contains a double that is within the valid range and is in the correct format. Returns \l Intermediate if \a input contains a double that is outside the range or is in the wrong format; e.g. with too many digits after the decimal point or is empty. Returns \l Invalid if the \a input is not a double. Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0) and \a input is a negative double then \l Invalid is returned. If notation() is set to StandardNotation, and the input contains more digits before the decimal point than a double in the valid range may have, \l Invalid is returned. If notation() is ScientificNotation, and the input is not in the valid range, \l Intermediate is returned. The value may yet become valid by changing the exponent. By default, the \a pos parameter is not used by this validator.*/#ifndef LLONG_MAX# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff)#endifQValidator::State QDoubleValidator::validate(QString & input, int &) const{ Q_D(const QDoubleValidator); QLocalePrivate::NumberMode numMode = QLocalePrivate::DoubleStandardMode; switch (d->notation) { case StandardNotation: numMode = QLocalePrivate::DoubleStandardMode; break; case ScientificNotation: numMode = QLocalePrivate::DoubleScientificMode; break; }; QByteArray buff; if (!locale().d()->validateChars(input, numMode, &buff, dec)) { QLocale cl(QLocale::C); if (!cl.d()->validateChars(input, numMode, &buff, dec)) return Invalid; } if (buff.isEmpty()) return Intermediate; if (b >= 0 && buff.startsWith('-')) return Invalid; if (t < 0 && buff.startsWith('+')) return Invalid; bool ok, overflow; double i = QLocalePrivate::bytearrayToDouble(buff.constData(), &ok, &overflow); if (overflow) return Invalid; if (!ok) return Intermediate; if (i >= b && i <= t) return Acceptable; if (d->notation == StandardNotation) { double max = qMax(qAbs(b), qAbs(t)); if (max < LLONG_MAX) { qlonglong n = pow10(numDigits(qlonglong(max))) - 1; if (qAbs(i) > n) return Invalid; } } return Intermediate;}/*! Sets the validator to accept doubles from \a minimum to \a maximum inclusive, with at most \a decimals digits after the decimal point.*/void QDoubleValidator::setRange(double minimum, double maximum, int decimals){ b = minimum; t = maximum; dec = decimals;}/*! \property QDoubleValidator::bottom \brief the validator's minimum acceptable value \sa setRange()*/void QDoubleValidator::setBottom(double bottom){ setRange(bottom, top(), decimals());}/*! \property QDoubleValidator::top \brief the validator's maximum acceptable value \sa setRange()*/void QDoubleValidator::setTop(double top){ setRange(bottom(), top, decimals());}/*! \property QDoubleValidator::decimals \brief the validator's maximum number of digits after the decimal point \sa setRange()*/void QDoubleValidator::setDecimals(int decimals){ setRange(bottom(), top(), decimals);}/*! \property QDoubleValidator::notation \since 4.3 \brief the notation of how a string can describe a number \sa Notation*/void QDoubleValidator::setNotation(Notation newNotation){ Q_D(QDoubleValidator); d->notation = newNotation;}QDoubleValidator::Notation QDoubleValidator::notation() const{ Q_D(const QDoubleValidator); return d->notation;}/*! \class QRegExpValidator \brief The QRegExpValidator class is used to check a string against a regular expression. \ingroup misc QRegExpValidator uses a regular expression (regexp) to determine whether an input string is \l Acceptable, \l Intermediate, or \l Invalid. The regexp can either be supplied when the QRegExpValidator is constructed, or at a later time. When QRegExpValidator determines whether a string is \l Acceptable or not, the regexp is treated as if it begins with the start of string assertion (\bold{^}) and ends with the end of string assertion (\bold{$}); the match is against the entire input string, or from the given position if a start position greater than zero is given. If a string is a prefix of an \l Acceptable string, it is considered \l Intermediate. For example, "" and "A" are \l Intermediate for the regexp \bold{[A-Z][0-9]} (whereas "_" would be \l Invalid). For a brief introduction to Qt's regexp engine, see \l QRegExp. Example of use: \code // regexp: optional '-' followed by between 1 and 3 digits QRegExp rx("-?\\d{1,3}"); QValidator *validator = new QRegExpValidator(rx, this); QLineEdit *edit = new QLineEdit(this); edit->setValidator(validator); \endcode Below we present some examples of validators. In practice they would normally be associated with a widget as in the example above. \code // integers 1 to 9999 QRegExp rx("[1-9]\\d{0,3}"); // the validator treats the regexp as "^[1-9]\\d{0,3}$" QRegExpValidator v(rx, 0); QString s; int pos = 0; s = "0"; v.validate(s, pos); // returns Invalid s = "12345"; v.validate(s, pos); // returns Invalid s = "1"; v.validate(s, pos); // returns Acceptable rx.setPattern("\\S+"); // one or more non-whitespace characters v.setRegExp(rx); s = "myfile.txt"; v.validate(s, pos); // Returns Acceptable s = "my file.txt"; v.validate(s, pos); // Returns Invalid // A, B or C followed by exactly five digits followed by W, X, Y or Z rx.setPattern("[A-C]\\d{5}[W-Z]"); v.setRegExp(rx); s = "a12345Z"; v.validate(s, pos); // Returns Invalid s = "A12345Z"; v.validate(s, pos); // Returns Acceptable s = "B12"; v.validate(s, pos); // Returns Intermediate // match most 'readme' files rx.setPattern("read\\S?me(\.(txt|asc|1st))?"); rx.setCaseSensitive(false); v.setRegExp(rx); s = "readme"; v.validate(s, pos); // Returns Acceptable s = "README.1ST"; v.validate(s, pos); // Returns Acceptable s = "read me.txt"; v.validate(s, pos); // Returns Invalid s = "readm"; v.validate(s, pos); // Returns Intermediate \endcode \sa QRegExp, QIntValidator, QDoubleValidator, {Settings Editor Example}*//*! Constructs a validator with a \a parent object that accepts any string (including an empty one) as valid.*/QRegExpValidator::QRegExpValidator(QObject *parent) : QValidator(parent), r(QString::fromLatin1(".*")){}/*! Constructs a validator with a \a parent object that accepts all strings that match the regular expression \a rx. The match is made against the entire string; e.g. if the regexp is \bold{[A-Fa-f0-9]+} it will be treated as \bold{^[A-Fa-f0-9]+$}.*/QRegExpValidator::QRegExpValidator(const QRegExp& rx, QObject *parent) : QValidator(parent), r(rx){}#ifdef QT3_SUPPORT/*! \obsolete Constructs a validator with a \a parent object and \a name that accepts any string (including an empty one) as valid.*/QRegExpValidator::QRegExpValidator(QObject *parent, const char *name) : QValidator(parent), r(QString::fromLatin1(".*")){ setObjectName(QString::fromAscii(name));}/*! \obsolete Constructs a validator with a \a parent object and a \a name that accepts all strings that match the regular expression \a rx. The match is made against the entire string; e.g. if the regexp is \bold{[A-Fa-f0-9]+} it will be treated as \bold{^[A-Fa-f0-9]+$}.*/QRegExpValidator::QRegExpValidator(const QRegExp& rx, QObject *parent, const char *name) : QValidator(parent), r(rx){ setObjectName(QString::fromAscii(name));}#endif/*! Destroys the validator.*/QRegExpValidator::~QRegExpValidator(){}/*! Returns \l Acceptable if \a input is matched by the regular expression for this validator, \l Intermediate if it has matched partially (i.e. could be a valid match if additional valid characters are added), and \l Invalid if \a input is not matched. The \a pos parameter is set to the length of the \a input parameter. For example, if the regular expression is \bold{\\w\\d\\d} (word-character, digit, digit) then "A57" is \l Acceptable, "E5" is \l Intermediate, and "+9" is \l Invalid. \sa QRegExp::exactMatch()*/QValidator::State QRegExpValidator::validate(QString &input, int& pos) const{ if (r.exactMatch(input)) { return Acceptable; } else { if (const_cast<QRegExp &>(r).matchedLength() == input.size()) { return Intermediate; } else { pos = input.size(); return Invalid; } }}/*! \property QRegExpValidator::regExp \brief the regular expression used for validation*/void QRegExpValidator::setRegExp(const QRegExp& rx){ r = rx;}#endif#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -