Inheritance diagram of QSqlQueryModel

Inherited by: QSqlTableModel, QSqlRelationalTableModel



Virtual functions

Detailed Description

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

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

model =  QSqlQueryModel()
model.setQuery("SELECT name, salary FROM employee")
model.setHeaderData(0, Qt.Horizontal, tr("Name"))
model.setHeaderData(1, Qt.Horizontal, tr("Salary"))

view =  QTableView()



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

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

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

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:

salary = model.data(model.index(4, 2))

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

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

If the database doesn’t return the amount of selected rows in a query, the model will fetch rows incrementally. See PySide.QtSql.QSqlQueryModel.fetchMore() for more information.

class PySide.QtSql.QSqlQueryModel([parent=None])

Creates an empty PySide.QtSql.QSqlQueryModel with the given parent .


Clears the model and releases any acquired resource.

Return type:PySide.QtCore.QModelIndex

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() PySide.QtSql.QSqlQueryModel.insertColumns() PySide.QtSql.QSqlQueryModel.removeColumns()

Return type:PySide.QtSql.QSqlError

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

Return type:PySide.QtSql.QSqlQuery

Returns the PySide.QtSql.QSqlQuery associated with this model.


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

PySide.QtSql.QSqlQueryModel.query() returns the new query.

Return type:PySide.QtSql.QSqlRecord

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.

Return type:PySide.QtSql.QSqlRecord

This is an overloaded 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.


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


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().

PySide.QtSql.QSqlQueryModel.lastError() can be used to retrieve verbose information if there was an error setting the query.


Calling PySide.QtSql.QSqlQueryModel.setQuery() will remove any inserted columns.

PySide.QtSql.QSqlQueryModel.setQuery(query[, db=QSqlDatabase()])

This is an overloaded function.

Executes the query query for the given database connection db . If no database (or an invalid database) is specified, the default connection is used.

PySide.QtSql.QSqlQueryModel.lastError() can be used to retrieve verbose information if there was an error setting the query.


model = QSqlQueryModel()
model.setQuery("select * from MyTable")
if model.lastError().isValid():
    print model.lastError()