Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QDoubleValidator Class Reference

The QDoubleValidator class provides range checking of floating-point numbers. More...

#include <qvalidator.h>

+ Inheritance diagram for QDoubleValidator:
+ Collaboration diagram for QDoubleValidator:

Public Types

enum  Notation { StandardNotation , ScientificNotation }
 
- Public Types inherited from QValidator
enum  State { Invalid , Intermediate , Acceptable }
 This enum type defines the states in which a validated string can exist. More...
 

Signals

void bottomChanged (double bottom)
 This signal is emitted after the bottom property changed.
 
void topChanged (double top)
 This signal is emitted after the top property changed.
 
void decimalsChanged (int decimals)
 This signal is emitted after the decimals property changed.
 
void notationChanged (QDoubleValidator::Notation notation)
 This signal is emitted after the notation property changed.
 
- Signals inherited from QValidator
void changed ()
 This signal is emitted when any property that may affect the validity of a string has changed.
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QDoubleValidator (QObject *parent=nullptr)
 Constructs a validator object with a parent object that accepts any double.
 
 QDoubleValidator (double bottom, double top, int decimals, QObject *parent=nullptr)
 Constructs a validator object with a parent object.
 
 ~QDoubleValidator ()
 Destroys the validator.
 
QValidator::State validate (QString &, int &) const override
 Returns \l Acceptable if the string input is in the correct format and contains a double within the valid range.
 
void fixup (QString &input) const override
 
void setRange (double bottom, double top, int decimals)
 Sets the validator to accept doubles from minimum to maximum inclusive, with at most decimals digits after the decimal point.
 
void setRange (double bottom, double top)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the validator to accept doubles from minimum to maximum inclusive without changing the number of digits after the decimal point.
 
void setBottom (double)
 
void setTop (double)
 
void setDecimals (int)
 
void setNotation (Notation)
 
double bottom () const
 
double top () const
 
int decimals () const
 
Notation notation () const
 
- Public Member Functions inherited from QValidator
 QValidator (QObject *parent=nullptr)
 Sets up the validator.
 
 ~QValidator ()
 Destroys the validator, freeing any storage and other resources used.
 
void setLocale (const QLocale &locale)
 Sets the locale that will be used for the validator.
 
QLocale locale () const
 Returns the locale for the validator.
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool event (QEvent *event)
 This virtual function receives events to an object and should return true if the event e was recognized and processed.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Properties

double bottom
 the validator's minimum acceptable value
 
double top
 the validator's maximum acceptable value
 
int decimals
 the validator's maximum number of digits after the decimal point
 
Notation notation
 the notation of how a string can describe a number
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Member Functions inherited from QValidator
 QValidator (QObjectPrivate &d, QObject *parent)
 
 QValidator (QValidatorPrivate &d, QObject *parent)
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Detailed Description

The QDoubleValidator class provides range checking of floating-point numbers.

\inmodule QtGui

QDoubleValidator provides an upper bound, a lower bound, and a limit on the number of digits after the decimal point.

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.

Note
The QLocale::NumberOptions set on the locale() also affect the way the number is interpreted. For example, since QLocale::RejectGroupSeparator is not set by default (except on the "C" locale), the validator will accept group separators. If the string passes validation, pass it to locale().toDouble() to obtain its numeric value.
See also
QIntValidator, QRegularExpressionValidator, QLocale::toDouble(), {Line Edits Example}

Definition at line 88 of file qvalidator.h.

Member Enumeration Documentation

◆ Notation

Since
4.3 This enum defines the allowed notations for entering a double.

\value StandardNotation The string is written in the standard format, a whole number part optionally followed by a separator and fractional part, for example {"0.015"}.

\value ScientificNotation The string is written in scientific form, which optionally appends an exponent part to the standard format, for example {"1.5E-2"}.

The whole number part may, as usual, include a sign. This, along with the separators for fractional part, exponent and any digit-grouping, depend on locale. QDoubleValidator doesn't check the placement (which would also depend on locale) of any digit-grouping separators it finds, but it will reject input that contains them if \l QLocale::RejectGroupSeparator is set in locale().numberOptions().

See also
QLocale::numberOptions(), QLocale::decimalPoint(), QLocale::exponential(), QLocale::negativeSign()
Enumerator
StandardNotation 
ScientificNotation 

Definition at line 101 of file qvalidator.h.

Constructor & Destructor Documentation

◆ QDoubleValidator() [1/2]

QDoubleValidator::QDoubleValidator ( QObject * parent = nullptr)
explicit

Constructs a validator object with a parent object that accepts any double.

Definition at line 583 of file qvalidator.cpp.

◆ QDoubleValidator() [2/2]

QDoubleValidator::QDoubleValidator ( double bottom,
double top,
int decimals,
QObject * parent = nullptr )

Constructs a validator object with a parent object.

This validator will accept doubles from bottom to top inclusive, with up to decimals digits after the decimal point.

Definition at line 595 of file qvalidator.cpp.

References bottom, decimals, and top.

◆ ~QDoubleValidator()

QDoubleValidator::~QDoubleValidator ( )

Destroys the validator.

Definition at line 609 of file qvalidator.cpp.

Member Function Documentation

◆ bottom()

double QDoubleValidator::bottom ( ) const
inline

Definition at line 116 of file qvalidator.h.

◆ bottomChanged

void QDoubleValidator::bottomChanged ( double bottom)
signal

This signal is emitted after the bottom property changed.

See also
QDoubleValidator::top(), QDoubleValidator::setTop(), QDoubleValidator::bottom(), QDoubleValidator::setBottom()

Referenced by setRange().

+ Here is the caller graph for this function:

◆ decimals()

int QDoubleValidator::decimals ( ) const
inline

Definition at line 118 of file qvalidator.h.

References dec.

◆ decimalsChanged

void QDoubleValidator::decimalsChanged ( int decimals)
signal

This signal is emitted after the decimals property changed.

Referenced by setRange().

+ Here is the caller graph for this function:

◆ fixup()

void QDoubleValidator::fixup ( QString & input) const
overridevirtual
Since
6.3 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Attempts to fix the input string to an \l Acceptable representation of a double.

The format of the number is determined by \l notation(), \l decimals(), \l locale() and the latter's \l {QLocale::}{numberOptions()}.

To comply with \l notation(), when \l ScientificNotation is used, the fixed value will be represented in its normalized form, which means that any non-zero value will have one non-zero digit before the decimal point.

QString input = "0.98765e2";
val.fixup(input); // input == "9.8765e+01"

To comply with \l decimals(), when it is {-1} the number of digits used will be determined by \l QLocale::FloatingPointShortest. Otherwise, the fractional part of the number is truncated (with rounding, as appropriate) if its length exceeds \l decimals(). When \l notation() is \l ScientificNotation this is done after the number has been put into its normalized form.

input = "-1234.6789";
val.setDecimals(2);
val.setLocale(QLocale::C);
val.fixup(input); // input == "-1234.68"
Note
If \l decimals() is set to, and the string provides, more than {std::numeric_limits<double>::digits10}, digits beyond that many in the fractional part may be changed. The resulting string shall encode the same floating-point number, when parsed to a double.

Reimplemented from QValidator.

Definition at line 731 of file qvalidator.cpp.

References d, QLocaleData::DoubleScientificMode, QLocaleData::DoubleStandardMode, QValidator::locale(), and StandardNotation.

+ Here is the call graph for this function:

◆ notation()

QDoubleValidator::Notation QDoubleValidator::notation ( ) const

Definition at line 895 of file qvalidator.cpp.

References d.

◆ notationChanged

void QDoubleValidator::notationChanged ( QDoubleValidator::Notation notation)
signal

This signal is emitted after the notation property changed.

QDoubleValidator::Notation is not a registered metatype, so for queued connections, you will have to register it with Q_DECLARE_METATYPE() and qRegisterMetaType().

Referenced by setNotation().

+ Here is the caller graph for this function:

◆ setBottom()

void QDoubleValidator::setBottom ( double bottom)

Definition at line 840 of file qvalidator.cpp.

References decimals, setRange(), and top.

+ Here is the call graph for this function:

◆ setDecimals()

void QDoubleValidator::setDecimals ( int decimals)

Definition at line 870 of file qvalidator.cpp.

References bottom, decimals, setRange(), and top.

+ Here is the call graph for this function:

◆ setNotation()

void QDoubleValidator::setNotation ( Notation newNotation)

Definition at line 885 of file qvalidator.cpp.

References QValidator::changed(), d, emit, and notationChanged().

+ Here is the call graph for this function:

◆ setRange() [1/2]

void QDoubleValidator::setRange ( double bottom,
double top )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the validator to accept doubles from minimum to maximum inclusive without changing the number of digits after the decimal point.

Definition at line 826 of file qvalidator.cpp.

References decimals, and setRange().

+ Here is the call graph for this function:

◆ setRange() [2/2]

void QDoubleValidator::setRange ( double minimum,
double maximum,
int decimals )

Sets the validator to accept doubles from minimum to maximum inclusive, with at most decimals digits after the decimal point.

Note
Setting the number of decimals to -1 effectively sets it to unlimited. This is also the value used by a default-constructed validator.

Definition at line 796 of file qvalidator.cpp.

References bottomChanged(), QValidator::changed(), decimals, decimalsChanged(), emit, and topChanged().

Referenced by setBottom(), setDecimals(), setRange(), and setTop().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTop()

void QDoubleValidator::setTop ( double top)

Definition at line 855 of file qvalidator.cpp.

References bottom, decimals, and setRange().

+ Here is the call graph for this function:

◆ top()

double QDoubleValidator::top ( ) const
inline

Definition at line 117 of file qvalidator.h.

◆ topChanged

void QDoubleValidator::topChanged ( double top)
signal

This signal is emitted after the top property changed.

See also
QDoubleValidator::top(), QDoubleValidator::setTop(), QDoubleValidator::bottom(), QDoubleValidator::setBottom()

Referenced by setRange().

+ Here is the caller graph for this function:

◆ validate()

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

Returns \l Acceptable if the string input is in the correct format and contains a double within the valid range.

Returns \l Intermediate if input is in the wrong format or contains a double outside the range.

Returns \l Invalid if the input doesn't represent a double or has too many digits after the decimal point.

Note: If the valid range consists of just positive doubles (e.g. 0.0 to 100.0) and 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 pos parameter is not used by this validator.

Implements QValidator.

Definition at line 640 of file qvalidator.cpp.

References d, QLocaleData::DoubleScientificMode, QLocaleData::DoubleStandardMode, QValidator::locale(), ScientificNotation, and StandardNotation.

+ Here is the call graph for this function:

Property Documentation

◆ bottom

QDoubleValidator::bottom
readwrite

the validator's minimum acceptable value

By default, this property contains a value of -infinity.

See also
setRange()

Definition at line 91 of file qvalidator.h.

Referenced by QDoubleValidator(), setDecimals(), and setTop().

◆ decimals

QDoubleValidator::decimals
readwrite

the validator's maximum number of digits after the decimal point

By default, this property contains a value of -1, which means any number of digits is accepted.

See also
setRange()

Definition at line 93 of file qvalidator.h.

Referenced by QDoubleValidator(), setBottom(), setDecimals(), setRange(), setRange(), and setTop().

◆ notation

QDoubleValidator::notation
readwrite

the notation of how a string can describe a number

Since
4.3

By default, this property is set to ScientificNotation.

See also
Notation

Definition at line 94 of file qvalidator.h.

◆ top

QDoubleValidator::top
readwrite

the validator's maximum acceptable value

By default, this property contains a value of infinity.

See also
setRange()

Definition at line 92 of file qvalidator.h.

Referenced by QDoubleValidator(), setBottom(), and setDecimals().


The documentation for this class was generated from the following files: