com.trolltech.qt.sql
Class QSqlRelationalTableModel

java.lang.Object
  extended by com.trolltech.qt.internal.QSignalEmitterInternal
      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:

The following code example is written in c++.

    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:

The following code example is written in c++.

    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, and Relational 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.AbstractSignal, QSignalEmitter.PrivateSignal0, QSignalEmitter.PrivateSignal1, QSignalEmitter.PrivateSignal2, QSignalEmitter.PrivateSignal3, QSignalEmitter.PrivateSignal4, QSignalEmitter.PrivateSignal5, QSignalEmitter.PrivateSignal6, QSignalEmitter.PrivateSignal7, QSignalEmitter.PrivateSignal8, QSignalEmitter.PrivateSignal9, 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.sql.QSqlTableModel
beforeDelete, beforeInsert, beforeUpdate, primeInsert
 
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
QSqlRelationalTableModel()
          Creates an empty QSqlRelationalTableModel and sets the parent to parent and the database connection to db.
QSqlRelationalTableModel(QObject parent)
          Creates an empty QSqlRelationalTableModel and sets the parent to parent and the database connection to db.
QSqlRelationalTableModel(QObject parent, QSqlDatabase db)
          Creates an empty QSqlRelationalTableModel and sets the parent to parent and the database connection to db.
 
Method Summary
 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.
 void setRelation(int column, QSqlRelation relation)
          Lets the specified column be a foreign index specified by relation.
 
Methods inherited from class com.trolltech.qt.sql.QSqlTableModel
database, deleteRowFromTable, editStrategy, fieldIndex, filter, insertRecord, insertRowIntoTable, isDirty, orderByClause, primaryKey, revertAll, revertRow, select, selectStatement, setEditStrategy, setFilter, setPrimaryKey, setRecord, setSort, setTable, submitAll, tableName, updateRowInTable
 
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.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

QSqlRelationalTableModel

public QSqlRelationalTableModel(QObject parent)
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.


QSqlRelationalTableModel

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


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(), and QSqlRelation::isValid().


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(), and relation().


setRelation

public void setRelation(int column,
                        QSqlRelation relation)
Lets the specified column be a foreign index specified by relation.

Example:

The following code example is written in c++.

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