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 QSqlRelationalTableModel

    java.lang.Object
      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
                                  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:

- The table must have a primary key declared.
- The table's primary key may not contain a relation to another table.
- If a relational table contains keys that refer to non-existent rows in the referenced table, the rows containing the invalid keys will not be exposed through the model. The user or the database is responsible for keeping referential integrity.
- If a relation's display column name is also used as a column name in the main table, or if it is used as display column name in more than one relation it will be aliased. The alias is is the relation's table name and display column name joined by an underscore (e.g. `tablename_columnname`). All occurances of the duplicate display column name are aliased when duplication is detected, but no aliasing is done to the column names in the main table. The aliasing doesn't affect `QSqlRelation`, so `QSqlRelation::displayColumn()` will return the original display column name, but `QSqlRecord::fieldName()` will return aliases.

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.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9`

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()` 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
`static QSqlRelationalTableModel` | `fromNativePointer(QNativePointer nativePointer)` This method returns the QSqlRelationalTableModel instance pointed to by `nativePointer`.
`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 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()`.

### fromNativePointer

    public static QSqlRelationalTableModel fromNativePointer(QNativePointer nativePointer)

  This method returns the QSqlRelationalTableModel instance pointed to by `nativePointer`.

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