Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

QSqlModel Class Reference

The QSqlModel class provides a read only data model for SQL result sets. More...

#include <QSqlModel>

Inherits QAbstractTableModel.

Inherited by QSqlTableModel.

List of all members.

Writable Properties

Public Functions

Public Slots

Signals

Static Public Members

Protected Functions


Detailed Description

The QSqlModel class provides a read only data model for SQL result sets.

QSqlModel is a data model that provides data from a QSqlQuery. The QSqlQuery has to be valid and may not be forward-only.

    QSqlModel model;
    model.setQuery("SELECT FORENAME, SURNAME, ID FROM TEST");

The model is read-only by default, to make it read-write, it is neccessary to reimplement the setData() and isEditable() methods.

QSqlTableModel is a convenience subclass which allows manipulating a database table.

See also QSqlTableModel and QSqlQuery.


Member Function Documentation

QSqlModel::QSqlModel ( QObject * parent = 0 )

Creates an empty QSqlModel and sets the parent to parent.

QSqlModel::~QSqlModel ()   [virtual]

Destroys the object and frees any allocated resources.

void QSqlModel::clear ()   [virtual]

Clears the model and releases all aquired resources

QVariant QSqlModel::data ( const QModelIndex & item, int role = QAbstractItemModel::DisplayRole ) const   [virtual]

Returns the value for the specified item and role.

For items with type QModelIndex::HorizontalHeader, the name of the database field is returned. This can be overridden by setData().

For items with type QModelIndex::VerticalHeader, the index of the row is returned.

An invalid QVariant is returned if item is out of bounds or if an error occured.

Reimplemented from QAbstractItemModel.

See also setData() and lastError().

QModelIndex QSqlModel::dataIndex ( const QModelIndex & item ) const   [protected]

Returns the index of the value in the database result set for the given item for situations where the row and column of an item in the model does not map to the same row and column in the database result set.

Returns an invalid model index if item is out of bounds or if item does not point to a value in the result set.

bool QSqlModel::insertColumn ( int column, const QModelIndex & parent = QModelIndex(), int count = 1 )

Inserts count columns into the model at position column. The parent parameter must always be an invalid QModelIndex, since the model does not support parent-child relationships.

To populate the newly inserted columns with data, you must reimplement QSqlModel::data().

Example:

    QVariant MyModel::data(const QModelIndex &item, int role) const
    {
        if (item.column() == myNewlyInsertedColumn) {
            return "My calculated value";
        }
        return QSqlModel::data(item, role);
    }

Returns true if column is within bounds; otherwise returns false.

QSqlError QSqlModel::lastError () const

Returns information about the last error that occurred on the database.

const QSqlQuery QSqlModel::query () const

Returns the QSqlQuery that is associated with this model.

See also setQuery().

QSqlRecord QSqlModel::record () const

Returns the record containing information about the fields that are currently displayed. Returns an empty record if the model has not yet been initialized.

bool QSqlModel::removeColumn ( int column, const QModelIndex & parent = QModelIndex(), int count = 1 )

Removes count columns from the model starting from position column. The parent parameter must always be an invalid QModelIndex, since the model does not support parent-child relationships.

Note that removing columns does not affect the underlying QSqlQuery, it will just hide the columns.

Returns true if the columns were removed; otherwise returns false.

bool QSqlModel::setData ( const QModelIndex & index, int role, const QVariant & value )   [virtual]

This function is used to set the caption for the horizontal header of a column to value.

It returns true if role is QAbstractItemModel::Display and index is of type QModelIndex::HorizontalHeader and points to a valid column; otherwise returns false.

Note that this function cannot be used to modify values in the database since the model is read-only.

Reimplemented from QAbstractItemModel.

See also data().

void QSqlModel::setLastError ( const QSqlError & error )   [protected]

Protected function which allows derived classes to set the value of the last error that occurred on the database to error.

See also lastError().

void QSqlModel::setQuery ( const QSqlQuery & query )   [virtual]

Resets the model and sets the data provider to be the given query. Note that the query must be active and must not be isForwardOnly().

See also query(), QSqlQuery::isActive(), and QSqlQuery::setForwardOnly().


Copyright © 2004 Trolltech. Trademarks
Qt 4.0.0-tp1