QgsRelationManager class

This class manages a set of relations between layers.

Public static functions

static auto discoverRelations(const QList<QgsRelation>& existingRelations, const QList<QgsVectorLayer*>& layers) -> QList<QgsRelation>
Discover all the relations available from the current layers.

Constructors, destructors, conversion operators

QgsRelationManager(QgsProject* project = nullptr) explicit
Constructor for QgsRelationManager.

Public functions

void addRelation(const QgsRelation& relation)
Add a relation.
void clear()
Remove any relation managed by this class.
auto referencedRelations(QgsVectorLayer* layer = nullptr) const -> QList<QgsRelation>
Gets all relations where this layer is the referenced part (i.e.
auto referencingRelations(const QgsVectorLayer* layer = nullptr, int fieldIdx = -2) const -> QList<QgsRelation>
Gets all relations where the specified layer (and field) is the referencing part (i.e.
auto relation(const QString& id) const -> Q_INVOKABLE QgsRelation
Gets access to a relation by its id.
auto relations() const -> QMap<QString, QgsRelation>
Gets access to the relations managed by this class.
auto relationsByName(const QString& name) const -> QList<QgsRelation>
Returns a list of relations with matching names.
void removeRelation(const QString& id)
Remove a relation.
void removeRelation(const QgsRelation& relation)
Remove a relation.
void setRelations(const QList<QgsRelation>& relations)
Will set the specified relations and remove any relation currently set.

Signals

void changed()
Emitted when relations are added or removed to the manager.
void relationsLoaded()
This signal is emitted when the relations were loaded after reading a project.

Public slots

void updateRelationsStatus()
Updates relations status.

Function documentation

static QList<QgsRelation> QgsRelationManager::discoverRelations(const QList<QgsRelation>& existingRelations, const QList<QgsVectorLayer*>& layers)

Discover all the relations available from the current layers.

Parameters
existingRelations the existing relations to filter them out
layers the current layers
Returns the list of discovered relations

QgsRelationManager::QgsRelationManager(QgsProject* project = nullptr) explicit

Constructor for QgsRelationManager.

Parameters
project associated project (used to notify project of changes)

void QgsRelationManager::addRelation(const QgsRelation& relation)

Add a relation.

Parameters
relation The relation to add.

Invalid relations are added only if both referencing layer and referenced layer exist.

QList<QgsRelation> QgsRelationManager::referencedRelations(QgsVectorLayer* layer = nullptr) const

Gets all relations where this layer is the referenced part (i.e.

Parameters
layer The layer which should be searched for.
Returns A list of relations where the specified layer is the referenced part.

the parent table with the primary key being referenced from another layer).

QList<QgsRelation> QgsRelationManager::referencingRelations(const QgsVectorLayer* layer = nullptr, int fieldIdx = -2) const

Gets all relations where the specified layer (and field) is the referencing part (i.e.

Parameters
layer The layer which should be searched for.
fieldIdx The field which should be part of the foreign key. If not set will return all relations.
Returns A list of relations matching the given layer and fieldIdx.

the child table with the foreign key).

Q_INVOKABLE QgsRelation QgsRelationManager::relation(const QString& id) const

Gets access to a relation by its id.

Parameters
id The id to search for
Returns A relation. Invalid if not found.

QMap<QString, QgsRelation> QgsRelationManager::relations() const

Gets access to the relations managed by this class.

Returns A QMap where the key is the relation id, the value the relation object.

QList<QgsRelation> QgsRelationManager::relationsByName(const QString& name) const

Returns a list of relations with matching names.

Parameters
name relation name to search for. Searching is case insensitive.
Returns a list of matching relations

void QgsRelationManager::removeRelation(const QString& id)

Remove a relation.

Parameters
id The id of the relation to remove.

void QgsRelationManager::removeRelation(const QgsRelation& relation)

Remove a relation.

Parameters
relation The relation to remove.

void QgsRelationManager::setRelations(const QList<QgsRelation>& relations)

Will set the specified relations and remove any relation currently set.

Parameters
relations A list of relations to set.

void QgsRelationManager::changed() signal

Emitted when relations are added or removed to the manager.