Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QSqlRelationalTableModel Class Reference

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

 #include <QSqlRelationalTableModel>

Inherits: QSqlTableModel.

Public Types

enum JoinMode { InnerJoin, LeftJoin }

Public Functions

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 )

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

virtual void revertRow ( int row )

Reimplemented Protected Functions

virtual bool insertRowIntoTable ( const QSqlRecord & values )
virtual QString orderByClause () const
virtual QString selectStatement () const
virtual bool updateRowInTable ( int row, const QSqlRecord & values )

Additional Inherited Members

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 main table, or if it is used as display column name in more than one relation it will be aliased. The alias is is the relation's table name and display column name joined by an underscore (e.g. tablename_columnname). 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, but QSqlRecord::fieldName() will return aliases.
  • 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

This enum specifies the type of mode to use when joining two tables.

ConstantValueDescription
QSqlRelationalTableModel::InnerJoin0Inner join mode, return rows when there is at least one match in both tables.
QSqlRelationalTableModel::LeftJoin1Left 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().

void QSqlRelationalTableModel::setJoinMode ( QSqlRelationalTableModel::JoinMode joinMode )

Sets the SQL join mode to the value given by joinMode to show or hide rows with NULL foreign keys.

In InnerJoin mode (the default) these rows will not be shown; 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().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 85
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 23
  5. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
Page suivante

Le blog Digia au hasard

Logo

Créer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut Cuvelier

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.8
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web