The QSqlField class manipulates the fields in SQL database tables and views.
.PP
QSqlField represents the characteristics of a single column in a database table or view, such as the data type and column name. A field also contains the value of the database column, which can be viewed or changed.
.PP
Field data values are stored as QVariants. Using an incompatible type is not permitted. For example:
.PP
.nf
.br
QSqlField f( "myfield", QVariant::Int );
.br
f.setValue( QPixmap() ); // will not work
.br
.fi
.PP
However, the field will attempt to cast certain data types to the field data type where possible:
.PP
.nf
.br
QSqlField f( "myfield", QVariant::Int );
.br
f.setValue( QString("123") ); // casts QString to int
.br
.fi
.PP
QSqlField objects are rarely created explicitly in application code. They are usually accessed indirectly through QSqlRecord or QSqlCursor which already contain a list of fields. For example:
.PP
.nf
.br
QSqlCursor cur( "Employee" ); // create cursor using the 'Employee' table
.br
QSqlField* f = cur.field( "name" ); // use the 'name' field
.br
f->setValue( "Dave" ); // set field value
.br
...
.br
.fi
.PP
In practice we rarely need to extract a pointer to a field at all. The previous example would normally be written:
Clears the value of the field. If the field is read-only, nothing happens. If \fInullify\fR is TRUE (the default), the field is set to NULL.
.SH "bool QSqlField::isNull () const"
Returns TRUE if the field is currently NULL; otherwise returns FALSE.
.SH "bool QSqlField::isReadOnly () const"
Returns TRUE if the field's value is read only; otherwise returns FALSE.
.SH "QString QSqlField::name () const"
Returns the name of the field.
.PP
Example: sql/overview/table4/main.cpp.
.SH "QSqlField & QSqlField::operator= ( const QSqlField & other )"
Sets the field equal to \fIother\fR.
.SH "bool QSqlField::operator== ( const QSqlField & other ) const"
Returns TRUE if the field is equal to \fIother\fR; otherwise returns FALSE. Fields are considered equal when the following field properties are the same:
.TP
name()
.TP
isNull()
.TP
value()
.TP
isReadOnly()
.SH "void QSqlField::setName ( const QString & name )\fC [virtual]\fR"
Sets the name of the field to \fIname\fR.
.SH "void QSqlField::setNull ()\fC [virtual]\fR"
Sets the field to NULL and clears the value using clear(). If the field is read-only, nothing happens.
Sets the read only flag of the field's value to \fIreadOnly\fR.
.PP
See also setValue().
.SH "void QSqlField::setValue ( const QVariant & value )\fC [virtual]\fR"
Sets the value of the field to \fIvalue\fR. If the field is read-only (isReadOnly() returns TRUE), nothing happens. If the data type of \fIvalue\fR differs from the field's current data type, an attempt is made to cast it to the proper type. This preserves the data type of the field in the case of assignment, e.g. a QString to an integer data type. For example:
.PP
.nf
.br
QSqlCursor cur( "Employee" ); // 'Employee' table
.br
QSqlField* f = cur.field( "student_count" ); // an integer field
.br
...
.br
f->setValue( myLineEdit->text() ); // cast the line edit text to an integer
.br
.fi
.PP
See also isReadOnly().
.SH "QVariant::Type QSqlField::type () const"
Returns the field's type as stored in the database. Note that the actual value might have a different type, Numerical values that are too large to store in a long int or double are usually stored as strings to prevent precision loss.