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. In addition, columns can be set as foreign keys into another table. 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 ()
- virtual void clear ()
- virtual QVariant data ( const QModelIndex & index, int role = QAbstractItemModel::DisplayRole ) const
- QSqlRelation relation ( int column ) const
- virtual QSqlTableModel * relationModel ( int column )
- virtual bool select ()
- virtual bool setData ( const QModelIndex & index, int role, const QVariant & value )
- virtual void setRelation ( int column, const QSqlRelation & relation )

- 20 public functions inherited from QSqlTableModel
- 11 public functions inherited from QSqlQueryModel
- 3 public functions inherited from QAbstractTableModel
- 31 public functions inherited from QAbstractItemModel
- 24 public functions inherited from QObject

### Public Slots

- virtual void revertRow ( int row )

- 3 public slots inherited from QSqlTableModel
- 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

- virtual QString selectStatement () const

- 7 protected functions inherited from QSqlTableModel
- 2 protected functions inherited from QSqlQueryModel
- 1 protected function inherited from QAbstractItemModel
- 9 protected functions inherited from QObject

## Detailed Description

The QSqlRelationalTableModel class provides an editable data model for a single database table. In addition, columns can be set as foreign keys into another table.

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

The following example assumes that there are two tables called `people` and `titles`. The `titles` table has two columns called `id` and `title`. The `people` table's 4th column is a foreign index into the `titles` table:

        QSqlTableModel model;
        model.setTable("people");
        model.setRelation(3, QSqlRelation("titles", "id", "title"));
        model.select();

Instead of displaying the id of the title, the model will display the value stored in the `title` column of the `titles` table.

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

### void QSqlRelationalTableModel::clear () `[virtual]`

### QVariant QSqlRelationalTableModel::data ( const QModelIndex & index, int role = QAbstractItemModel::DisplayRole ) const `[virtual]`

Returns the data stored under the given role for the item referred to by index. Returns an invalid variant if index is out of bounds.

Reimplemented from QAbstractItemModel.

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

Returns the relation for the column column.

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

### void QSqlRelationalTableModel::revertRow ( int row ) `[virtual slot]`

### bool QSqlRelationalTableModel::select () `[virtual]`

### QString QSqlRelationalTableModel::selectStatement () const `[virtual protected]`

### bool QSqlRelationalTableModel::setData ( const QModelIndex & index, int role, const QVariant & value ) `[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 has to 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.