Qt Jambi Home

com.trolltech.qt.sql
Class QSqlRelationalTableModel

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.core.QObject
              extended by com.trolltech.qt.core.QAbstractItemModel
                  extended by com.trolltech.qt.gui.QAbstractTableModel
                      extended by com.trolltech.qt.sql.QSqlQueryModel
                          extended by com.trolltech.qt.sql.QSqlTableModel
                              extended by com.trolltech.qt.sql.QSqlRelationalTableModel
All Implemented Interfaces:
QtJambiInterface

public class QSqlRelationalTableModel
extends QSqlTableModel

The QSqlRelationalTableModel class provides an editable data model for a single database table, with foreign key support.

QSqlRelationalTableModel acts like QSqlTableModel, but allows columns to be set as foreign keys into other database tables.

The screenshot on the left shows a plain QSqlTableModel in a QTableView. Foreign keys (city and country) aren't resolved to human-readable values. The screenshot on the right shows a QSqlRelationalTableModel, with foreign keys resolved into human-readable text strings.

The following code snippet shows how the QSqlRelationalTableModel was set up:

        model->setTable("employee");

        model->setRelation(2, QSqlRelation("city", "id", "name"));
        model->setRelation(3, QSqlRelation("country", "id", "name"));

The setRelation function calls establish a relationship between two tables. The first call specifies that column 2 in table employee is a foreign key that maps with field id of table city, and that the view should present the city's name field to the user. The second call does something similar with column 3.

If you use a read-write QSqlRelationalTableModel, you probably want to use QSqlRelationalDelegate on the view. Unlike the default delegate, QSqlRelationalDelegate provides a combobox for fields that are foreign keys into other tables. To use the class, simply call QAbstractItemView::setItemDelegate() on the view with an instance of QSqlRelationalDelegate:

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

The sql/relationaltablemodel example illustrates how to use QSqlRelationalTableModel in conjunction with QSqlRelationalDelegate to provide tables with foreigh key support.

Notes:

See Also:
QSqlRelation, QSqlRelationalDelegate, Table Model Example

Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.sql.QSqlTableModel
QSqlTableModel.EditStrategy
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Field Summary
 
Fields inherited from class com.trolltech.qt.sql.QSqlTableModel
beforeDelete, beforeInsert, beforeUpdate, primeInsert
 
Fields inherited from class com.trolltech.qt.core.QAbstractItemModel
dataChanged, headerDataChanged, layoutAboutToBeChanged, layoutChanged
 
Constructor Summary
QSqlRelationalTableModel()
          Equivalent to QSqlRelationalTableModel(0, QSqlDatabase()).
QSqlRelationalTableModel(QObject parent)
          Equivalent to QSqlRelationalTableModel(parent, QSqlDatabase()).
QSqlRelationalTableModel(QObject parent, QSqlDatabase db)
          Creates an empty QSqlRelationalTableModel and sets the parent to parent and the database connection to db.
 
Method Summary
 void clear()
          

Clears the model and releases any acquired resource.

 java.lang.Object data(QModelIndex item, int role)
          

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.

static QSqlRelationalTableModel fromNativePointer(QNativePointer nativePointer)
          This function returns the QSqlRelationalTableModel instance pointed to by nativePointer
protected  boolean insertRowIntoTable(QSqlRecord values)
          

Inserts the values values into the currently active database table.

protected  java.lang.String orderByClause()
          

Returns an SQL ORDER BY clause based on the currently set sort order.

 QSqlRelation relation(int column)
          Returns the relation for the column column, or an invalid relation if no relation is set.
 QSqlTableModel relationModel(int column)
          Returns a QSqlTableModel object for accessing the table for which column is a foreign key, or 0 if there is no relation for the given column.
 boolean removeColumns(int column, int count, QModelIndex parent)
          

Removes count columns from the parent model, starting at index column.

 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.

 boolean setData(QModelIndex item, java.lang.Object value, int role)
          Sets the data for the role in the item with the specified item to the value given.
 void setRelation(int column, QSqlRelation relation)
          Lets the specified column be a foreign index specified by relation.
 void setTable(java.lang.String tableName)
          

Sets the database table on which the model operates to tableName.

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.QSqlTableModel
database, deleteRowFromTable, editStrategy, fieldIndex, filter, flags, headerData, indexInQuery, insertRecord, insertRows, isDirty, primaryKey, removeRows, revert, revertAll, rowCount, setEditStrategy, setFilter, setPrimaryKey, setQuery, setRecord, setSort, sort, submit, submitAll, tableName
 
Methods inherited from class com.trolltech.qt.sql.QSqlQueryModel
canFetchMore, columnCount, fetchMore, insertColumns, lastError, query, queryChange, record, record, setHeaderData, setLastError, setQuery, setQuery
 
Methods inherited from class com.trolltech.qt.gui.QAbstractTableModel
dropMimeData, hasChildren, index, parent
 
Methods inherited from class com.trolltech.qt.core.QAbstractItemModel
beginInsertColumns, beginInsertRows, beginRemoveColumns, beginRemoveRows, buddy, changePersistentIndex, changePersistentIndexList, columnCount, createIndex, createIndex, createIndex, data, data, data, decodeData, encodeData, endInsertColumns, endInsertRows, endRemoveColumns, endRemoveRows, hasChildren, hasIndex, hasIndex, headerData, index, insertColumn, insertColumn, insertColumns, insertRow, insertRow, insertRows, itemData, match, match, match, match, mimeData, mimeTypes, persistentIndexList, removeColumn, removeColumn, removeColumns, removeRow, removeRow, removeRows, reset, rowCount, setData, setData, setData, setHeaderData, setItemData, setSupportedDragActions, setSupportedDragActions, sibling, sort, span, supportedDragActions, supportedDropActions
 
Methods inherited from class com.trolltech.qt.core.QObject
blockSignals, childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, property, removeEventFilter, setObjectName, setParent, setProperty, signalsBlocked, startTimer, thread, timerEvent
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
disconnect, disconnect, signalSender
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QSqlRelationalTableModel

public QSqlRelationalTableModel(QObject parent)

Equivalent to QSqlRelationalTableModel(parent, QSqlDatabase()).


QSqlRelationalTableModel

public QSqlRelationalTableModel()

Equivalent to QSqlRelationalTableModel(0, QSqlDatabase()).


QSqlRelationalTableModel

public QSqlRelationalTableModel(QObject parent,
                                QSqlDatabase db)

Creates an empty QSqlRelationalTableModel and sets the parent to parent and the database connection to db. If db is not valid, the default database connection will be used.

Method Detail

relation

public final QSqlRelation relation(int column)

Returns the relation for the column column, or an invalid relation if no relation is set.

See Also:
setRelation, QSqlRelation::isValid

clear

public void clear()

Clears the model and releases any acquired resource.

Overrides:
clear in class QSqlTableModel

data

public java.lang.Object data(QModelIndex item,
                             int role)

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.

Overrides:
data in class QSqlTableModel
See Also:
setData

insertRowIntoTable

protected boolean insertRowIntoTable(QSqlRecord values)

Inserts the values values into the currently active database table.

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.

Overrides:
insertRowIntoTable in class QSqlTableModel
See Also:
lastError, insertRow, insertRows

orderByClause

protected java.lang.String orderByClause()

Returns an SQL ORDER BY clause based on the currently set sort order.

Overrides:
orderByClause in class QSqlTableModel
See Also:
setSort, selectStatement

relationModel

public QSqlTableModel relationModel(int column)

Returns a QSqlTableModel object for accessing the table for which column is a foreign key, or 0 if there is no relation for the given column.

The returned object is owned by the QSqlRelationalTableModel.

See Also:
setRelation, relation

removeColumns

public boolean removeColumns(int column,
                             int count,
                             QModelIndex parent)

Removes count columns from the parent model, starting at index column.

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

Overrides:
removeColumns in class QSqlTableModel
See Also:
removeRows

revertRow

public void revertRow(int row)

Reverts all changes for the specified row.

Overrides:
revertRow in class QSqlTableModel
See Also:
revert, revertAll, submit, submitAll

select

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

Overrides:
select in class QSqlTableModel
See Also:
setTable, setFilter, selectStatement

selectStatement

protected java.lang.String selectStatement()

Returns the SQL SELECT statement used internally to populate the model. The statement includes the filter and the ORDER BY clause.

Overrides:
selectStatement in class QSqlTableModel
See Also:
filter, orderByClause

setData

public boolean setData(QModelIndex item,
                       java.lang.Object value,
                       int role)

Sets the data for the role in the item with the specified item to the value given. Depending on the edit strategy, the value might be applied to the database at once, or it may be cached in the model.

Returns true if the value could be set, or false on error (for example, if item is out of bounds).

For relational columns, value must be the index, not the display value.

Overrides:
setData in class QSqlTableModel
See Also:
editStrategy, data, submit, revertRow

setRelation

public void setRelation(int column,
                        QSqlRelation relation)

Lets the specified column be a foreign index specified by relation.

Example:

        model->setTable("employee");

        model->setRelation(2, QSqlRelation("city", "id", "name"));

The setRelation call specifies that column 2 in table employee is a foreign key that maps with field id of table city, and that the view should present the city's name field to the user.

Note: The table's primary key may not contain a relation to another table.

See Also:
relation

setTable

public void setTable(java.lang.String tableName)

Sets the database table on which the model operates to tableName. Does not select data from the table, but fetches its field information.

To populate the model with the table's data, call select.

Error information can be retrieved with lastError.

Overrides:
setTable in class QSqlTableModel
See Also:
select, setFilter, lastError

updateRowInTable

protected boolean updateRowInTable(int row,
                                   QSqlRecord values)

Updates the given row in the currently active database table with the specified values. Returns true if successful; otherwise returns false.

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

Overrides:
updateRowInTable in class QSqlTableModel
See Also:
QSqlRecord::isGenerated, setData

fromNativePointer

public static QSqlRelationalTableModel fromNativePointer(QNativePointer nativePointer)
This function returns the QSqlRelationalTableModel instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

Qt Jambi Home