QT 015 【数据库】 QSqlTableModel Class
Ref: http://doc.qt.io/qt-5/qsqltablemodel.html
1 小结
2 翻译:
QSqlTableModel Class
The QSqlTableModel class provides an editable data model for a single database table. More...
Header: | #include <QSqlTableModel> |
qmake: | QT += sql |
Inherits: | QSqlQueryModel |
Inherited By: |
QSqlRelationalTableModel |
- List of all members, including inherited members
Public Types
enum | EditStrategy { OnFieldChange, OnRowChange, OnManualSubmit } |
Public Functions
QSqlTableModel(QObject *parent = Q_NULLPTR, QSqlDatabase db = QSqlDatabase()) | |
virtual | ~QSqlTableModel() |
QSqlDatabase | database() const |
EditStrategy | editStrategy() const |
int | fieldIndex(const QString &fieldName) const |
QString | filter() const |
bool | insertRecord(int row, const QSqlRecord &record) |
bool | isDirty(const QModelIndex &index) const |
bool | isDirty() const |
QSqlIndex | primaryKey() const |
QSqlRecord | record() const |
QSqlRecord | record(int row) const |
virtual void | revertRow(int row) |
virtual void | setEditStrategy(EditStrategy strategy) |
virtual void | setFilter(const QString &filter) |
bool | setRecord(int row, const QSqlRecord &values) |
virtual void | setSort(int column, Qt::SortOrder order) |
virtual void | setTable(const QString &tableName) |
QString | tableName() const |
Reimplemented Public Functions
virtual void | clear() |
virtual QVariant | data(const QModelIndex &index, int role = Qt::DisplayRole) const |
virtual Qt::ItemFlags | flags(const QModelIndex &index) const |
virtual QVariant | headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const |
virtual bool | insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) |
virtual bool | removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) |
virtual bool | removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) |
virtual int | rowCount(const QModelIndex &parent = QModelIndex()) const |
virtual bool | setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) |
virtual void | sort(int column, Qt::SortOrder order) |
- 16 public functions inherited from QSqlQueryModel
- 4 public functions inherited from QAbstractTableModel
- 39 public functions inherited from QAbstractItemModel
- 32 public functions inherited from QObject
Public Slots
virtual void | revert() |
void | revertAll() |
virtual bool | select() |
virtual bool | selectRow(int row) |
virtual bool | submit() |
bool | submitAll() |
- 2 public slots inherited from QAbstractItemModel
- 1 public slot inherited from QObject
Signals
void | beforeDelete(int row) |
void | beforeInsert(QSqlRecord &record) |
void | beforeUpdate(int row, QSqlRecord &record) |
void | primeInsert(int row, QSqlRecord &record) |
- 18 signals inherited from QAbstractItemModel
- 2 signals inherited from QObject
Protected Functions
virtual bool | deleteRowFromTable(int row) |
virtual bool | insertRowIntoTable(const QSqlRecord &values) |
virtual QString | orderByClause() const |
QSqlRecord | primaryValues(int row) const |
virtual QString | selectStatement() const |
void | setPrimaryKey(const QSqlIndex &key) |
void | setQuery(const QSqlQuery &query) |
virtual bool | updateRowInTable(int row, const QSqlRecord &values) |
Reimplemented Protected Functions
virtual QModelIndex | indexInQuery(const QModelIndex &item) const |
- 3 protected functions inherited from QSqlQueryModel
- 19 protected functions inherited from QAbstractItemModel
- 9 protected functions inherited from QObject
Additional Inherited Members
- 1 property inherited from QObject
- 11 static public members inherited from QObject
- 1 protected slot inherited from QAbstractItemModel
Detailed Description
The QSqlTableModel class provides an editable data model for a single database table.
QSqlTableModel is a high-level interface for reading and writing database records from a single table. It is built on top of the lower-level QSqlQuery and can be used to provide data to view classes such as QTableView. For example:
QSqlTableModel *model = new QSqlTableModel(parentObject, database);model->setTable("employee");model->setEditStrategy(QSqlTableModel::OnManualSubmit);model->select();model->setHeaderData(0, Qt::Horizontal, tr("Name"));model->setHeaderData(1, Qt::Horizontal, tr("Salary"));QTableView *view = new QTableView;view->setModel(model);view->hideColumn(0); // don't show the IDview->show();
We set the SQL table's name and the edit strategy, then we set up the labels displayed in the view header. The edit strategy dictates when the changes done by the user in the view are actually applied to the database. The possible values are OnFieldChange, OnRowChange, andOnManualSubmit.
QSqlTableModel can also be used to access a database programmatically, without binding it to a view:
QSqlTableModel model;model.setTable("employee");model.select();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 querySELECT * from employee
.
It is possible to set filters using setFilter(), or modify the sort order using setSort(). At the end, you must call select() to populate the model with data.
The tablemodel example illustrates how to use QSqlTableModel as the data source for aQTableView.
QSqlTableModel provides no direct support for foreign keys. Use the QSqlRelationalTableModeland QSqlRelationalDelegate if you want to resolve foreign keys.
See also QSqlRelationalTableModel, QSqlQuery, Model/View Programming, Table Model Example, and Cached Table Example.
Member Type Documentation
enum QSqlTableModel::EditStrategy
This enum type describes which strategy to choose when editing values in the database.
Constant | Value | Description |
---|---|---|
QSqlTableModel::OnFieldChange
|
0
|
All changes to the model will be applied immediately to the database. |
QSqlTableModel::OnRowChange
|
1
|
Changes to a row will be applied when the user selects a different row. |
QSqlTableModel::OnManualSubmit
|
2
|
All changes will be cached in the model until either submitAll() or revertAll() is called. |
Note: To prevent inserting only partly initialized rows into the database, OnFieldChange
will behave like OnRowChange
for newly inserted rows.
See also setEditStrategy().
Member Function Documentation
QSqlTableModel::QSqlTableModel(QObject *parent = Q_NULLPTR,QSqlDatabase db = QSqlDatabase())
The default edit strategy is OnRowChange.
[virtual]
QSqlTableModel::~QSqlTableModel()
Destroys the object and frees any allocated resources.
[signal]
void QSqlTableModel::beforeDelete(int row)
This signal is emitted by deleteRowFromTable() before the row is deleted from the currently active database table.
[signal]
void QSqlTableModel::beforeInsert(QSqlRecord &record)
[signal]
void QSqlTableModel::beforeUpdate(int row, QSqlRecord &record)
See also QSqlRecord::isGenerated().
[virtual]
void QSqlTableModel::clear()
Reimplemented from QSqlQueryModel::clear().
[virtual]
QVariant QSqlTableModel::data(const QModelIndex &index, introle = Qt::DisplayRole) const
Reimplemented from QAbstractItemModel::data().
QSqlDatabase QSqlTableModel::database() const
Returns the model's database connection.
[virtual protected]
bool QSqlTableModel::deleteRowFromTable(int row)
Deletes the given row from the currently active database table.
This is a low-level method that operates directly on the database and should not be called directly. Use removeRow() or removeRows() to delete values. The model will decide depending on its edit strategy when to modify the database.
Returns true
if the row was deleted; otherwise returns false
.
See also removeRow() and removeRows().
EditStrategy QSqlTableModel::editStrategy() const
Returns the current edit strategy.
int QSqlTableModel::fieldIndex(const QString &fieldName) const
Returns the index of the field fieldName, or -1 if no corresponding field exists in the model.
QString QSqlTableModel::filter() const
Returns the currently set filter.
See also setFilter() and select().
[virtual]
Qt::ItemFlags QSqlTableModel::flags(const QModelIndex &index) const
Reimplemented from QAbstractItemModel::flags().
[virtual]
QVariant QSqlTableModel::headerData(int section,Qt::Orientation orientation, int role = Qt::DisplayRole) const
Reimplemented from QAbstractItemModel::headerData().
[virtual protected]
QModelIndex QSqlTableModel::indexInQuery(constQModelIndex &item) const
Reimplemented from QSqlQueryModel::indexInQuery().
Returns the index of the value in the database result set for the given item in the model.
See also QSqlQueryModel::indexInQuery().
bool QSqlTableModel::insertRecord(int row, const QSqlRecord &record)
Returns true
if the record could be inserted, otherwise false.
See also insertRows(), removeRows(), and setRecord().
[virtual protected]
bool QSqlTableModel::insertRowIntoTable(constQSqlRecord &values)
Inserts the values values into the currently active database table.
See also lastError(), insertRow(), and insertRows().
[virtual]
bool QSqlTableModel::insertRows(int row, int count, constQModelIndex &parent = QModelIndex())
Reimplemented from QAbstractItemModel::insertRows().
Does not submit rows, regardless of edit strategy.
See also primeInsert() and insertRecord().
bool QSqlTableModel::isDirty(const QModelIndex &index) const
If index is invalid or points to a non-existing row, false is returned.
bool QSqlTableModel::isDirty() const
This is an overloaded function.
Returns true
if the model contains modified values that have not been committed to the database, otherwise false.
This function was introduced in Qt 5.0.
[virtual protected]
QString QSqlTableModel::orderByClause() const
Returns an SQL ORDER BY
clause based on the currently set sort order.
See also setSort() and selectStatement().
QSqlIndex QSqlTableModel::primaryKey() const
See also setTable(), setPrimaryKey(), and QSqlDatabase::primaryIndex().
[protected]
QSqlRecord QSqlTableModel::primaryValues(int row) const
This function was introduced in Qt 5.1.
[signal]
void QSqlTableModel::primeInsert(int row, QSqlRecord &record)
QSqlRecord QSqlTableModel::record() const
This is an overloaded function.
See also setRecord() and QSqlRecord::isEmpty().
QSqlRecord QSqlTableModel::record(int row) const
Returns the record at row in the model.
If row is the index of a valid row, the record will be populated with values from that row.
If the model is not initialized, an empty record will be returned.
This function was introduced in Qt 5.0.
See also QSqlRecord::isEmpty().
[virtual]
bool QSqlTableModel::removeColumns(int column, int count, const QModelIndex &parent = QModelIndex())
Reimplemented from QAbstractItemModel::removeColumns().
Removes count columns from the parent model, starting at index column.
Returns if the columns were successfully removed; otherwise returns false
.
[virtual]
bool QSqlTableModel::removeRows(int row, int count, constQModelIndex &parent = QModelIndex())
Reimplemented from QAbstractItemModel::removeRows().
Before a row is deleted from the database, the beforeDelete() signal is emitted.
See also removeColumns() and insertRows().
[virtual slot]
void QSqlTableModel::revert()
Reimplemented from QAbstractItemModel::revert().
This reimplemented slot is called by the item delegates when the user canceled editing the current row.
Reverts the changes if the model's strategy is set to OnRowChange or OnFieldChange. Does nothing for the OnManualSubmit strategy.
Use revertAll() to revert all pending changes for the OnManualSubmit strategy or revertRow() to revert a specific row.
See also submit(), submitAll(), revertRow(), and revertAll().
[slot]
void QSqlTableModel::revertAll()
Reverts all pending changes.
See also revert(), revertRow(), and submitAll().
[virtual]
void QSqlTableModel::revertRow(int row)
Reverts all changes for the specified row.
See also revert(), revertAll(), submit(), and submitAll().
[virtual]
int QSqlTableModel::rowCount(const QModelIndex &parent = QModelIndex()) const
Reimplemented from QAbstractItemModel::rowCount().
[virtual slot]
bool QSqlTableModel::select()
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
.
Note: Calling select() will revert any unsubmitted changes and remove any inserted columns.
See also setTable(), setFilter(), and selectStatement().
[virtual slot]
bool QSqlTableModel::selectRow(int row)
Refreshes row in the model with values from the database table row matching on primary key values. Without a primary key, all column values must match. If no matching row is found, the model will show an empty row.
Returns true
if successful; otherwise returns false
.
This function was introduced in Qt 5.0.
See also select().
[virtual protected]
QString QSqlTableModel::selectStatement() const
See also filter() and orderByClause().
[virtual]
bool QSqlTableModel::setData(const QModelIndex &index, constQVariant &value, int role = Qt::EditRole)
Reimplemented from QAbstractItemModel::setData().
Sets the data for the item index for the role role to value.
Returns true
if the value could be set or false on error, for example if index is out of bounds.
See also editStrategy(), data(), submit(), submitAll(), and revertRow().
[virtual]
void QSqlTableModel::setEditStrategy(EditStrategy strategy)
Sets the strategy for editing values in the database to strategy.
This will revert any pending changes.
See also editStrategy() and revertAll().
[virtual]
void QSqlTableModel::setFilter(const QString &filter)
Sets the current filter to filter.
The filter is a SQL WHERE
clause without the keyword WHERE
(for example,name='Josephine')
.
See also filter(), select(), selectStatement(), and orderByClause().
[protected]
void QSqlTableModel::setPrimaryKey(const QSqlIndex &key)
Protected method that allows subclasses to set the primary key to key.
Normally, the primary index is set automatically whenever you call setTable().
See also primaryKey() and QSqlDatabase::primaryIndex().
[protected]
void QSqlTableModel::setQuery(const QSqlQuery &query)
bool QSqlTableModel::setRecord(int row, const QSqlRecord &values)
Returns true
if all the values could be set; otherwise returns false.
See also record() and editStrategy().
[virtual]
void QSqlTableModel::setSort(int column, Qt::SortOrder order)
See also sort(), select(), and orderByClause().
[virtual]
void QSqlTableModel::setTable(const QString &tableName)
To populate the model with the table's data, call select().
Error information can be retrieved with lastError().
See also select(), setFilter(), and lastError().
[virtual]
void QSqlTableModel::sort(int column, Qt::SortOrder order)
Reimplemented from QAbstractItemModel::sort().
See also setSort(), select(), and orderByClause().
[virtual slot]
bool QSqlTableModel::submit()
Reimplemented from QAbstractItemModel::submit().
This reimplemented slot is called by the item delegates when the user stopped editing the current row.
Submits the currently edited row if the model's strategy is set to OnRowChange orOnFieldChange. Does nothing for the OnManualSubmit strategy.
Use submitAll() to submit all pending changes for the OnManualSubmit strategy.
Returns true
on success; otherwise returns false
. Use lastError() to query detailed error information.
Does not automatically repopulate the model. Submitted rows are refreshed from the database on success.
See also revert(), revertRow(), submitAll(), revertAll(), and lastError().
[slot]
bool QSqlTableModel::submitAll()
Submits all pending changes and returns true
on success. Returns false
on error, detailed error information can be obtained with lastError().
In OnManualSubmit, on success the model will be repopulated. Any views presenting it will lose their selections.
Note: In OnManualSubmit mode, already submitted changes won't be cleared from the cache when submitAll() fails. This allows transactions to be rolled back and resubmitted without losing data.
See also revertAll() and lastError().
QString QSqlTableModel::tableName() const
Returns the name of the currently selected table.
[virtual protected]
bool QSqlTableModel::updateRowInTable(int row, const QSqlRecord &values)
QT 015 【数据库】 QSqlTableModel Class相关推荐
- 【Qt】数据库实战之QSqlTableModel模型
00. 目录 文章目录 00. 目录 01. 概述 02. 开发环境 03. QSqlTableModel基本操作 04. QSqlTableModel修改操作 05. QSqlTableModel查 ...
- 【Qt】数据库实战之QSqlRelationalTableModel
00. 目录 文章目录 00. 目录 01. 概述 02. 开发环境 03. QSqlRelationalTableModel外键 04. QSqlRelationalDelegate委托类 05. ...
- 【Qt】数据库实战之QSqlQueryModel
00. 目录 文章目录 00. 目录 01. 概述 02. 开发环境 03. QSqlQueryModel查询模型 04. QSqlQueryModel常用操作 05. QSqlQueryModel自 ...
- qt的mysql编程_界面编程之QT的数据库操作20180801
/*******************************************************************************************/ 一.数据库连 ...
- Qt入门------数据库操作
文章目录 一.数据库 1.数据库驱动 2.查询驱动 3.连接数据库 4.执行sql语句 5.插入数据 6.事务 二 ,sql模型类 1.QSqlQueryModel模型 2.QSqlTableMode ...
- QT 查询数据库 sql QSqlQueryModel
QT 查询数据库 sql QSqlQueryModel Qt中使用了自己的机制来避免使用SQL语句,它为我们提供了更简单的数据库操作和数据显示模型.它们分别是只 读的QSqlQueryModel,操作 ...
- 【Qt】数据库实战(三)
00. 目录 文章目录 00. 目录 01. 概述 02. 开发环境 03. 增删改查操作 04. 名字绑定和位置绑定 05. 程序示例 06. 批处理操作 07. 事务操作 08. 附录 01. 概 ...
- 【Qt】数据库实战(二)
00. 目录 文章目录 00. 目录 01. 概述 02. 开发环境 03. 连接到数据库 04. 操作结果集 06. 附录 01. 概述 SQL即结构化查询语言,是关系数据库的标准语言.前面已经在Q ...
- 【Qt】数据库实战(一)
00. 目录 文章目录 00. 目录 01. 概述 02. 开发环境 03. 数据库驱动 04. 创建数据库连接 05. 数据库简单操作 06. 附录 01. 概述 Qt中的Qt SQL模块提供了对数 ...
最新文章
- 一文详解基于先验地图的视觉定位
- 4月书讯:迟来的告白
- matlab 删除路径_MATLAB自动管理文件
- 全国计算机等级2级试题,全国计算机等级考试二级QBASIC试题
- leetcode738. 单调递增的数字
- linux pdb创建表空间,ORACLE12C PDB创建默认表空间和用户语句(示例代码)
- 光模块是怎么分类的?光模块的应用及作用有哪些?
- java线程一定是thread_深入理解Java多线程(multiThread)
- 论文 计算机网络技术的功能,计算机网络技术在有线电视网络中的作用论文
- java this()函数_Java经典面试题之(如何正确的使用this?)
- python加载dll后如何查看函数_Python调用DLL带指针函数读写IC卡
- 天猫tf卡速度测试软件,没有对比就没有伤害,老司机实测告诉你高速TF卡究竟有什么好处...
- excel表格分割线一分为二_仓储匠人资料合集:仓库物流案例Excel程序、PPT、音频、视频和教材 (最后2天优惠)...
- 各省2002-2017年绿色全要素生产率(超效率SBM、EBM、方向距离函数)
- c语言工程师专业分析,一个资深c语言工程师说如何学习c语言.pdf
- python开发PC端桌面应用
- 迦瓦栈队 团队第一周项目总结
- 机器视觉工程师的几种类型
- 分析天平计算机分类,分析化学之分析天平的概述和分类
- Radysis ATCA-PP50板交换芯片配置
热门文章
- Centos Docker 安装 Redis 并测试使用记录
- 编程语言中,差、交、并、自然连接、选择、投影、笛卡尔积分别都是什么运算...
- 【C语言】数据结构C语言版 实验6 树
- 在一个C程序中,main()函数可以放在哪?
- 词嵌入 网络嵌入_深入研究词嵌入以进行情感分析
- python 多线程处理_Python中的多处理与多线程:您需要了解的内容。
- 高位字节 低位字节_所有字节从何而来?
- enable 华为交换机ntdp_华为交换机配置
- 数据结构:单链表和双向链表
- linux 分区 备份软件下载,硬盘分区备份(Image For Windows)