Qt:Документация 4.3.2/qsqlrelationaltablemodel
Материал из Wiki.crossplatform.ru
Внимание: Актуальная версия перевода документации находится здесь |
__NOTOC__
Главная · Все классы · Основные классы · Классы по группам · Модули · Функции |
[править] QSqlRelationalTableModel Class Reference
[модуль QtSql ]
The QSqlRelationalTableModel class provides an editable data model for a single database table, with foreign key support. More...
#include <QSqlRelationalTableModel>
Inherits QSqlTableModel.
[править] Открытые функции
- QSqlRelationalTableModel ( QObject * parent = 0, QSqlDatabase db = QSqlDatabase() )
- virtual ~QSqlRelationalTableModel ()
- QSqlRelation relation ( int column ) const
- virtual QSqlTableModel * relationModel ( int column ) const
- virtual bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )
- virtual void setRelation ( int column, const QSqlRelation & relation )
- 20 открытых функций унаследованных от QSqlTableModel
- 15 открытых функций унаследованных от QSqlQueryModel
- 1 открытая функция унаследованная от QAbstractTableModel
- 34 открытых функций унаследованных от QAbstractItemModel
- 29 открытых функций унаследованных от QObject
[править] Дополнительные унаследованные члены
- 1 свойство унаследованное от QObject
- 4 открытых слотов унаследованных от QSqlTableModel
- 2 открытых слотов унаследованных от QAbstractItemModel
- 1 открытый слот унаследованный от QObject
- 4 сигнала унаследованных от QSqlTableModel
- 14 сигнала унаследованных от QAbstractItemModel
- 1 сигнал унаследованный от QObject
- 5 статических открытых члена унаследованных от QObject
- 8 защищенных функций унаследованных от QSqlTableModel
- 3 защищенных функций унаследованных от QSqlQueryModel
- 14 защищенных функций унаследованных от QAbstractItemModel
- 7 защищенных функций унаследованных от QObject
[править] Подробное описание
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 occurances 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.
See also QSqlRelation, QSqlRelationalDelegate, and Relational Table Model Example.
[править] Описание функций-членов
[править] 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.
[править] 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::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ) [virtual]
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.
Reimplemented from QAbstractItemModel.
See also editStrategy(), data(), submit(), and revertRow().
[править] 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().
Copyright © 2007 Trolltech | Trademarks | Qt 4.3.2
|