CopperSpice API  1.9.1
QValidator Class Referenceabstract

The QValidator class provides validation of input text. More...

Inheritance diagram for QValidator:
QObject QDoubleValidator QIntValidator QRegularExpressionValidator

Public Types

enum  State
 

Public Signals

void changed ()
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Methods

 QValidator (QObject *parent=nullptr)
 
 ~QValidator ()
 
virtual void fixup (QString &input) const
 
QLocale locale () const
 
void setLocale (const QLocale &locale)
 
virtual State validate (QString &input, int &pos) const = 0
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The QValidator class provides validation of input text, it is an abstract class. QIntValidator and QDoubleValidator inherit form QValidator and provide basic numeric-range checking. QRegularExpressionValidator provides general checking using a custom regular expression.

If the built-in validators are not sufficient you can subclass QValidator. The class has two virtual methods, validate() and fixup(). The validate() methods must be implemented by every subclass. It returns Invalid, Intermediate or Acceptable depending on whether its argument is valid for the subclass definition of valid.

Validation states

An Invalid string is clearly invalid.

Intermediate is less obvious. The concept of validity is difficult to apply when the string is incomplete (still being edited). QValidator defines Intermediate as the property of a string that is neither clearly invalid nor acceptable as a final result.

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 Acceptable string is Intermediate.

  • For a line edit which accepts integers from 10 to 1000 inclusive

    • 42 and 123 are Acceptable
    • an empty string and the number 5 are Intermediate
    • "asdf" and 1114 are both Invalid
     
  • Given an editable combobox which accepts a URL

    • any well formed URL is Acceptable.
    • "http://example.com/," is Intermediate (user might be a cut and paste action that accidentally took in a comma at the end)
    • an empty string is Intermediate (user might select and delete all of the text in preparation for entering a new URL)
    • "http:///./" is Invalid
     
  • Give a spin box which accepts lengths

    • "11cm" and "1in" are Acceptable
    • "11" and the empty string are Intermediate
    • "http://example.com" and "hour" are both Invalid

The fixup() method 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 Invalid string 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.

See also
QIntValidator, QDoubleValidator, QRegularExpressionValidator

Member Enumeration Documentation

This enum type defines the states in which a validated string can exist.

ConstantValueDescription
QValidator::Invalid0The string is clearly invalid.
QValidator::Intermediate1The string is a plausible intermediate value.
QValidator::Acceptable2The string is acceptable as a final result; i.e. it is valid.

Constructor & Destructor Documentation

QValidator::QValidator ( QObject parent = nullptr)
explicit

Sets up the validator. The parent object is passed to the QObject constructor.

QValidator::~QValidator ( )

Destroys the validator, freeing any storage and other resources used.

Method Documentation

void QValidator::changed ( )
signal

This signal is emitted when any property which can affect the validity has changed.

void QValidator::fixup ( QString input) const
virtual

This function attempts to change 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 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.

Reimplemented in QIntValidator::fixup()

QLocale QValidator::locale ( ) const

Returns the locale for the validator. The locale is by default initialized to the same as QLocale().

See also
setLocale(), QLocale::QLocale()
void QValidator::setLocale ( const QLocale locale)

Sets the 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.

See also
locale(), QLocale::setDefault()
State QValidator::validate ( QString input,
int &  pos 
) const
pure virtual

This method returns Invalid if input is invalid according to this validator's rules, Intermediate if it is likely that a little more editing will make the input acceptable, and Acceptable if the input is valid.

The method can change both input and pos (the cursor position) if required.

Implemented in QRegularExpressionValidator::validate(), QDoubleValidator::validate(), QIntValidator::validate()