QT——QSqlTableModel

QSqlTableModel::QSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
Creates an empty QSqlTableModel and sets the parent to parent and the database connection to db. 
创建一个空的QSqlTableModel,设置parent参数为传入参数parent,并且设置 数据库连接 为 db。


If db is not valid, the default database connection will be used.

如果指定的数据库db是无效的,默认 数据库连接 将会被使用。


The default edit strategy is OnRowChange.

默认的编辑策略是OnRowChange。
----------------------------------------------------------
enum QSqlTableModel::EditStrategy/*编辑策略*/
This enum type describes which strategy to choose when editing values in the database.
该枚举描述了当数据库中的被编辑时,应该选择何种编辑策略。

QSqlTableModel::OnFieldChangeAll changes to the model will be applied immediately to the database.

对于模块的所有改变都将会被立即向数据库提出申请。
QSqlTableModel::OnRowChangeChanges to a row will be applied when the user selects a different row.
QSqlTableModel::OnManualSubmitAll changes will be cached in the model until either submitAll() or revertAll() is called./* 提交修改才会生效 */
数据的所有改变都将会在模块中缓存,直到submitAll() 或者 revertAll() 之一被调用。
QSqlTableModel::OnRowChange 1Changes to a row will be applied when the user selects a different row.
当用户选择一个不同的行时,行的改变将会想数据库提出申请。

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

void QSqlTableModel::setEditStrategy(EditStrategy strategy) [virtual]

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

bool QSqlTableModel::select() [virtual slot]
Populates the model with data from the table that was set via setTable(),
using the specified filter and sort condition, and returns true if successful; otherwise returns false.
向model中填入使用表中经过指定过滤器和排序条件后的数据,该表是通过setTable()设置的表。
如果成功返回true,失败返回false

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

void QSqlTableModel::setTable(const QString & tableName) [virtual]
Sets the database table on which the model operates to tableName.
Does not select data from the table, but fetches its field information.
设置该模块要操作的数据表中的 表名。
不会进行数据的选择,但是会获取它的字段(field)的信息。

To populate the model with the table's data, call select().
为了向该模块中填充表中的数据,调用select()。
Error information can be retrieved with lastError().
错误信息可由lastError带回。

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

voidsetEditTriggers(EditTriggerstriggers)

enum QAbstractItemView::EditTrigger

This enum describes actions which will initiate item editing.

QAbstractItemView::NoEditTriggers   No editing possible.//不可编辑
QAbstractItemView::CurrentChanged   Editing start whenever current item changes.
QAbstractItemView::DoubleClicked    Editing starts when an item is double clicked.
QAbstractItemView::SelectedClicked  Editing starts when clicking on an already selected item.
QAbstractItemView::EditKeyPressed   Editing starts when the platform edit key has been pressed over an item.
QAbstractItemView::AnyKeyPressed     Editing starts when any key is pressed over an item.
QAbstractItemView::AllEditTriggers   Editing starts for all above actions.

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

void QSqlTableModel::setQuery(const QSqlQuery & query) [protected]
This function simply calls QSqlQueryModel::setQuery(query). 
这个函数只是简单的调用QSqlQueryModel::setQuery(query)。
You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.

通常情况下,不应该在QSqlTableModel类上调用它。使用setTable(), setSort(), setFilter()等函数来替代创建 query 查询。

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

QSqlTableModel和 QSqlQueryModel 区别

1、QSqlQueryModel 提供只读的数据模型,当然也可以实现修改,但是比较麻烦;QSqlTableModel 继承自 QSqlQueryModel ,提供了可实现单表读写的模型,在view上显示的同时,允许用户进行编辑操作(修改、插入、删除、查询和排序),可进行事务操作。如果只是单纯的显示数据,用 QSqlTableModel 还是很方便的。

2、QSqlQueryModel没有完全脱离SQL语句:

QSqlQueryModel* model =new QSqlQueryModel;
model->setQuery("SELECT name, salary FROM employee");

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

而 QSqlTableModel  完全脱离了SQL语句:

只需要执行select()函数就能查询整张表。但也正是因为QSqlQueryModel 完全脱离了SQL语句,使得它对数据操作(修改、插入、删除、查询和排序)有限制。

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

事务操作:

bool QSqlDatabase::transaction()
如果数据库驱动支持事务,开始一个事务操作。

bool QSqlTableModel::submitAll() [slot]

提交所有挂起的修改。

void QSqlTableModel::revertAll() [slot]

撤销所有挂起的修改。

bool QSqlDatabase::commit()

如果数据库驱动支持事务,向数据库提交一个事务。

bool QSqlDatabase::rollback()

如果数据库驱动支持事务,在数据库上回滚一个事务。

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值