⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qvalidator.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <qdebug.h>#include "qvalidator.h"#ifndef QT_NO_VALIDATOR#include "private/qobject_p.h"#include "private/qlocale_p.h"#include <limits.h>#include <math.h>/*!    \class QValidator    \brief The QValidator class provides validation of input text.    \ingroup misc    \mainclass    The class itself is abstract. Two subclasses, \l QIntValidator and    \l QDoubleValidator, provide basic numeric-range checking, and \l    QRegExpValidator provides general checking using a custom regular    expression.    If the built-in validators aren't sufficient, you can subclass    QValidator. The class has two virtual functions: validate() and    fixup().    \l validate() must be implemented by every subclass. It returns    \l Invalid, \l Intermediate or \l Acceptable depending on whether    its argument is valid (for the subclass's definition of valid).    These three states require some explanation. An \l Invalid string    is \e clearly invalid. \l Intermediate is less obvious: the    concept of validity is difficult to apply when the string is    incomplete (still being edited). QValidator defines \l Intermediate    as the property of a string that is neither clearly invalid nor    acceptable as a final result. \l Acceptable means that the string    is acceptable as a final result. One might say that any string    that is a plausible intermediate state during entry of an \l    Acceptable string is \l Intermediate.    Here are some examples:    \list    \i For a line edit that accepts integers from 10 to 999 inclusive,    42 and 123 are \l Acceptable, the empty string and 5 are \l    Intermediate, and "asdf" and 1114 is \l Invalid.    \i For an editable combobox that accepts URLs, any well-formed URL    is \l Acceptable, "http://www.trolltech.com/," is \l Intermediate    (it might be a cut and paste action that accidentally took in a    comma at the end), the empty string is \l Intermediate (the user    might select and delete all of the text in preparation for entering    a new URL) and "http:///./" is \l Invalid.    \i For a spin box that accepts lengths, "11cm" and "1in" are \l    Acceptable, "11" and the empty string are \l Intermediate, and    "http://www.trolltech.com" and "hour" are \l Invalid.    \endlist    \l fixup() is provided for validators that can repair some user    errors. The default implementation does nothing. QLineEdit, for    example, will call fixup() if the user presses Enter (or Return)    and the content is not currently valid. This allows the fixup()    function the opportunity of performing some magic to make an \l    Invalid string \l Acceptable.    A validator has a locale, set with setLocale(). It is typically used    to parse localized data. For example, QIntValidator and QDoubleValidator    use it to parse localized representations of integers and doubles.    QValidator is typically used with QLineEdit, QSpinBox and    QComboBox.    \sa QIntValidator, QDoubleValidator, QRegExpValidator, {Line Edits Example}*//*!    \enum QValidator::State    This enum type defines the states in which a validated string can    exist.    \value Invalid       The string is \e clearly invalid.    \value Intermediate  The string is a plausible intermediate value                         during editing.    \value Acceptable    The string is acceptable as a final result;                         i.e. it is valid.    \omitvalue Valid*/class QValidatorPrivate : public QObjectPrivate{    Q_DECLARE_PUBLIC(QValidator)public:    QValidatorPrivate() : QObjectPrivate()    {    }    QLocale locale;};/*!    Sets up the validator. The \a parent parameter is    passed on to the QObject constructor.*/QValidator::QValidator(QObject * parent)    : QObject(*new QValidatorPrivate, parent){}#ifdef QT3_SUPPORT/*!  \obsolete    Sets up the validator. The \a parent and \a name parameters are    passed on to the QObject constructor.*/QValidator::QValidator(QObject * parent, const char *name)    : QObject(*new QValidatorPrivate, parent){    setObjectName(QString::fromAscii(name));}#endif/*!    Destroys the validator, freeing any storage and other resources    used.*/QValidator::~QValidator(){}/*!    Returns the locale for the validator. The locale is by default initialized to the same as QLocale().    \sa setLocale()    \sa QLocale::QLocale()*/QLocale QValidator::locale() const{    Q_D(const QValidator);    return d->locale;}/*!    Sets the \a locale that will be used for the validator. Unless    setLocale has been called, the validator will use the default    locale set with QLocale::setDefault(). If a default locale has not    been set, it is the operating system's locale.    \sa locale() QLocale::setDefault()*/void QValidator::setLocale(const QLocale &locale){    Q_D(QValidator);    d->locale = locale;}/*!    \fn QValidator::State QValidator::validate(QString &input, int &pos) const    This virtual function returns \l Invalid if \a input is invalid    according to this validator's rules, \l Intermediate if it    is likely that a little more editing will make the input    acceptable (e.g. the user types "4" into a widget which accepts    integers between 10 and 99), and \l Acceptable if the input is    valid.    The function can change both \a input and \a pos (the cursor position)    if required.*//*!    \fn void QValidator::fixup(QString & input) const    This function attempts to change \a input to be valid according to    this validator's rules. It need not result in a valid string:    callers of this function must re-test afterwards; the default does    nothing.    Reimplementations of this function can change \a input even if    they do not produce a valid string. For example, an ISBN validator    might want to delete every character except digits and "-", even    if the result is still not a valid ISBN; a surname validator might    want to remove whitespace from the start and end of the string,    even if the resulting string is not in the list of accepted    surnames.*/void QValidator::fixup(QString &) const{}/*!    \class QIntValidator    \brief The QIntValidator class provides a validator that ensures    a string contains a valid integer within a specified range.    \ingroup misc    Example of use:    \code    QValidator *validator = new QIntValidator(100, 999, this);    QLineEdit *edit = new QLineEdit(this);    // the edit lineedit will only accept integers between 100 and 999    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    QString str;    int pos = 0;    QIntValidator v(100, 999, this);    str = "1";    v.validate(str, pos);     // returns Intermediate    str = "12";    v.validate(str, pos);     // returns Intermediate    str = "123";    v.validate(str, pos);     // returns Acceptable    str = "678";    v.validate(str, pos);     // returns Acceptable    str = "1234";    v.validate(str, pos);     // returns Invalid    str = "-123";    v.validate(str, pos);     // returns Invalid    str = "abc";    v.validate(str, pos);     // returns Invalid    str = "12cm";    v.validate(str, pos);     // returns Invalid    \endcode    The minimum and maximum values are set in one call with setRange(),    or individually with setBottom() and setTop().    QIntValidator uses its locale() to interpret the number. For example,    in Arabic locales, QIntValidator will accept Arabic digits. In addition,    QIntValidator is always guaranteed to accept a number formatted according    to the "C" locale.    \sa QDoubleValidator, QRegExpValidator, {Line Edits Example}*//*!    Constructs a validator with a \a parent object that    accepts all integers.*/QIntValidator::QIntValidator(QObject * parent)    : QValidator(parent){    b = INT_MIN;    t = INT_MAX;}/*!    Constructs a validator with a \a parent, that accepts integers    from \a minimum to \a maximum inclusive.*/QIntValidator::QIntValidator(int minimum, int maximum,                              QObject * parent)    : QValidator(parent){    b = minimum;    t = maximum;}#ifdef QT3_SUPPORT/*!  \obsolete    Constructs a validator with a \a parent object and a \a name that    accepts all integers.*/QIntValidator::QIntValidator(QObject * parent, const char *name)    : QValidator(parent){    setObjectName(QString::fromAscii(name));    b = INT_MIN;    t = INT_MAX;}/*!  \obsolete    Constructs a validator called \a name with a \a parent, that    accepts integers from \a minimum to \a maximum inclusive.*/QIntValidator::QIntValidator(int minimum, int maximum,                              QObject * parent, const char* name)    : QValidator(parent){    setObjectName(QString::fromAscii(name));    b = minimum;    t = maximum;}#endif/*!    Destroys the validator.*/QIntValidator::~QIntValidator(){    // nothing}/*!    \fn QValidator::State QIntValidator::validate(QString &input, int &pos) const    Returns \l Acceptable if the \a input is an integer within the    valid range, \l Intermediate if the \a input is an integer outside    the valid range and \l Invalid if the \a input is not an integer.    Note: If the valid range consists of just positive integers (e.g. 32 to 100)    and \a input is a negative integer then Invalid is returned. If \a input    has a greater number of digits than an integer in the valid range can have,    Invalid is returned.    \code    int pos = 0;    s = "abc";    v.validate(s, pos);    // returns Invalid    s = "5";    v.validate(s, pos);    // returns Intermediate    s = "50";    v.validate(s, pos);    // returns Acceptable    \endcode    By default, the \a pos parameter is not used by this validator.*/static int numDigits(qlonglong n){    if (n == 0)        return 1;    return (int)log10(double(n)) + 1;};static qlonglong pow10(int exp){    qlonglong result = 1;    for (int i = 0; i < exp; ++i)        result *= 10;    return result;}QValidator::State QIntValidator::validate(QString & input, int&) const{    QByteArray buff;    if (!locale().d()->validateChars(input, QLocalePrivate::IntegerMode, &buff)) {        QLocale cl(QLocale::C);        if (!cl.d()->validateChars(input, QLocalePrivate::IntegerMode, &buff))            return Invalid;    }    if (buff.isEmpty())        return Intermediate;    if (b >= 0 && buff.startsWith('-'))        return Invalid;    if (t < 0 && buff.startsWith('+'))        return Invalid;    bool ok, overflow;    qlonglong i = QLocalePrivate::bytearrayToLongLong(buff.constData(), 10, &ok, &overflow);    if (overflow)        return Invalid;    if (!ok)        return Intermediate;    if (i >= b && i <= t)        return Acceptable;    qlonglong max = qMax(qAbs(b), qAbs(t));    qlonglong n = pow10(numDigits(max)) - 1;    if (qAbs(i) > n)        return Invalid;    return Intermediate;}/*!    Sets the range of the validator to only accept integers between \a    bottom and \a top inclusive.*/void QIntValidator::setRange(int bottom, int top){    b = bottom;    t = top;}/*!    \property QIntValidator::bottom    \brief the validator's lowest acceptable value    \sa setRange()*/void QIntValidator::setBottom(int bottom){    setRange(bottom, top());}/*!    \property QIntValidator::top    \brief the validator's highest acceptable value    \sa setRange()*/void QIntValidator::setTop(int top){    setRange(bottom(), top);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -