QSqlRelationalTableModel Class
The QSqlRelationalTableModel class provides an editable data model for a single database table, with foreign key support. More...
#include <QSqlRelationalTableModel> Inherits: QSqlTableModel.
Public Types
Public Functions
| QAbstractItemModel(QObject * parent = 0) |
| QSqlRelationalTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase()) |
virtual | ~QAbstractItemModel() |
virtual | ~QSqlRelationalTableModel() |
virtual QModelIndex | buddy(const QModelIndex & index) const |
virtual bool | canDropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) const |
virtual bool | canFetchMore(const QModelIndex & parent) const |
virtual int | columnCount(const QModelIndex & parent = QModelIndex()) const = 0 |
virtual QVariant | data(const QModelIndex & index, int role = Qt::DisplayRole) const = 0 |
virtual bool | dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) |
virtual void | fetchMore(const QModelIndex & parent) |
virtual Qt::ItemFlags | flags(const QModelIndex & index) const |
virtual bool | hasChildren(const QModelIndex & parent = QModelIndex()) const |
bool | hasIndex(int row, int column, const QModelIndex & parent = QModelIndex()) const |
virtual QVariant | headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const |
virtual QModelIndex | index(int row, int column, const QModelIndex & parent = QModelIndex()) const = 0 |
bool | insertColumn(int column, const QModelIndex & parent = QModelIndex()) |
virtual bool | insertColumns(int column, int count, const QModelIndex & parent = QModelIndex()) |
bool | insertRow(int row, const QModelIndex & parent = QModelIndex()) |
virtual bool | insertRows(int row, int count, const QModelIndex & parent = QModelIndex()) |
virtual QMap<int, QVariant> | itemData(const QModelIndex & index) const |
virtual QModelIndexList | match(const QModelIndex & start, int role, const QVariant & value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags( Qt::MatchStartsWith | Qt::MatchWrap )) const |
virtual QMimeData * | mimeData(const QModelIndexList & indexes) const |
virtual QStringList | mimeTypes() const |
bool | moveColumn(const QModelIndex & sourceParent, int sourceColumn, const QModelIndex & destinationParent, int destinationChild) |
virtual bool | moveColumns(const QModelIndex & sourceParent, int sourceColumn, int count, const QModelIndex & destinationParent, int destinationChild) |
bool | moveRow(const QModelIndex & sourceParent, int sourceRow, const QModelIndex & destinationParent, int destinationChild) |
virtual bool | moveRows(const QModelIndex & sourceParent, int sourceRow, int count, const QModelIndex & destinationParent, int destinationChild) |
virtual QModelIndex | parent(const QModelIndex & index) const = 0 |
QSqlRelation | relation(int column) const |
virtual QSqlTableModel * | relationModel(int column) const |
bool | removeColumn(int column, const QModelIndex & parent = QModelIndex()) |
virtual bool | removeColumns(int column, int count, const QModelIndex & parent = QModelIndex()) |
bool | removeRow(int row, const QModelIndex & parent = QModelIndex()) |
virtual bool | removeRows(int row, int count, const QModelIndex & parent = QModelIndex()) |
virtual QHash<int, QByteArray> | roleNames() const |
virtual int | rowCount(const QModelIndex & parent = QModelIndex()) const = 0 |
virtual bool | setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) |
virtual bool | setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole) |
virtual bool | setItemData(const QModelIndex & index, const QMap<int, QVariant> & roles) |
void | setJoinMode(QSqlRelationalTableModel::JoinMode joinMode) |
virtual void | setRelation(int column, const QSqlRelation & relation) |
QModelIndex | sibling(int row, int column, const QModelIndex & index) const |
virtual void | sort(int column, Qt::SortOrder order = Qt::AscendingOrder) |
virtual QSize | span(const QModelIndex & index) const |
virtual Qt::DropActions | supportedDragActions() const |
virtual Qt::DropActions | supportedDropActions() const |
Reimplemented Public Functions
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) |
Public Slots
Signals
void | columnsAboutToBeInserted(const QModelIndex & parent, int start, int end) |
void | columnsAboutToBeMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationColumn) |
void | columnsAboutToBeRemoved(const QModelIndex & parent, int start, int end) |
void | columnsInserted(const QModelIndex & parent, int start, int end) |
void | columnsMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationColumn) |
void | columnsRemoved(const QModelIndex & parent, int start, int end) |
void | dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QSet<int> & roles = QSet<int> ()) |
void | headerDataChanged(Qt::Orientation orientation, int first, int last) |
void | layoutAboutToBeChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex> ()) |
void | layoutChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex> ()) |
void | modelAboutToBeReset() |
void | modelReset() |
void | rowsAboutToBeInserted(const QModelIndex & parent, int start, int end) |
void | rowsAboutToBeMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) |
void | rowsAboutToBeRemoved(const QModelIndex & parent, int start, int end) |
void | rowsInserted(const QModelIndex & parent, int start, int end) |
void | rowsMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow) |
void | rowsRemoved(const QModelIndex & parent, int start, int end) |
Protected Functions
void | beginInsertColumns(const QModelIndex & parent, int first, int last) |
void | beginInsertRows(const QModelIndex & parent, int first, int last) |
bool | beginMoveColumns(const QModelIndex & sourceParent, int sourceFirst, int sourceLast, const QModelIndex & destinationParent, int destinationChild) |
bool | beginMoveRows(const QModelIndex & sourceParent, int sourceFirst, int sourceLast, const QModelIndex & destinationParent, int destinationChild) |
void | beginRemoveColumns(const QModelIndex & parent, int first, int last) |
void | beginRemoveRows(const QModelIndex & parent, int first, int last) |
void | beginResetModel() |
void | changePersistentIndex(const QModelIndex & from, const QModelIndex & to) |
void | changePersistentIndexList(const QModelIndexList & from, const QModelIndexList & to) |
QModelIndex | createIndex(int row, int column, void * ptr = 0) const |
QModelIndex | createIndex(int row, int column, quint32 id) const |
void | endInsertColumns() |
void | endInsertRows() |
void | endMoveColumns() |
void | endMoveRows() |
void | endRemoveColumns() |
void | endRemoveRows() |
void | endResetModel() |
QModelIndexList | persistentIndexList() const |
Reimplemented Protected Functions
Additional Inherited Members
- 1 property inherited from QObject
- 11 static public members 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 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:
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 relational table, or if it is used as display column name in more than one relation it will be aliased. The alias is the relation's table name, display column name and a unique id joined by an underscore (e.g. tablename_columnname_id). QSqlRecord::fieldName() will return the aliased column name. All occurrences 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.
- The reference table name is aliased. The alias is the word "relTblAl" and the relationed column index joined by an underscore (e.g. relTblAl_2). The alias can be used to filter the table (For example, setFilter("relTblAl_2='Oslo' OR relTblAl_3='USA'")).
- When using setData() the role should always be Qt::EditRole, and when using data() the role should always be Qt::DisplayRole.
See also QSqlRelation, QSqlRelationalDelegate, and Relational Table Model Example.
Member Type Documentation
enum QSqlRelationalTableModel::JoinMode
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 also QSqlRelationalTableModel::setJoinMode.
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]
Reimplemented from QSqlQueryModel::clear().
QVariant QSqlRelationalTableModel::data(const QModelIndex & index, int role = Qt::DisplayRole) const [virtual]
Reimplemented from QAbstractItemModel::data().
See also setData().
bool QSqlRelationalTableModel::insertRowIntoTable(const QSqlRecord & values) [virtual protected]
Reimplemented from QSqlTableModel::insertRowIntoTable().
QString QSqlRelationalTableModel::orderByClause() const [virtual protected]
Reimplemented from QSqlTableModel::orderByClause().
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::removeColumns(int column, int count, const QModelIndex & parent = QModelIndex()) [virtual]
Reimplemented from QAbstractItemModel::removeColumns().
void QSqlRelationalTableModel::revertRow(int row) [virtual slot]
Reimplemented from QSqlTableModel::revertRow().
bool QSqlRelationalTableModel::select() [virtual]
Reimplemented from QSqlTableModel::select().
QString QSqlRelationalTableModel::selectStatement() const [virtual protected]
Reimplemented from QSqlTableModel::selectStatement().
bool QSqlRelationalTableModel::setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) [virtual]
Reimplemented from QAbstractItemModel::setData().
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. The index must also exist in the referenced table, otherwise the function returns false.
See also editStrategy(), data(), submit(), and revertRow().
Sets the SQL join mode to show or hide rows with NULL foreign keys. In InnerJoin mode (the default) these rows will not be showed: use the LeftJoin mode if you want to show them.
This function was introduced in Qt 4.8.
See also QSqlRelationalTableModel::JoinMode.
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.
Note: The table's primary key may not contain a relation to another table.
See also relation().
void QSqlRelationalTableModel::setTable(const QString & table) [virtual]
Reimplemented from QSqlTableModel::setTable().
bool QSqlRelationalTableModel::updateRowInTable(int row, const QSqlRecord & values) [virtual protected]
Reimplemented from QSqlTableModel::updateRowInTable().
|