Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.trolltech.qt.sql
Class QSqlQueryModel

    java.lang.Object
      com.trolltech.qt.internal.QSignalEmitterInternal
          com.trolltech.qt.QSignalEmitter
              com.trolltech.qt.QtJambiObject
                  com.trolltech.qt.core.QObject
                      com.trolltech.qt.core.QAbstractItemModel
                          com.trolltech.qt.gui.QAbstractTableModel
                              com.trolltech.qt.sql.QSqlQueryModel

All Implemented Interfaces:
  QtJambiInterface

Direct Known Subclasses:
  QSqlTableModel

    public class QSqlQueryModel

extends QAbstractTableModel

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.Orientation.Horizontal, tr("Name"));
            model.setHeaderData(1, Qt.Orientation.Horizontal, 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 = new QSqlQueryModel();
            model.setQuery("SELECT * FROM employee");
            int salary = ((Integer) model.record(4).value("salary")).intValue();

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 = ((Integer) model.data(model.index(4, 2))).intValue();

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

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

See also:
  `QSqlTableModel`, `QSqlRelationalTableModel`, `QSqlQuery`, Model/View Programming, and Query Model Example.

Nested Class Summary

Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
`QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9`

Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
`com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal`

Field Summary

Fields inherited from class com.trolltech.qt.core.QAbstractItemModel
`dataChanged, headerDataChanged, layoutAboutToBeChanged, layoutChanged`

Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
`currentSender`

Constructor Summary
`QSqlQueryModel()` Creates an empty `QSqlQueryModel` with the given `parent`.
`QSqlQueryModel(QObject parent)` Creates an empty `QSqlQueryModel` with the given `parent`.

Method Summary
`void` | `clear()` Clears the model and releases any acquired resource.
`protected com.trolltech.qt.core.QModelIndex` | `indexInQuery(com.trolltech.qt.core.QModelIndex item)` Returns the index of the value in the database result set for the given `item` in the model.
`QSqlError` | `lastError()` Returns information about the last error that occurred on the database.
`QSqlQuery` | `query()` Returns the `QSqlQuery` associated with this model.
`protected void` | `queryChange()` This virtual function is called whenever the query changes.
`QSqlRecord` | `record()` Returns an empty record containing information about the fields of the current query.
`QSqlRecord` | `record(int row)` Returns the record containing information about the fields of the current query.
`protected void` | `setLastError(QSqlError error)` Protected function which allows derived classes to set the value of the last error that occurred on the database to `error`.
`void` | `setQuery(QSqlQuery query)` Resets the model and sets the data provider to be the given `query`.
`void` | `setQuery(java.lang.String query)` Executes the query `query` for the given database connection `db`.
`void` | `setQuery(java.lang.String query, QSqlDatabase db)` Executes the query `query` for the given database connection `db`.

Methods inherited from class com.trolltech.qt.core.QAbstractItemModel
`beginInsertColumns, beginInsertRows, beginRemoveColumns, beginRemoveRows, buddy, canFetchMore, changePersistentIndex, changePersistentIndexList, columnCount, columnCount, createIndex, createIndex, createIndex, data, data, data, data, dropMimeData, endInsertColumns, endInsertRows, endRemoveColumns, endRemoveRows, fetchMore, flags, hasChildren, hasChildren, hasIndex, hasIndex, headerData, headerData, index, index, insertColumn, insertColumn, insertColumns, insertColumns, insertRow, insertRow, insertRows, insertRows, itemData, match, match, match, match, mimeData, mimeTypes, parent, persistentIndexList, removeColumn, removeColumn, removeColumns, removeColumns, removeRow, removeRow, removeRows, removeRows, reset, revert, rowCount, rowCount, setData, setData, setData, setData, setHeaderData, setHeaderData, setItemData, setSupportedDragActions, setSupportedDragActions, sibling, sort, sort, span, submit, supportedDragActions, supportedDropActions`

Methods inherited from class com.trolltech.qt.core.QObject
`childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, indexOfProperty, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, properties, property, removeEventFilter, setObjectName, setParent, setProperty, startTimer, timerEvent, toString, userProperty`

Methods inherited from class com.trolltech.qt.QtJambiObject
`dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr`

Methods inherited from class com.trolltech.qt.QSignalEmitter
`blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread`

Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
`__qt_signalInitialization`

Methods inherited from class java.lang.Object
`clone, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Methods inherited from interface com.trolltech.qt.QtJambiInterface
`disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership`

Constructor Detail

### QSqlQueryModel

    public QSqlQueryModel()

  Creates an empty `QSqlQueryModel` with the given `parent`.

### QSqlQueryModel

    public QSqlQueryModel(QObject parent)

  Creates an empty `QSqlQueryModel` with the given `parent`.

Method Detail

### indexInQuery

    protected com.trolltech.qt.core.QModelIndex indexInQuery(com.trolltech.qt.core.QModelIndex item)

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

### lastError

    public final QSqlError lastError()

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

See also:
  `setLastError()`, and `query()`.

### query

    public final QSqlQuery query()

  Returns the `QSqlQuery` associated with this model.

See also:
  `setQuery()`.

### record

    public final QSqlRecord record()

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

### record

    public final QSqlRecord record(int row)

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

### setLastError

    protected final void setLastError(QSqlError error)

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

See also:
  `lastError()`.

### setQuery

    public void setQuery(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().

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

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

### setQuery

    public final void setQuery(java.lang.String query)

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

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

  Example:

          QSqlQueryModel model = new QSqlQueryModel();
          model.setQuery("select * from MyTable");
          if (model.lastError().isValid())
              System.out.println(model.lastError());

See also:
  `query()`, `queryChange()`, and `lastError()`.

### setQuery

    public final void setQuery(java.lang.String query,
                               QSqlDatabase db)

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

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

  Example:

          QSqlQueryModel model = new QSqlQueryModel();
          model.setQuery("select * from MyTable");
          if (model.lastError().isValid())
              System.out.println(model.lastError());

See also:
  `query()`, `queryChange()`, and `lastError()`.

### clear

    public void clear()

  Clears the model and releases any acquired resource.

### queryChange

    protected void queryChange()

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

  `query()` returns the new query.

See also:
  `query()`, and `setQuery()`.

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD