QT——QSqlRelationalTableModel 、 QSqlRelationalDelegate

The QSqlRelationalTableModel class provides an editable data model for a single database table, with foreign key support.
QSqlRelationalTableModel 类为单数据表,提供了一个可编辑的数据模型,提供外键的支持。
------------------------------------------------------
QSqlRelationalTableModel acts like QSqlTableModel, but allows columns to be set as foreign keys into other database tables.

QSqlRelationalTableModel 的行为和 QSqlTableModel 类似,但是允许指定类被设置为其他数据表的外键。



The screenshot on the left shows a plain QSqlTableModel in a QTableView.
左侧截图在QTableView中了一个普通的QSqlTableModel。
Foreign keys (city and country) aren't resolved to human-readable values. 
外键(city and country)是非可读的数值。
The screenshot on the right shows a QSqlRelationalTableModel, with foreign keys resolved into human-readable text strings.
右侧截图展示了一个QSqlRelationalTableModel,通过外键可以解析为可读文本字符串。
------------------------------------------------------
The following code snippet shows how the QSqlRelationalTableModel was set up:
下列代码片段显示 QSqlRelationalTableModel 是如何建立的:
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.
setRelation() 函数调用,在两个表之间创建了一种连接关系。
第一个函数指定 employee 表的第二列作为外键,和city表的id字段进行映射,

并且视图上应该给用户显示当前的城市名。第二个函数在第三列做了类似的操作。

------------------------------------------------------

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:
如果你使用了可读写的 QSqlRelationalTableModel,你有可能要在视图上使用QSqlRelationalDelegate
和默认的代理不同的是,QSqlRelationalDelegate 为关联到其他表的字段,提供了一个组合框(combobox)。

为了使用这个类,只需在视图上简单的调用一下QAbstractItemView::setItemDelegate(),下面是关于QSqlRelationalDelegate的实例:


QTableView *view = new QTableView;
view->setModel(model);
view->setItemDelegate(new QSqlRelationalDelegate(view));

------------------------------------------------------

void QSqlRelationalTableModel::setTable(const QString & table) [virtual]

继承自QSqlTableModel::setTable().

------------------------------------------------------

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

Lets the specified column be a foreign index specified by relation.
让指定的 column 成为一个由 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.

表的主键可能不包含和另一个表的关系。

------------------------------------------------------

bool QSqlRelationalTableModel::select() [virtual]
继承自QSqlTableModel::select().

------------------------------------------------------

void QTableView::setModel(QAbstractItemModel * model) [virtual]
Reimplemented from QAbstractItemView::setModel().

------------------------------------------------------

void QSqlTableModel::setEditStrategy(EditStrategy strategy) [virtual]
Sets the strategy for editing values in the database to strategy.
设置正被编辑数据的编辑策略。
This will revert any pending changes.
可以恢复任何挂起的修改。

------------------------------------------------------

void QAbstractItemView::setItemDelegate(QAbstractItemDelegate * delegate)

Sets the item delegate for this view and its model to delegate. 
This is useful if you want complete control over the editing and display of items.
用参数delegate为视图和模块设置项的代理。
如果你想完成项可视化的编辑这是很有用。

Any existing delegate will be removed, but not deleted. QAbstractItemView does not take ownership of delegate.

任何存在的代理将会被移除,但是不会被删除。QAbstractItemView 不会采用 delegate 的所有权。

------------------

Warning: You should not share the same instance of a delegate between views. 
警告:不应该在视图之间共享相同的代理实例。
Doing so can cause incorrect or unintuitive editing behavior since each view 
connected to a given delegate may receive the closeEditor() signal, and attempt to access, 
modify or close an editor that has already been closed.
如果这样做,可能会表现出不正确或者和不同于直观上的编辑行为,因为每个视图都被连接到给定的代理,
视图有可能已接收到关闭编辑器(
closeEditor())的信号,又尝试 access, modify 或者close 一个已经关闭了的编辑器。 
------------------------------------------------------
void QAbstractItemDelegate::closeEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint = NoHint) [signal]
This signal is emitted when the user has finished editing an item using the specified editor.
这个信号会在用户使用指定的编辑器完成项的编辑后,会被发射。

=======================================================

QSqlRelationalDelegate

The QSqlRelationalDelegate class provides a delegate that is used to display and edit data from a QSqlRelationalTableModel

QSqlRelationalDelegate类提供一个可被用于显示并编辑来自于 QSqlRelationalTableModel 模型上的代理。


  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值