CopperSpice API  1.7.2
QRegularExpressionValidator Class Reference

Validates a string based on a regular expression. More...

Inheritance diagram for QRegularExpressionValidator:
QValidator QObject

Public Signals

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

Public Slots

void setRegularExpression (const QRegularExpression &regExp)
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QRegularExpressionValidator (const QRegularExpression &regExp, QObject *parent=nullptr)
 
 QRegularExpressionValidator (QObject *parent=nullptr)
 
 ~QRegularExpressionValidator ()
 
const QRegularExpressionregularExpression () const
 
QValidator::State validate (QString &input, int &pos) const override
 
- Public Methods inherited from QValidator
 QValidator (QObject *parent=nullptr)
 
 ~QValidator ()
 
virtual void fixup (QString &input) const
 
QLocale locale () const
 
void setLocale (const QLocale &locale)
 
- 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 &objName=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
 

Properties

 regularExpression
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

- Public Types inherited from QValidator
enum  State
 
- 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)
 
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)
 

Detailed Description

The QRegularExpressionValidator class is validate a string based a regular expression.

QRegularExpressionValidator uses a regular expression (regexp) to determine whether an input string is Acceptable, Intermediate, or Invalid. The regexp can either be supplied when the QRegularExpressionValidator is constructed, or at a later time.

If the regexp partially matches against the string, the result is considered Intermediate. For example "" and "A" are Intermediate for the regexp [A-Z][0-9], whereas "_" would be Invalid.

QRegularExpressionValidator automatically wraps the regular expression in the \A and \z anchors, in other words it always attempts to do an exact match.

QRegularExpression regExp("-?\d{1,3}"); // optional '-' followed by between 1 and 3 digits
QValidator *v = new QRegularExpressionValidator(regExp, this);
QLineEdit *edit = new QLineEdit(this);
edit->setValidator(v);

Validator Example

The following regular expression declarations are the setup for the four examples.

QRegularExpression regExp("[1-9]\d{0,3}");
QString str;
int pos = 0;

Example 1:

str = "0";
v.validate(s, pos); // returns Invalid
str = "12345";
v.validate(str, pos); // returns Invalid
str = "1";
v.validate(str, pos); // returns Acceptable

Example 2:

regExp.setPattern("\S+"); // one or more non-whitespace characters
v.setRegularExpression(regExp);
str = "myfile.txt";
v.validate(str, pos); // returns Acceptable
str = "my file.txt";
v.validate(str, pos); // returns Invalid

Example 3:

regExp.setPattern("[A-C]\d{5}[W-Z]"); // A, B or C followed by exactly five digits followed by W, X, Y or Z
v.setRegularExpression(regExp);
str = "a12345Z";
v.validate(str, pos); // returns Invalid
str = "A12345Z";
v.validate(str, pos); // returns Acceptable
str = "B12";
v.validate(str, pos); // returns Intermediate

Example 4:

regExp.setPattern("read\S?me(\.(txt|asc|1st))?");
regExp.setPatternOptions(QPatternOption::CaseInsensitiveOption);
v.setRegularExpression(regExp);
str = "readme";
v.validate(str, pos); // returns Acceptable
str = "README.1ST";
v.validate(str, pos); // returns Acceptable
str = "read me.txt";
v.validate(str, pos); // returns Invalid
str = "readm";
v.validate(str, pos); // returns Intermediate
See also
QRegularExpression, QIntValidator, QDoubleValidator

Constructor & Destructor Documentation

QRegularExpressionValidator::QRegularExpressionValidator ( QObject parent = nullptr)
explicit

Constructs a validator with a parent object that accepts any string (including an empty one) as valid.

QRegularExpressionValidator::QRegularExpressionValidator ( const QRegularExpression regExp,
QObject parent = nullptr 
)
explicit

Constructs a validator with a parent object that accepts all strings that match the regular expression regExp.

QRegularExpressionValidator::~QRegularExpressionValidator ( )

Destroys the validator.

Method Documentation

const QRegularExpression & QRegularExpressionValidator::regularExpression ( ) const

Retrieves the regular expression saved with setRegularExpression().

void QRegularExpressionValidator::regularExpressionChanged ( const QRegularExpression regExp)
signal

This signal is emitted when the value of the regular expression is changed.

void QRegularExpressionValidator::setRegularExpression ( const QRegularExpression regExp)
slot

Stores regExp with the option QPatternOption::ExactMatchOption enabled. The regular expression is used when validating input to the validate() method.

QValidator::State QRegularExpressionValidator::validate ( QString input,
int &  pos 
) const
overridevirtual

Returns Acceptable if input is matched by the regular expression for this validator, Intermediate if it has matched partially (i.e. could be a valid match if additional valid characters are added), and Invalid if input is not matched. In case the input is not matched the pos parameter is set to the length of the input parameter otherwise, it is not modified.

As an example, if the regular expression is \w\d\d (word-character, digit, digit) then "A57" is Acceptable, "E5" is Intermediate, and "+9" is Invalid.

See also
QRegularExpression::match()

Implements QValidator.

Property Documentation

QRegularExpressionValidator::regularExpression

This property holds the regular expression used for validation. By default this property contains a regular expression with an empty pattern (which therefore matches any string).

PropertiesClass Methods
read regularExpression
write setRegularExpression
notify regularExpressionChanged