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

QSqlTableModel Class Reference

The QSqlTableModel class provides an editable data model for a single database table. More...

#include <QSqlTableModel>

Inherits QSqlQueryModel.

Inherited by QSqlRelationalTableModel.

Public Types

Writable Properties

Public Functions

Public Slots

Signals

Static Public Members

Protected Functions


Detailed Description

The QSqlTableModel class provides an editable data model for a single database table.

QSqlTableModel is a data model that provides data from a database table. By default, the model can be edited.

    QSqlTableModel model;
    model.setTable("MYTABLE");
    model.select();

Before the table can be used, a table name has to be set. After that, it is possible to set filters with setFilter() or modify the sort order with setSort().

After all the desired options have been set, select() has to be called to populate the model with data.


Member Type Documentation

enum QSqlTableModel::EditStrategy

This enum type describes which strategy to choose when editing values in the database.

QSqlTableModel::OnFieldChangeAll changes to the model will be applied immediately to the database.
QSqlTableModel::OnRowChangeChanges will be applied when the current row changes.
QSqlTableModel::OnManualSubmitAll changes will be cached in the model until either submitChanges() or revertAll() is invoked.

Member Function Documentation

QSqlTableModel::QSqlTableModel ( QObject * parent = 0, QSqlDatabase db = QSqlDatabase() )

Creates an empty QSqlTableModel and sets the parent to parent and the database connection to db. If db is not valid, the default database connection will be used.

QSqlTableModel::~QSqlTableModel ()   [virtual]

Destroys the object and frees any allocated resources.

void QSqlTableModel::beforeDelete ( int row )   [signal]

This signal is emitted before the row row is deleted.

void QSqlTableModel::beforeInsert ( QSqlRecord & record )   [signal]

This signal is emitted before a new row is inserted. The values that are about to be inserted are stored in record and can be modified before they will be inserted.

void QSqlTableModel::beforeUpdate ( int row, QSqlRecord & record )   [signal]

This signal is emitted before the row row is updated with the values from record.

Note that only values that are marked as generated will be updated. The generated flag can be set with QSqlRecord::setGenerated() and checked with QSqlRecord::isGenerated().

See also QSqlRecord::isGenerated().

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

Returns the data for the item at position idx for the role role. Returns an invalid variant if idx is out of bounds.

Reimplemented from QAbstractItemModel.

QModelIndex QSqlTableModel::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.

QSqlDatabase QSqlTableModel::database () const

Returns a pointer to the used QSqlDatabase or 0 if no database was set.

bool QSqlTableModel::deleteRow ( int row )   [virtual protected]

Deletes the given row in the model. Returns true if the row was deleted; otherwise returns false.

EditStrategy QSqlTableModel::editStrategy () const

Returns the current edit strategy.

See also EditStrategy and setEditStrategy().

int QSqlTableModel::fieldIndex ( const QString & fieldName ) const

Returns the index of the field fieldName.

QString QSqlTableModel::filter () const

Returns the currently set filter.

See also setFilter() and select().

QVariant QSqlTableModel::headerData ( int section, Qt::Orientation orientation, int role )

Returns the header data for the given role in the section of the header with the specified orientation.

bool QSqlTableModel::insertRow ( const QSqlRecord & values )   [virtual protected]

Inserts the values values into the database table. Returns true if the values could be inserted, otherwise false. Error information can be retrieved with lastError().

See also lastError().

bool QSqlTableModel::insertRows ( int row, const QModelIndex & parent = QModelIndex::Null, int count = 1 )   [virtual]

// TODO document manual updates Inserts an empty row at position row. Note that parent has to be invalid, since this model does not support parent-child relations.

Note that only one row can be inserted at a time, so count should always be 1.

The primeInsert() signal will be emitted, so the newly inserted values can be initialized.

Returns false if the parameters are out of bounds, otherwise true.

Reimplemented from QAbstractItemModel.

See also primeInsert().

bool QSqlTableModel::isDirty ( const QModelIndex & index ) const

Returns true if the value at the index index is dirty, otherwise false. Dirty values are values that were modified in the model but not yet written into the database.

If index is invalid or points to a non-existing row, false is returned.

QString QSqlTableModel::orderByStatement () const   [virtual protected]

Returns a SQL 'ORDER BY' statement based on the currently set sort order.

See also sort().

QSqlIndex QSqlTableModel::primaryKey () const

Returns the primary key for the current table, or an empty QSqlIndex if the table is not set or has no primary key.

void QSqlTableModel::primeInsert ( int row, QSqlRecord & record )   [signal]

This signal is emitted when an insertion is initiated in the given row. The record parameter can be written to (since it is a reference), for example to populate some fields with default values.

bool QSqlTableModel::removeColumn ( int column, const QModelIndex & parent = QModelIndex::Null )

Removes the given column from the parent model.

See also removeRows() and TODO.

bool QSqlTableModel::removeRows ( int row, const QModelIndex & parent = QModelIndex::Null, int count = 1 )   [virtual]

Removes count rows starting at row. Since this model does not support hierarchical structures, parent must be an invalid model index.

Emits the beforeDelete() signal before a row is deleted.

Returns true if all rows could be removed, otherwise false. Detailed error information can be retrieved with lastError().

Reimplemented from QAbstractItemModel.

See also removeColumns().

void QSqlTableModel::revertAll ()   [slot]

Revert all pending changes.

void QSqlTableModel::revertRow ( int row )   [virtual slot]

Reverts all changes for the current row.

bool QSqlTableModel::select ()   [virtual]

Populates the model with data from the table that was set via setTable(), using the specified filter and sort condition.

See also setTable(), setFilter(), and sort().

QString QSqlTableModel::selectStatement () const   [virtual protected]

Returns a SQL SELECT statement.

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

Sets the data for the item index for the role role to value. Depending on the edit strategy, the value might be applied to the database at once or cached in the model.

Returns true if the value could be set or false on error, for example if index is out of bounds.

Reimplemented from QAbstractItemModel.

See also editStrategy(), data(), submitChanges(), and revertRow().

void QSqlTableModel::setEditStrategy ( EditStrategy strategy )   [virtual]

Sets the strategy for editing values in the database to strategy. This will revert all pending changes.

See also EditStrategy and editStrategy().

void QSqlTableModel::setFilter ( const QString & filter )   [virtual]

Sets the current filter to filter. Note that no new records are selected. To select new records, use select(). The filter will apply to any subsequent select() calls.

The filter is a SQL WHERE clause without the keyword 'WHERE', e.g. name='Harry' which will be processed by the DBMS.

See also filter() and select().

void QSqlTableModel::setPrimaryKey ( const QSqlIndex & key )   [protected]

Protected method to allow subclasses to set the primary key to key.

bool QSqlTableModel::setRecord ( int row, const QSqlRecord & record )

Sets the values at the specified row to the values of record. Returns false if not all the values could be set, otherwise true.

See also record().

void QSqlTableModel::setSort ( int column, Qt::SortOrder order )   [virtual]

Sets the sort oder for column to order. This does not affect the current data, to refresh the data using the new sort order, call select().

See also select(), sort(), isSortable(), and orderByStatement().

void QSqlTableModel::setTable ( const QString & tableName )

Sets the table to tableName. Does not select data from the table, but fetches its field information.

To populate the model with the table's data, call select().

See also select(), setFilter(), and sort().

void QSqlTableModel::sort ( int column, Qt::SortOrder order )

Sorts the data by column with the sort order order. This will immediately select data, use setSort() to set a sort order without populating the model with data.

See also setSort(), isSortable(), select(), and orderByStatement().

bool QSqlTableModel::submitChanges ()   [virtual slot]

Submits all pending changes and returns true on success.

See also lastError().

QString QSqlTableModel::tableName () const

Returns the name of the currently selected table.

bool QSqlTableModel::updateRow ( int row, const QSqlRecord & values )   [virtual protected]

Updates the row row in the currently active database table with the values from values.

Note that only values that have the generated-flag set are updated. The generated-flag can be set with QSqlRecord::setGenerated() and tested with QSqlRecord::isGenerated().

See also QSqlRecord::isGenerated().


Copyright © 2004 Trolltech. Trademarks
Qt 4.0.0-tp2