Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QSqlRelationalTableModel Class Reference

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

    #include <QSqlRelationalTableModel>

Inherits QSqlTableModel.

- List of all members, including inherited members

### Writable Properties

- 1 writable property inherited from QObject

### Public Functions

- QSqlRelationalTableModel ( QObject * parent = 0, QSqlDatabase db = QSqlDatabase() )
- virtual ~QSqlRelationalTableModel ()
- QSqlRelation relation ( int column ) const
- virtual QSqlTableModel * relationModel ( int column ) const
- virtual bool setData ( const QModelIndex & index, const QVariant & value, int role = EditRole )
- virtual void setRelation ( int column, const QSqlRelation & relation )

- 20 public functions inherited from QSqlTableModel
- 12 public functions inherited from QSqlQueryModel
- 1 public function inherited from QAbstractTableModel
- 31 public functions inherited from QAbstractItemModel
- 26 public functions inherited from QObject

### Public Slots

- 4 public slots inherited from QSqlTableModel
- 2 public slots inherited from QAbstractItemModel
- 1 public slot inherited from QObject

### Signals

- 4 signals inherited from QSqlTableModel
- 6 signals inherited from QAbstractItemModel
- 1 signal inherited from QObject

### Static Public Members

- 4 static public members inherited from QObject

### Protected Functions

- 8 protected functions inherited from QSqlTableModel
- 3 protected functions inherited from QSqlQueryModel
- 1 protected function inherited from QAbstractItemModel
- 7 protected functions inherited from QObject

## Detailed Description

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

See also QSqlRelation and QSqlRelationalDelegate.

## Member Function Documentation

### QSqlRelationalTableModel::QSqlRelationalTableModel ( QObject * parent = 0, QSqlDatabase db = QSqlDatabase() )

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::~QSqlRelationalTableModel () `[virtual]`

Destroys the object and frees any allocated resources.

### QSqlRelation QSqlRelationalTableModel::relation ( int column ) const

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

See also setRelation() and QSqlRelation::isValid().

### QSqlTableModel * QSqlRelationalTableModel::relationModel ( int column ) const `[virtual]`

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

### bool QSqlRelationalTableModel::setData ( const QModelIndex & index, const QVariant & value, int role = EditRole ) `[virtual]`

Sets the data for the role in the item with the specified index 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 index is out of bounds).

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

Reimplemented from QAbstractItemModel.

See also editStrategy(), data(), submitChanges(), and revertRow().

### void QSqlRelationalTableModel::setRelation ( int column, const QSqlRelation & relation ) `[virtual]`

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.

See also relation().