6.0.0-rc4Source

QSqlField Class

The QSqlField class manipulates the fields of SQL database tables and views. More…

Header#include <QSqlField>
CMaketarget_link_libraries(app PRIVATE Nt::Sql)
ModuleNt SQL C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QSqlField(const QString &fieldName = QString(), QMetaType type = QMetaType(), const QString &tableName = QString())
voidsetValue(const QVariant &value)
QVariantvalue() const
voidsetName(const QString &name)
QStringname() const
voidsetTableName(const QString &tableName)
QStringtableName() const
boolisNull() const
voidsetReadOnly(bool readOnly)
boolisReadOnly() const
voidclear()
QMetaTypemetaType() const
voidsetMetaType(QMetaType type)
boolisAutoValue() const
voidsetAutoValue(bool autoVal)
voidsetRequiredStatus(RequiredStatus status)
voidsetRequired(bool required)
RequiredStatusrequiredStatus() const
voidsetLength(int fieldLength)
intlength() const
voidsetPrecision(int precision)
intprecision() const
voidsetDefaultValue(const QVariant &value)
QVariantdefaultValue() const
voidsetGenerated(bool gen)
boolisGenerated() const
boolisValid() const
voidswap(QSqlField &other)
booloperator==(const QSqlField &other) const
booloperator!=(const QSqlField &other) const

Detailed Description

A QSqlField stands for one column: it has a name(), a type (metaType()), a value() and what the database tells about the column, such as requiredStatus(), length() and defaultValue(). Fields are mostly met as the parts of a QSqlRecord.

QSqlField field("age", QMetaType::fromType<int>());
field.setValue(42);
field.isNull();   // false
field.clear();
field.isNull();   // true

A read-only field ignores setValue() and clear(). setGenerated() tells QSqlTableModel and QSqlDriver::sqlStatement() whether the field takes part in the SQL they generate.

See also QSqlRecord.

Differences from Qt

A field is a plain value: copies are not implicitly shared. A value that is set keeps its own type and is not converted to metaType().

Member Type Documentation

enum QSqlField::RequiredStatus

Whether a field must have a value when a row is inserted.

ConstantDescription
QSqlField::UnknownThe database does not tell.
QSqlField::OptionalThe field may be left out.
QSqlField::RequiredThe field needs a value.

Member Function Documentation

[explicit] QSqlField::QSqlField(const QString &fieldName = QString(), QMetaType type = QMetaType(), const QString &tableName = QString())

Constructs a field named fieldName of the type type that belongs to the table tableName. The value of the field is null.

void QSqlField::clear()

Sets the value of the field to a null value of the type of the field. Nothing happens if the field is read-only.

See also isNull() and setValue().

QVariant QSqlField::defaultValue() const

Returns the value that the database gives the field when a row is inserted without one.

See also setDefaultValue().

bool QSqlField::isAutoValue() const

Returns true if the database gives the field its value, as for an auto-incremented key; otherwise returns false.

See also setAutoValue().

bool QSqlField::isGenerated() const

Returns true if the field takes part in generated SQL statements, which is the default; otherwise returns false.

See also setGenerated().

bool QSqlField::isNull() const

Returns true if the value of the field is null; otherwise returns false.

See also clear().

bool QSqlField::isReadOnly() const

Returns true if the value of the field cannot be changed; otherwise returns false.

See also setReadOnly().

bool QSqlField::isValid() const

Returns true if the field has a valid type; otherwise returns false.

int QSqlField::length() const

Returns the length of the field, or a negative number if the database does not tell.

See also setLength().

QMetaType QSqlField::metaType() const

Returns the type of the field in the database.

See also setMetaType().

QString QSqlField::name() const

Returns the name of the field.

See also setName().

bool QSqlField::operator!=(const QSqlField &other) const

Returns true if this field differs from other; otherwise returns false.

bool QSqlField::operator==(const QSqlField &other) const

Returns true if this field equals other in every respect, the value included; otherwise returns false.

int QSqlField::precision() const

Returns the precision of the field, or a negative number if the database does not tell.

See also setPrecision().

RequiredStatus QSqlField::requiredStatus() const

Returns whether the field must have a value when a row is inserted.

See also setRequiredStatus().

void QSqlField::setAutoValue(bool autoVal)

Marks the field as one that the database gives its value if autoVal is true.

See also isAutoValue().

void QSqlField::setDefaultValue(const QVariant &value)

Sets the default value of the field to value.

See also defaultValue().

void QSqlField::setGenerated(bool gen)

Sets whether the field takes part in generated SQL statements to gen.

See also isGenerated().

void QSqlField::setLength(int fieldLength)

Sets the length of the field to fieldLength: the number of characters of a string, for example.

See also length().

void QSqlField::setMetaType(QMetaType type)

Sets the type of the field to type. A null value takes the new type; another value stays as it is.

See also metaType().

void QSqlField::setName(const QString &name)

Sets the name of the field to name.

See also name().

void QSqlField::setPrecision(int precision)

Sets the precision of the field to precision: the number of digits after the decimal point of a number.

See also precision().

void QSqlField::setReadOnly(bool readOnly)

Makes the field read-only if readOnly is true, and writable otherwise. The value of a read-only field cannot be changed.

See also isReadOnly().

void QSqlField::setRequired(bool required)

Sets the required status of the field to Required if required is true, and to Optional otherwise.

See also setRequiredStatus().

void QSqlField::setRequiredStatus(RequiredStatus status)

Sets whether the field must have a value to status.

See also requiredStatus() and setRequired().

void QSqlField::setTableName(const QString &tableName)

Sets the name of the table that the field belongs to, to tableName.

See also tableName().

void QSqlField::setValue(const QVariant &value)

Sets the value of the field to value. Nothing happens if the field is read-only.

See also value(), isReadOnly() and clear().

void QSqlField::swap(QSqlField &other)

Swaps this field with other.

QString QSqlField::tableName() const

Returns the name of the table that the field belongs to, which may be empty.

See also setTableName().

QVariant QSqlField::value() const

Returns the value of the field. Use isNull() to tell a null value.

See also setValue().