
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQSqlRelationalTableModel class provides an editable data model for a single database table, with foreign key support.More...
| Header: | #include <QSqlRelationalTableModel> |
| Inherits: | QSqlTableModel |
| enum | JoinMode { InnerJoin, LeftJoin } |
| QSqlRelationalTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase()) | |
| virtual | ~QSqlRelationalTableModel() |
| QSqlRelation | relation(int column) const |
| virtual QSqlTableModel * | relationModel(int column) const |
| void | setJoinMode(QSqlRelationalTableModel::JoinMode joinMode) |
| virtual void | setRelation(int column, const QSqlRelation & relation) |
| virtual void | clear() |
| virtual QVariant | data(const QModelIndex & index, int role = Qt::DisplayRole) const |
| virtual bool | removeColumns(int column, int count, const QModelIndex & parent = QModelIndex()) |
| virtual bool | select() |
| virtual bool | setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) |
| virtual void | setTable(const QString & table) |
| virtual void | revertRow(int row) |
| virtual bool | insertRowIntoTable(const QSqlRecord & values) |
| virtual QString | orderByClause() const |
| virtual QString | selectStatement() const |
| virtual bool | updateRowInTable(int row, const QSqlRecord & values) |
TheQSqlRelationalTableModel class provides an editable data model for a single database table, with foreign key support.
QSqlRelationalTableModel acts likeQSqlTableModel, but allows columns to be set as foreign keys into other database tables.
![]() | ![]() |
The screenshot on the left shows a plainQSqlTableModel in aQTableView. Foreign keys (city andcountry) aren't resolved to human-readable values. The screenshot on the right shows aQSqlRelationalTableModel, with foreign keys resolved into human-readable text strings.
The following code snippet shows how theQSqlRelationalTableModel was set up:
model->setTable("employee"); model->setRelation(2,QSqlRelation("city","id","name")); model->setRelation(3,QSqlRelation("country","id","name"));
ThesetRelation() function calls establish a relationship between two tables. The first call specifies that column 2 in tableemployee is a foreign key that maps with fieldid of tablecity, and that the view should present thecity'sname field to the user. The second call does something similar with column 3.
If you use a read-writeQSqlRelationalTableModel, you probably want to useQSqlRelationalDelegate 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 callQAbstractItemView::setItemDelegate() on the view with an instance ofQSqlRelationalDelegate:
QTableView*view=newQTableView; view->setModel(model); view->setItemDelegate(newQSqlRelationalDelegate(view));
Thesql/relationaltablemodel example illustrates how to useQSqlRelationalTableModel in conjunction withQSqlRelationalDelegate to provide tables with foreign key support.

Notes:
See alsoQSqlRelation,QSqlRelationalDelegate, andRelational Table Model Example.
This enum specifies the type of mode to use when joining two tables.
| Constant | Value | Description |
|---|---|---|
QSqlRelationalTableModel::InnerJoin | 0 | Inner join mode, return rows when there is at least one match in both tables. |
QSqlRelationalTableModel::LeftJoin | 1 | Left join mode, returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2). |
This enum was introduced or modified in Qt 4.8.
See alsoQSqlRelationalTableModel::setJoinMode().
Creates an emptyQSqlRelationalTableModel and sets the parent toparent and the database connection todb. Ifdb is not valid, the default database connection will be used.
[virtual]QSqlRelationalTableModel::~QSqlRelationalTableModel()Destroys the object and frees any allocated resources.
[virtual]void QSqlRelationalTableModel::clear()Reimplemented fromQSqlQueryModel::clear().
[virtual]QVariant QSqlRelationalTableModel::data(constQModelIndex & index,int role = Qt::DisplayRole) constReimplemented fromQAbstractItemModel::data().
See alsosetData().
[virtual protected]bool QSqlRelationalTableModel::insertRowIntoTable(constQSqlRecord & values)Reimplemented fromQSqlTableModel::insertRowIntoTable().
[virtual protected]QString QSqlRelationalTableModel::orderByClause() constReimplemented fromQSqlTableModel::orderByClause().
Returns the relation for the columncolumn, or an invalid relation if no relation is set.
See alsosetRelation() andQSqlRelation::isValid().
[virtual]QSqlTableModel * QSqlRelationalTableModel::relationModel(int column) constReturns aQSqlTableModel object for accessing the table for whichcolumn is a foreign key, or 0 if there is no relation for the givencolumn.
The returned object is owned by theQSqlRelationalTableModel.
See alsosetRelation() andrelation().
[virtual]bool QSqlRelationalTableModel::removeColumns(int column,int count, constQModelIndex & parent = QModelIndex())Reimplemented fromQAbstractItemModel::removeColumns().
[virtual slot]void QSqlRelationalTableModel::revertRow(int row)Reimplemented fromQSqlTableModel::revertRow().
[virtual]bool QSqlRelationalTableModel::select()Reimplemented fromQSqlTableModel::select().
[virtual protected]QString QSqlRelationalTableModel::selectStatement() constReimplemented fromQSqlTableModel::selectStatement().
[virtual]bool QSqlRelationalTableModel::setData(constQModelIndex & index, constQVariant & value,int role = Qt::EditRole)Reimplemented fromQAbstractItemModel::setData().
Sets the data for therole in the item with the specifiedindex to thevalue 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, ifindex is out of bounds).
For relational columns,value must be the index, not the display value. The index must also exist in the referenced table, otherwise the function returns false.
See alsoeditStrategy(),data(),submit(), andrevertRow().
Sets the SQL join mode to the value given byjoinMode to show or hide rows with NULL foreign keys.
InInnerJoin mode (the default) these rows will not be shown; use theLeftJoin mode if you want to show them.
This function was introduced in Qt 4.8.
See alsoQSqlRelationalTableModel::JoinMode.
[virtual]void QSqlRelationalTableModel::setRelation(int column, constQSqlRelation & relation)Lets the specifiedcolumn be a foreign index specified byrelation.
Example:
model->setTable("employee"); model->setRelation(2,QSqlRelation("city","id","name"));
The setRelation() call specifies that column 2 in tableemployee is a foreign key that maps with fieldid of tablecity, and that the view should present thecity'sname field to the user.
Note: The table's primary key may not contain a relation to another table.
See alsorelation().
[virtual]void QSqlRelationalTableModel::setTable(constQString & table)Reimplemented fromQSqlTableModel::setTable().
[virtual protected]bool QSqlRelationalTableModel::updateRowInTable(int row, constQSqlRecord & values)Reimplemented fromQSqlTableModel::updateRowInTable().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.