6.0.0-rc4Source

QSqlRelationalTableModel Class

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

Header#include <QSqlRelationalTableModel>
CMaketarget_link_libraries(app PRIVATE Nt::Sql)
ModuleNt SQL C++ Classes
InheritsQSqlTableModel

List of all members, including inherited members

Public Types

enumJoinMode

Public Functions

QSqlRelationalTableModel(QObject *parent = nullptr, const QSqlDatabase &db = QSqlDatabase())
~QSqlRelationalTableModel()
virtual voidsetRelation(int column, const QSqlRelation &relation)
QSqlRelationrelation(int column) const
virtual QSqlTableModel *relationModel(int column) const
voidsetJoinMode(JoinMode joinMode)

Reimplemented Public Functions

QVariantdata(const QModelIndex &item, int role = Qt::DisplayRole) const
boolsetData(const QModelIndex &item, const QVariant &value, int role = Qt::EditRole)
boolremoveColumns(int column, int count, const QModelIndex &parent = QModelIndex())
voidclear()
boolselect()
voidsetTable(const QString &tableName)

Public Slots

voidrevertRow(int row)

Reimplemented Protected Functions

Detailed Description

A QSqlRelationalTableModel is a QSqlTableModel that resolves foreign keys: a column that holds the key of a row in another table shows a value from that row, a name in the place of a number.

QSqlRelationalTableModel model;
model.setTable("employee");
model.setRelation(2, QSqlRelation("city", "id", "name"));
model.setRelation(3, QSqlRelation("country", "id", "name"));
model.select();
model.data(model.index(0, 2)); // "Oslo", not 5

Set the relations after setTable() and before select(). The rows come from a join of the table with the tables that it refers to; with the default join mode, a row whose foreign key is null or finds nothing is left out, and setJoinMode() with LeftJoin keeps it.

A column with a relation is changed by giving it a key: setData() takes a value of the index column of the relation, writes it to the table, and the item shows the display value that goes with it. relationModel() gives the rows of the other table to choose from.

The column of a relation is named after the display column in record() and in the header: name in the example above, or, if the model would then have two fields of the same name, the name of the other table, the display column and the number of the column, joined by underscores, as in country_name_3.

See also QSqlRelation, QSqlTableModel and Using Nt SQL.

Differences from Qt

The join is always written as INNER JOIN or LEFT JOIN. The display values of a relation are read when they are first needed and again after each select(). A row is found in the table by its primary key; in a table without one, by its columns that have no relation. updateRowInTable() and insertRowInTable() are not reimplemented, as the model keeps the fields of its own table for writing. QSqlRelationalDelegate is not provided.

Member Type Documentation

enum QSqlRelationalTableModel::JoinMode

Which rows the model has when a foreign key finds no row in the table that it refers to.

ConstantDescription
QSqlRelationalTableModel::InnerJoinOnly the rows whose foreign keys all find a row. This is the default.
QSqlRelationalTableModel::LeftJoinAll the rows; a foreign key that is null or finds no row is shown as null.

Member Function Documentation

[explicit] QSqlRelationalTableModel::QSqlRelationalTableModel(QObject *parent = nullptr, const QSqlDatabase &db = QSqlDatabase())

Constructs an empty model with the given parent, on the connection db, or on the default connection if db is not valid.

[override] QSqlRelationalTableModel::~QSqlRelationalTableModel()

Destroys the model, with the models of its relations.

[override] void QSqlRelationalTableModel::clear()

Reimplements QSqlTableModel::clear().

Empties the model: forgets the table, the relations, the rows and all pending changes. The join mode is kept.

[override] QVariant QSqlRelationalTableModel::data(const QModelIndex &item, int role = Qt::DisplayRole) const

Reimplements QSqlTableModel::data().

Returns the value of the item at item for role, the display role or the edit role. In a column with a relation this is the display value of the relation: the one that the row has in the database, or, for an item that was given another key with setData(), the one that goes with that key.

See also setData() and setRelation().

[override, protected] QString QSqlRelationalTableModel::orderByClause() const

Reimplements QSqlTableModel::orderByClause().

Returns the ORDER BY clause for the column and the order of setSort(). A column with a relation is sorted by its display values.

QSqlRelation QSqlRelationalTableModel::relation(int column) const

Returns the relation of the column column, or an invalid relation if it has none.

See also setRelation().

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

Returns a model of the table that the column column refers to, with its rows read, or nullptr if the column has no relation. The model belongs to this one. It is meant for choosing a key, in a combo box for example; changes that are made through it are not seen by this model until its next select().

See also setRelation().

[override] bool QSqlRelationalTableModel::removeColumns(int column, int count, const QModelIndex &parent = QModelIndex())

Reimplements QSqlTableModel::removeColumns().

Removes count columns starting with the column column from the model, with their relations, and returns true on success. parent has to be invalid.

[slot, override] void QSqlRelationalTableModel::revertRow(int row)

Reimplements QSqlTableModel::revertRow().

Drops the pending changes of the row row, as QSqlTableModel::revertRow() does.

[override] bool QSqlRelationalTableModel::select()

Reimplements QSqlTableModel::select().

Reads the rows of the table again, joined with the tables of the relations, and returns true on success. The display values of the relations are read again when they are next needed.

See also setRelation() and selectStatement().

[override, protected] QString QSqlRelationalTableModel::selectStatement() const

Reimplements QSqlTableModel::selectStatement().

Returns the SELECT statement that select() runs: the fields of the table, with the display column of the other table in the place of each foreign key, from the table joined with the tables of the relations, with the filter and the sort order. In the filter, a column of another table is written with the name that the table has in the statement, relTblAl_ followed by the number of the column of the relation, as in relTblAl_2.name = 'Oslo'.

See also select(), setJoinMode() and setFilter().

[override] bool QSqlRelationalTableModel::setData(const QModelIndex &item, const QVariant &value, int role = Qt::EditRole)

Reimplements QSqlTableModel::setData().

Sets the value of the item at item to value and returns true on success, as QSqlTableModel::setData() does; role has to be Qt::EditRole. For a column with a relation, value is a key: a value of the index column of the relation. It is refused, and false returned, if the other table has no row with that key; a null value is accepted.

See also data() and relationModel().

void QSqlRelationalTableModel::setJoinMode(JoinMode joinMode)

Sets which rows the model has when a foreign key finds no row to joinMode, for the next select().

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

Makes the column column a foreign key as relation describes. An invalid relation removes the relation of the column. Call this after setTable() and before select().

See also relation() and relationModel().

[override] void QSqlRelationalTableModel::setTable(const QString &tableName)

Reimplements QSqlTableModel::setTable().

Makes the model work on the table tableName, without relations. Set them with setRelation() afterwards.