|
|||||||||
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
com.trolltech.qt.sql.QSqlTableModel
public class QSqlTableModel
The QSqlTableModel class provides an editable data model for a single database table. QSqlTableModel is a high-level interface for reading and writing database records from a single table. It is build on top of the lower-level QSqlQuery
and can be used to provide data to view classes such as QTableView
. For example:
QSqlTableModel model = new QSqlTableModel(); model.setTable("employee"); model.setEditStrategy(QSqlTableModel.EditStrategy.OnManualSubmit); model.select(); model.removeColumn(0); // don't show the ID 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 SQL table's name and the edit strategy, then we set up the labels displayed in the view header. The edit strategy dictates when the changes done by the user in the view are actually applied to the database. The possible values are
OnFieldChange
, OnRowChange
, and OnManualSubmit
. QSqlTableModel can also be used to access a database programmatically, without binding it to a view:
QSqlTableModel model = new QSqlTableModel(); model.setTable("employee"); String name = model.record(4).value("name").toString();The code snippet above extracts the salary field from record 4 in the result set of the query SELECT * from employee.
It is possible to set filters using setFilter()
, or modify the sort order using setSort()
. At the end, you must call select()
to populate the model with data.
The sql/tablemodel example illustrates how to use QSqlTableModel as the data source for a QTableView
.
QSqlTableModel provides no direct support for foreign keys. Use the QSqlRelationalTableModel
and QSqlRelationalDelegate
if you want to resolve foreign keys.
QSqlRelationalTableModel
, QSqlQuery
, Model/View Programming, Table Model Example, and Cached Table Example.
Nested Class Summary | |
---|---|
static class |
QSqlTableModel.EditStrategy
This enum type describes which strategy to choose when editing values in the database. |
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary | |
---|---|
QSignalEmitter.Signal1 |
beforeDelete
This signal takes 1 generic argument(s). |
QSignalEmitter.Signal1 |
beforeInsert
This signal takes 1 generic argument(s). |
QSignalEmitter.Signal2 |
beforeUpdate
This signal takes 2 generic argument(s). |
QSignalEmitter.Signal2 |
primeInsert
This signal takes 2 generic argument(s). |
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 | |
---|---|
QSqlTableModel()
Creates an empty QSqlTableModel and sets the parent to parent and the database connection to db. |
|
QSqlTableModel(QObject parent)
Creates an empty QSqlTableModel and sets the parent to parent and the database connection to db. |
|
QSqlTableModel(QObject parent,
QSqlDatabase db)
Creates an empty QSqlTableModel and sets the parent to parent and the database connection to db. |
Method Summary | |
---|---|
QSqlDatabase |
database()
Returns a pointer to the used QSqlDatabase or 0 if no database was set. |
protected boolean |
deleteRowFromTable(int row)
Deletes the given row from the currently active database table. |
QSqlTableModel.EditStrategy |
editStrategy()
Returns the current edit strategy. |
int |
fieldIndex(java.lang.String fieldName)
Returns the index of the field fieldName. |
java.lang.String |
filter()
Returns the currently set filter. |
boolean |
insertRecord(int row,
QSqlRecord record)
Inserts the record after row. |
protected boolean |
insertRowIntoTable(QSqlRecord values)
Inserts the values values into the currently active database table. |
boolean |
isDirty(com.trolltech.qt.core.QModelIndex index)
Returns true if the value at the index index is dirty, otherwise false. |
protected java.lang.String |
orderByClause()
Returns an SQL ORDER BY clause based on the currently set sort order. |
QSqlIndex |
primaryKey()
Returns the primary key for the current table, or an empty QSqlIndex if the table is not set or has no primary key. |
void |
revertAll()
Reverts all pending changes. |
void |
revertRow(int row)
Reverts all changes for the specified row. |
boolean |
select()
Populates the model with data from the table that was set via setTable() , using the specified filter and sort condition, and returns true if successful; otherwise returns false. |
protected java.lang.String |
selectStatement()
Returns the SQL SELECT statement used internally to populate the model. |
void |
setEditStrategy(QSqlTableModel.EditStrategy strategy)
Sets the strategy for editing values in the database to strategy. |
void |
setFilter(java.lang.String filter)
Sets the current filter to filter. |
protected void |
setPrimaryKey(QSqlIndex key)
Protected method that allows subclasses to set the primary key to key. |
boolean |
setRecord(int row,
QSqlRecord record)
Sets the values at the specified row to the values of record. |
void |
setSort(int column,
Qt.SortOrder order)
Sets the sort order for column to order. |
void |
setTable(java.lang.String tableName)
Sets the database table on which the model operates to tableName. |
boolean |
submitAll()
Submits all pending changes and returns true on success. |
java.lang.String |
tableName()
Returns the name of the currently selected table. |
protected boolean |
updateRowInTable(int row,
QSqlRecord values)
Updates the given row in the currently active database table with the specified values. |
Methods inherited from class com.trolltech.qt.sql.QSqlQueryModel |
---|
clear, indexInQuery, lastError, query, queryChange, record, record, setLastError, setQuery, setQuery, setQuery |
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 |
Field Detail |
---|
public final QSignalEmitter.Signal1 beforeDelete
This signal takes 1 generic argument(s). We list their type and the name they go by in the description of this signal. <java.lang.Integer(named: row)>:
This signal is emitted by deleteRowFromTable()
before the row is deleted from the currently active database table.
public final QSignalEmitter.Signal1 beforeInsert
This signal takes 1 generic argument(s). We list their type and the name they go by in the description of this signal. <com.trolltech.qt.sql.QSqlRecord(named: record)>:
This signal is emitted by insertRowIntoTable()
before a new row is inserted into the currently active database table. The values that are about to be inserted are stored in record and can be modified before they will be inserted.
public final QSignalEmitter.Signal2 beforeUpdate
This signal takes 2 generic argument(s). We list their type and the name they go by in the description of this signal. <java.lang.Integer(named: row), com.trolltech.qt.sql.QSqlRecord(named: record)>:
This signal is emitted by updateRowInTable()
before the row is updated in the currently active database table with the values from record.
Note that only values that are marked as generated will be updated. The generated flag can be set with QSqlRecord::setGenerated()
and checked with QSqlRecord::isGenerated()
.
QSqlRecord::isGenerated()
.
public final QSignalEmitter.Signal2 primeInsert
This signal takes 2 generic argument(s). We list their type and the name they go by in the description of this signal. <java.lang.Integer(named: row), com.trolltech.qt.sql.QSqlRecord(named: record)>:
This signal is emitted by insertRows(), when an insertion is initiated in the given row of the currently active database table. The record parameter can be written to (since it is a reference), for example to populate some fields with default values.
Constructor Detail |
---|
public QSqlTableModel(QObject parent)
The default edit strategy is OnRowChange
.
public QSqlTableModel()
The default edit strategy is OnRowChange
.
public QSqlTableModel(QObject parent, QSqlDatabase db)
The default edit strategy is OnRowChange
.
Method Detail |
---|
public final QSqlDatabase database()
QSqlDatabase
or 0 if no database was set.
public final QSqlTableModel.EditStrategy editStrategy()
setEditStrategy()
.
public final int fieldIndex(java.lang.String fieldName)
public final java.lang.String filter()
setFilter()
, and select()
.
public final boolean insertRecord(int row, QSqlRecord record)
setRecord()
internally. Returns true if the row could be inserted, otherwise false.
public final boolean isDirty(com.trolltech.qt.core.QModelIndex index)
If index is invalid or points to a non-existing row, false is returned.
public final QSqlIndex primaryKey()
QSqlIndex
if the table is not set or has no primary key. setTable()
, setPrimaryKey()
, and QSqlDatabase::primaryIndex()
.
public final void revertAll()
revertRow()
, and submitAll()
.
protected final void setPrimaryKey(QSqlIndex key)
Normally, the primary index is set automatically whenever you call setTable()
.
primaryKey()
, and QSqlDatabase::primaryIndex()
.
public final boolean setRecord(int row, QSqlRecord record)
record()
.
public final boolean submitAll()
lastError()
. On success the model will be repopulated. Any views presenting it will lose their selections.
Note: In OnManualSubmit
mode, already submitted changes won't be cleared from the cache when submitAll()
fails. This allows transactions to be rolled back and resubmitted again without losing data.
revertAll()
, and lastError()
.
public final java.lang.String tableName()
protected boolean deleteRowFromTable(int row)
This is a low-level method that operates directly on the database and should not be called directly. Use removeRow()
or removeRows() to delete values. The model will decide depending on its edit strategy when to modify the database.
Returns true if the row was deleted; otherwise returns false.
removeRow()
, and removeRows().
protected boolean insertRowIntoTable(QSqlRecord values)
This is a low-level method that operates directly on the database and should not be called directly. Use insertRow()
and setData() to insert values. The model will decide depending on its edit strategy when to modify the database.
Returns true if the values could be inserted, otherwise false. Error information can be retrieved with lastError()
.
lastError()
, insertRow()
, and insertRows().
protected java.lang.String orderByClause()
setSort()
, and selectStatement()
.
public void revertRow(int row)
revertAll()
, submit(), and submitAll()
.
public boolean select()
setTable()
, using the specified filter and sort condition, and returns true if successful; otherwise returns false. setTable()
, setFilter()
, and selectStatement()
.
protected java.lang.String selectStatement()
filter()
, and orderByClause()
.
public void setEditStrategy(QSqlTableModel.EditStrategy strategy)
This will revert any pending changes.
editStrategy()
, and revertAll()
.
public void setFilter(java.lang.String filter)
The filter is a SQL WHERE clause without the keyword WHERE (for example, name='Josephine').
If the model is already populated with data from a database, the model re-selects it with the new filter. Otherwise, the filter will be applied the next time select()
is called.
filter()
, select()
, selectStatement()
, and orderByClause()
.
public void setSort(int column, Qt.SortOrder order)
select()
. select()
, and orderByClause()
.
public void setTable(java.lang.String tableName)
To populate the model with the table's data, call select()
.
Error information can be retrieved with lastError()
.
select()
, setFilter()
, and lastError()
.
protected boolean updateRowInTable(int row, QSqlRecord values)
This is a low-level method that operates directly on the database and should not be called directly. Use setData() to update values. The model will decide depending on its edit strategy when to modify the database.
Note that only values that have the generated-flag set are updated. The generated-flag can be set with QSqlRecord::setGenerated()
and tested with QSqlRecord::isGenerated()
.
QSqlRecord::isGenerated()
, and setData().
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |