Qt Jambi Home

com.trolltech.qt.gui
Class QRegExpValidator

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.core.QObject
              extended by com.trolltech.qt.gui.QValidator
                  extended by com.trolltech.qt.gui.QRegExpValidator
All Implemented Interfaces:
QtJambiInterface

public class QRegExpValidator
extends QValidator

The QRegExpValidator class is used to check a string against a regular expression.

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

When QRegExpValidator determines whether a string is Acceptable or not, the regexp is treated as if it begins with the start of string assertion (^) and ends with the end of string assertion ($); 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 Acceptable string, it is considered Intermediate. For example, "" and "A" are Intermediate for the regexp [A-Z][0-9] (whereas "_" would be Invalid).

For a brief introduction to Qt's regexp engine, see QRegExp.

Example of use:

    // 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);

Below we present some examples of validators. In practice they would normally be associated with a widget as in the example above.

    // 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

See Also:
QRegExp, QIntValidator, QDoubleValidator, Editor Example

Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.gui.QValidator
QValidator.QValidationData, QValidator.State
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Constructor Summary
QRegExpValidator(QObject parent)
          Constructs a validator with a parent object that accepts any string (including an empty one) as valid.
QRegExpValidator(QRegExp rx, QObject parent)
          Constructs a validator with a parent object that accepts all strings that match the regular expression rx.
 
Method Summary
static QRegExpValidator fromNativePointer(QNativePointer nativePointer)
          This function returns the QRegExpValidator instance pointed to by nativePointer
 QRegExp regExp()
          Returns the regular expression used for validation.
 void setRegExp(QRegExp rx)
          Sets the regular expression used for validation to rx.
 QValidator.State validate(QValidator.QValidationData input)
          Equivalent to validate(input, ).
 
Methods inherited from class com.trolltech.qt.gui.QValidator
fixup, locale, setLocale
 
Methods inherited from class com.trolltech.qt.core.QObject
blockSignals, childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, property, removeEventFilter, setObjectName, setParent, setProperty, signalsBlocked, startTimer, thread, timerEvent
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
disconnect, disconnect, signalSender
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QRegExpValidator

public QRegExpValidator(QObject parent)

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


QRegExpValidator

public QRegExpValidator(QRegExp rx,
                        QObject parent)

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

The match is made against the entire string; e.g. if the regexp is [A-Fa-f0-9]+ it will be treated as ^[A-Fa-f0-9]+$.

Method Detail

regExp

public final QRegExp regExp()

Returns the regular expression used for validation.

See Also:
setRegExp

setRegExp

public final void setRegExp(QRegExp rx)

Sets the regular expression used for validation to rx.

See Also:
regExp

validate

public QValidator.State validate(QValidator.QValidationData input)

Equivalent to validate(input, ).

Specified by:
validate in class QValidator

fromNativePointer

public static QRegExpValidator fromNativePointer(QNativePointer nativePointer)
This function returns the QRegExpValidator instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

Qt Jambi Home