QSqlField Class
The QSqlField class manipulates the fields of SQL database tables and views. More…
| Header | #include <QSqlField> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Sql) |
| Module | Nt SQL C++ Classes |
List of all members, including inherited members
Public Types
| enum | RequiredStatus |
Public Functions
| QSqlField(const QString &fieldName = QString(), QMetaType type = QMetaType(), const QString &tableName = QString()) | |
| void | setValue(const QVariant &value) |
| QVariant | value() const |
| void | setName(const QString &name) |
| QString | name() const |
| void | setTableName(const QString &tableName) |
| QString | tableName() const |
| bool | isNull() const |
| void | setReadOnly(bool readOnly) |
| bool | isReadOnly() const |
| void | clear() |
| QMetaType | metaType() const |
| void | setMetaType(QMetaType type) |
| bool | isAutoValue() const |
| void | setAutoValue(bool autoVal) |
| void | setRequiredStatus(RequiredStatus status) |
| void | setRequired(bool required) |
| RequiredStatus | requiredStatus() const |
| void | setLength(int fieldLength) |
| int | length() const |
| void | setPrecision(int precision) |
| int | precision() const |
| void | setDefaultValue(const QVariant &value) |
| QVariant | defaultValue() const |
| void | setGenerated(bool gen) |
| bool | isGenerated() const |
| bool | isValid() const |
| void | swap(QSqlField &other) |
| bool | operator==(const QSqlField &other) const |
| bool | operator!=(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.
| Constant | Description |
|---|---|
QSqlField::Unknown | The database does not tell. |
QSqlField::Optional | The field may be left out. |
QSqlField::Required | The field needs a value. |
Member Function Documentation
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().