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

# QSqlQueryModel Class Reference

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

    #include <QSqlQueryModel>

Inherits QAbstractTableModel.

Inherited by QSqlTableModel.

- List of all members, including inherited members

### Writable Properties

- 1 writable property inherited from QObject

### Public Functions

- QSqlQueryModel ( QObject * parent = 0 )
- virtual ~QSqlQueryModel ()
- virtual void clear ()
- virtual QVariant data ( const QModelIndex & item, int role = QAbstractItemModel::DisplayRole ) const
- virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = QAbstractItemModel::DisplayRole ) const
- bool insertColumn ( int column, const QModelIndex & parent = QModelIndex(), int count = 1 )
- QSqlError lastError () const
- const QSqlQuery query () const
- QSqlRecord record ( int row = -1 ) const
- bool removeColumn ( int column, const QModelIndex & parent = QModelIndex(), int count = 1 )
- virtual bool setHeaderData ( int section, Qt::Orientation orientation, int role, const QVariant & value )
- virtual void setQuery ( const QSqlQuery & query )
- void setQuery ( const QString & query, const QSqlDatabase & db = QSqlDatabase() )

- 3 public functions inherited from QAbstractTableModel
- 31 public functions inherited from QAbstractItemModel
- 24 public functions inherited from QObject

### Public Slots

- 1 public slot inherited from QObject

### Signals

- 6 signals inherited from QAbstractItemModel
- 1 signal inherited from QObject

### Static Public Members

- 4 static public members inherited from QObject

### Protected Functions

- QModelIndex dataIndex ( const QModelIndex & item ) const
- void setLastError ( const QSqlError & error )

- 1 protected function inherited from QAbstractItemModel
- 9 protected functions inherited from QObject

## Detailed Description

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

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

        QSqlQueryModel 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

### QSqlQueryModel::QSqlQueryModel ( QObject * parent = 0 )

Creates an empty QSqlQueryModel and sets the parent to parent.

### QSqlQueryModel::~QSqlQueryModel () `[virtual]`

Destroys the object and frees any allocated resources.

### void QSqlQueryModel::clear () `[virtual]`

Clears the model and releases all aquired resources

### QVariant QSqlQueryModel::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 QSqlQueryModel::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.

### QVariant QSqlQueryModel::headerData ( int section, Qt::Orientation orientation, int role = QAbstractItemModel::DisplayRole ) const `[virtual]`

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

Reimplemented from QAbstractItemModel.

### bool QSqlQueryModel::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 QSqlQueryModel::data().

Example:

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

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

### QSqlError QSqlQueryModel::lastError () const

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

### const QSqlQuery QSqlQueryModel::query () const

Returns the QSqlQuery that is associated with this model.

See also setQuery().

### QSqlRecord QSqlQueryModel::record ( int row = -1 ) const

Returns the record containing information about the fields in the database. If row points to a valid row, the record will be populated with values from that row.

If the model is not initialized, en empty record will be returned.

### bool QSqlQueryModel::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 QSqlQueryModel::setHeaderData ( int section, Qt::Orientation orientation, int role, const QVariant & value ) `[virtual]`

Sets the caption for the header with the given orientation to the specified value.

It returns true if role is `QAbstractItemModel::DisplayRole` and the section refers to a valid section; 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 QSqlQueryModel::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 QSqlQueryModel::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().

### void QSqlQueryModel::setQuery ( const QString & query, const QSqlDatabase & db = QSqlDatabase() )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Convenience method, executes the query query for the given connection db. If no database is specified, the default connection is used.