|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.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
public class QSqlQueryModel
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.
QSqlTableModel
, QSqlRelationalTableModel
, QSqlQuery
, Model/View Programming, and Query Model Example.
Nested Class Summary |
---|
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 |
---|
columnsAboutToBeInserted, columnsAboutToBeRemoved, columnsInserted, columnsRemoved, dataChanged, headerDataChanged, layoutAboutToBeChanged, layoutChanged, modelAboutToBeReset, modelReset, rowsAboutToBeInserted, rowsAboutToBeRemoved, rowsInserted, rowsRemoved |
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()
This is an overloaded member function, provided for convenience. |
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)
This is an overloaded member function, provided for convenience. |
void |
setQuery(java.lang.String query,
QSqlDatabase db)
This is an overloaded member function, provided for convenience. |
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 |
---|
public QSqlQueryModel()
public QSqlQueryModel(QObject parent)
Method Detail |
---|
protected com.trolltech.qt.core.QModelIndex indexInQuery(com.trolltech.qt.core.QModelIndex item)
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.
public final QSqlError lastError()
setLastError()
, and query()
.
public final QSqlQuery query()
QSqlQuery
associated with this model. setQuery()
.
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.
QSqlRecord::isEmpty()
.
public final QSqlRecord record(int row)
If the model is not initialized, an empty record will be returned.
QSqlRecord::isEmpty()
.
protected final void setLastError(QSqlError error)
lastError()
.
public void setQuery(QSqlQuery query)
lastError()
can be used to retrieve verbose information if there was an error setting the query.
query()
, QSqlQuery::isActive()
, QSqlQuery::setForwardOnly()
, and lastError()
.
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());
query()
, queryChange()
, and lastError()
.
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());
query()
, queryChange()
, and lastError()
.
public void clear()
protected void queryChange()
query()
returns the new query.
query()
, and setQuery()
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |