QT——QSqlQueryModel

The QSqlQueryModel class provides a read-only data model for SQL result sets.
QSqlQueryModel为SQL结果集提供只读的数据模型。(不能对表格的内容进行修改)
QSqlQueryModel is a high-level interface for executing SQL statements and traversing the result set.
QSqlQueryModel 是一个执行SQL语句和遍历结果集的高级接口。
It is built on top of the lower-level QSqlQuery and can be used to provide data to view classes such as QTableView. 
QSqlQueryModel 是建立在底层QSqlQuery之上的,可以为诸如QTableView的视图类,提供数据的类。
For example:
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT name, salary FROM employee");
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));

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

view->show();

We set the model's query, then we set up the labels displayed in the view header.
我们设置了模块的查询,接着我们在视图header上建立labels显示。
QSqlQueryModel can also be used to access a database programmatically, without binding it to a view:
QSqlQueryModel在没有绑定视图的情况下,也可以用于以编程的方式进入数据库:
QSqlQueryModel model;
model.setQuery("SELECT * FROM employee");
int salary = model.record(4).value("salary").toInt();
The code snippet above extracts the salary field from record 4 in the result set of the query SELECT * from employee.

以上的代码片段,从“SELECT * from employee”SQL语句的结果集的第 4 个记录中返回salary的值。


Assuming that salary is column 2, we can rewrite the last line as follows:

假如salary在第 2 列,我们可以重写最后一行,如下:
int salary = model.data(model.index(4, 2)).toInt();

The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). 
model默认情况下是只读的。为了使它可读写,必须子类化它,并且重载setData() 和 flags().
Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.
使用QSqlTableModel还有一种选择,该选择提供了一个基于单数据表可读写的模块。
The querymodel example illustrates how to use QSqlQueryModel to display the result of a query. 
例子 querymodel 阐述了如何使用 QSqlQueryModel 显示 query 查询的结果集。
It also shows how to subclass QSqlQueryModel to customize the contents of the data before showing it to the user, 
and how to create a read-write model based on QSqlQueryModel.
此例同时展示了如何子类化QSqlQueryModel,在显示给使用者前自定义数据的内容,并且如何创创建一个基于QSqlQueryModel的可读写模块。
If the database doesn't return the number of selected rows in a query, the model will fetch rows incrementally. 
如果数据库没有返回query中的被选中的行数,这个模块将以渐进性的递增累加的方式获取所有行。
See fetchMore() for more information.
查看fetchMore()获取更多的信息
=================================
QSqlQueryModel::QSqlQueryModel(QObject * parent = 0)
Creates an empty QSqlQueryModel with the given parent.
使用给出的parent来创建一个空的QSqlQueryModel。
----------------------------------------------------------
QSqlQuery QSqlQueryModel::query() const
Returns the QSqlQuery associated with this model.
返回和model相关联的QSqlQuery
---------------------------------------------------------
void QSqlQueryModel::setQuery(const QSqlQuery & query)
Resets the model and sets the data provider to be the given query.
重置模型,通过给定的query,提供结果数据给模型。
---------------------------------------------------------









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值