|  | 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>

Part of the QtSql module.

Inherits QAbstractTableModel.

Inherited by QSqlTableModel.

- List of all members, including inherited members

### Public Functions

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

- 1 public function inherited from QAbstractTableModel
- 31 public functions inherited from QAbstractItemModel
- 28 public functions inherited from QObject

### Protected Functions

- QModelIndex indexInQuery ( const QModelIndex & item ) const
- virtual void queryChange ()
- void setLastError ( const QSqlError & error )

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

### Additional Inherited Members

- 1 property inherited from QObject
- 2 public slots inherited from QAbstractItemModel
- 1 public slot inherited from QObject
- 6 signals inherited from QAbstractItemModel
- 1 signal inherited from QObject
- 4 static public members inherited from QObject

## Detailed Description

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

QSqlQueryModel is a high-level interface for executing SQL statements and traversing the result set. It is built on top of the lower-level QSqlQuery and can be used to provide data to view classes such as QTableView. For example:

            QSqlQueryModel *model = new QSqlQueryModel;
            model->setQuery("SELECT name, salary FROM employee");
            model->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
            model->setHeaderData(1, Qt::Horizontal, QObject::tr("Salary"));

            QTableView *view = new QTableView;
            view->setModel(model);
            view->show();

We set the model's query, then we set up the labels displayed in the view header.

QSqlQueryModel can also be used to access a database programmatically, without binding it to a view:

            QSqlQueryModel model;
            model.setQuery("SELECT * FROM employee");
            int salary = model.record(4).value("salary").toInt();

The code snippet above extracts the `salary` field from record 4 in the result set of the query `SELECT * from employee`. Assuming that `salary` is column 2, we can rewrite the last line as follows:

            int salary = model.data(model.index(4, 2)).toInt();

The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.

The sql/querymodel example illustrates how to use QSqlQueryModel to display the result of a query. It also shows how to subclass QSqlQueryModel to customize the contents of the data before showing it to the user, and how to create a read-write model based on QSqlQueryModel.

See also QSqlTableModel, QSqlRelationalTableModel, QSqlQuery, and Model/View Programming.

## Member Function Documentation

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

Creates an empty QSqlQueryModel with the given parent.

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

Destroys the object and frees any allocated resources.

See also clear().

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

Clears the model and releases any aquired resource.

### QVariant QSqlQueryModel::data ( const QModelIndex & item, int role = DisplayRole ) const `[virtual]`

Returns the value for the specified item and role.

If item is out of bounds or if an error occurred, an invalid QVariant is returned.

Reimplemented from QAbstractItemModel.

See also lastError().

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

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

Reimplemented from QAbstractItemModel.

### QModelIndex QSqlQueryModel::indexInQuery ( const QModelIndex & item ) const `[protected]`

Returns the index of the value in the database result set for the given item in the model.

The return value is identical to item if no columns or rows have been inserted, removed, or moved around.

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

See also QSqlTableModel::indexInQuery(), insertColumns(), and removeColumns().

### bool QSqlQueryModel::insertColumns ( int column, int count, const QModelIndex & parent = QModelIndex() ) `[virtual]`

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.

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

By default, inserted columns are empty. To fill them with data, reimplement data() and handle any inserted column separately:

        QVariant MyModel::data(const QModelIndex &item, int role) const
        {
            if (item.column() == m_specialColumnNo) {
                // handle column separately
            }
            return QSqlQueryModel::data(item, role);
        }

Reimplemented from QAbstractItemModel.

See also removeColumns().

### QSqlError QSqlQueryModel::lastError () const

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

### QSqlQuery QSqlQueryModel::query () const

Returns the QSqlQuery associated with this model.

See also setQuery().

### void QSqlQueryModel::queryChange () `[virtual protected]`

This virtual function is called whenever the query changes. The default implementation does nothing.

query() returns the new query.

See also query() and setQuery().

### QSqlRecord QSqlQueryModel::record ( int row ) const

Returns the record containing information about the fields of the current query. If row is the index of a valid row, the record will be populated with values from that row.

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

See also QSqlRecord::isEmpty().

### QSqlRecord QSqlQueryModel::record () const

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

Returns an empty record containing information about the fields of the current query.

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

See also QSqlRecord::isEmpty().

### bool QSqlQueryModel::removeColumns ( int column, int count, const QModelIndex & parent = QModelIndex() ) `[virtual]`

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.

Removing columns effectively hides them. It does not affect the underlying QSqlQuery.

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

Reimplemented from QAbstractItemModel.

### bool QSqlQueryModel::setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role = EditRole ) `[virtual]`

Sets the caption for the header with the given orientation to the specified value. This is useful if the model is used to display data in a view (e.g., QTableView).

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 )

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.

Executes the query query for the given database connection db. If no database is specified, the default connection is used.

See also query() and queryChange().