QSqlRelationalTableModel Class Reference

[ QtSql module]

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

继承 QSqlTableModel .

类型

方法


详细描述

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 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"));
			

setRelation () function calls establish a relationship between two tables. The first call specifies that column 2 in table employee 是 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 在 view. Unlike the default delegate, QSqlRelationalDelegate 提供 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));
			

sql/relationaltablemodel example illustrates how to use QSqlRelationalTableModel in conjunction with QSqlRelationalDelegate to provide tables with foreign key support.

注意事项:


类型文档编制

QSqlRelationalTableModel.JoinMode

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

常量 描述
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).

该枚举在 Qt 4.8 引入或被修改。

另请参阅 QSqlRelationalTableModel.setJoinMode ().


方法文档编制

QSqlRelationalTableModel.__init__ ( self , QObject   parent  = None, QSqlDatabase   db  = QSqlDatabase())

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Creates an empty QSqlRelationalTableModel and sets the parent to parent and the database connection to db 。若 db is not valid, the default database connection will be used.

QSqlRelationalTableModel.clear ( self )

重实现自 QSqlQueryModel.clear ().

QVariant QSqlRelationalTableModel.data ( self , QModelIndex   index , int  role  = Qt.DisplayRole)

重实现自 QAbstractItemModel.data ().

另请参阅 setData ().

bool QSqlRelationalTableModel.insertRowIntoTable ( self , QSqlRecord   values )

重实现自 QSqlTableModel.insertRowIntoTable ().

QString QSqlRelationalTableModel.orderByClause ( self )

重实现自 QSqlTableModel.orderByClause ().

QSqlRelation QSqlRelationalTableModel.relation ( self , int  column )

Returns the relation for the column column , or an invalid relation if no relation is set.

另请参阅 setRelation () 和 QSqlRelation.isValid ().

QSqlTableModel QSqlRelationalTableModel.relationModel ( self , int  column )

返回 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 .

另请参阅 setRelation () 和 relation ().

bool QSqlRelationalTableModel.removeColumns ( self , int  column , int  count , QModelIndex   parent  = QModelIndex())

重实现自 QAbstractItemModel.removeColumns ().

QSqlRelationalTableModel.revertRow ( self , int  row )

重实现自 QSqlTableModel.revertRow ().

bool QSqlRelationalTableModel.select ( self )

重实现自 QSqlTableModel.select ().

QString QSqlRelationalTableModel.selectStatement ( self )

重实现自 QSqlTableModel.selectStatement ().

bool QSqlRelationalTableModel.setData ( self , QModelIndex   index , QVariant  value , int  role  = Qt.EditRole)

重实现自 QAbstractItemModel.setData ().

Sets the data for the role in the item with the specified index 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.

另请参阅 editStrategy (), data (), submit (),和 revertRow ().

QSqlRelationalTableModel.setJoinMode ( self , JoinMode   joinMode )

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

InnerJoin mode (the default) these rows will not be shown; use the LeftJoin mode if you want to show them.

该函数在 Qt 4.8 引入。

另请参阅 QSqlRelationalTableModel.JoinMode .

QSqlRelationalTableModel.setRelation ( self , int  column , QSqlRelation   relation )

Lets the specified column be a foreign index specified by relation .

范例:

     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.

另请参阅 relation ().

QSqlRelationalTableModel.setTable ( self , QString  tableName )

重实现自 QSqlTableModel.setTable ().

bool QSqlRelationalTableModel.updateRowInTable ( self , int  row , QSqlRecord   values )

重实现自 QSqlTableModel.updateRowInTable ().