QT 015 【数据库】 QSqlTableModel Class
2017-08-05 15:14
417 查看
Ref: http://doc.qt.io/qt-5/qsqltablemodel.html
1 小结
2 翻译:
The QSqlTableModel class provides an editable data model
for a single database table. More...
List of all members, including inherited members
16 public functions inherited from QSqlQueryModel
4 public functions inherited from QAbstractTableModel
39 public functions inherited from QAbstractItemModel
32 public functions inherited from QObject
2 public slots inherited from QAbstractItemModel
1 public slot inherited from QObject
18 signals inherited from QAbstractItemModel
2 signals inherited from QObject
3 protected functions inherited from QSqlQueryModel
19 protected functions inherited from QAbstractItemModel
9 protected functions inherited from QObject
1 property inherited from QObject
11 static public members inherited from QObject
1 protected slot inherited from QAbstractItemModel
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:
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:
The code snippet above extracts the
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.
This enum type describes which strategy to choose when editing values in the database.
Note: To prevent inserting only partly initialized rows into the database,
newly inserted rows.
See also setEditStrategy().
Creates an empty QSqlTableModel and sets the parent to parent and
the database connection to db. If db is not valid, the default database connection will be used.
The default edit strategy is OnRowChange.
Destroys the object and frees any allocated resources.
This signal is emitted by deleteRowFromTable()
before the row is deleted from the currently active database table.
This signal is emitted by insertRowIntoTable()
before a new row is inserted into the currently active database table. The values that are about to be inserted are stored in record and can be modified before they will be inserted.
This signal is emitted by updateRowInTable()
before the row is updated in the currently active database table with the values from record.
Note that only values that are marked as generated will be updated. The generated flag can be set with QSqlRecord::setGenerated()
and checked with QSqlRecord::isGenerated().
See also QSqlRecord::isGenerated().
Reimplemented from QSqlQueryModel::clear().
Reimplemented from QAbstractItemModel::data().
See also setData().
Returns the model's database connection.
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
See also removeRow()
and removeRows().
Returns the current edit strategy.
See also setEditStrategy().
Returns the index of the field fieldName, or -1 if no corresponding field exists in the model.
Returns the currently set filter.
See also setFilter()
and select().
Reimplemented from QAbstractItemModel::flags().
Reimplemented from QAbstractItemModel::headerData().
Reimplemented from QSqlQueryModel::indexInQuery().
Returns the index of the value in the database result set for the given item in the model.
The return value is identical to item if no columns or rows have been inserted, removed, or moved around.
Returns an invalid model index if item is out of bounds or if item does not point to a value
in the result set.
See also QSqlQueryModel::indexInQuery().
Inserts the record at position row. If row is
negative, the record will be appended to the end. Calls insertRows()
and setRecord() internally.
Returns
Changes are submitted immediately for OnFieldChange and OnRowChange.
Failure does not leave a new row in the model.
See also insertRows(), removeRows(),
and setRecord().
Inserts the values values into the currently active database table.
This is a low-level method that operates directly on the database and should not be called directly. Use insertRow()
and setData() to insert values. The model will
decide depending on its edit strategy when to modify the database.
Returns
See also lastError(), insertRow(),
and insertRows().
Reimplemented from QAbstractItemModel::insertRows().
Inserts count empty rows at position row. Note that parent must
be invalid, since this model does not support parent-child relations.
For edit strategies OnFieldChange and OnRowChange,
only one row may be inserted at a time and the model may not contain other cached changes.
The primeInsert() signal will be emitted for
each new row. Connect to it if you want to initialize the new row with default values.
Does not submit rows, regardless of edit strategy.
Returns
See also primeInsert()
and insertRecord().
Returns
false. Dirty values are values that were modified in the model but not yet written into the database.
If index is invalid or points to a non-existing row, false is returned.
This is an overloaded function.
Returns
This function was introduced in Qt 5.0.
Returns an SQL
See also setSort()
and selectStatement().
Returns the primary key for the current table, or an empty QSqlIndex if
the table is not set or has no primary key.
See also setTable(), setPrimaryKey(),
and QSqlDatabase::primaryIndex().
Returns a record containing the fields represented in the primary key set to the values at row. If no primary key is defined, the returned record will contain all fields.
This function was introduced in Qt 5.1.
See also primaryKey().
This signal is emitted by insertRows(), when
an insertion is initiated in the given row of the currently active database table. The record parameter
can be written to (since it is a reference), for example to populate some fields with default values and set the generated flags of the fields. Do not try to edit the record via other means such as setData()
or setRecord() while handling this signal.
This is an overloaded function.
It returns an empty record, having only the field names. This function can be used to retrieve the field names of a record.
See also setRecord()
and QSqlRecord::isEmpty().
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().
Reimplemented from QAbstractItemModel::removeColumns().
Removes count columns from the parent model, starting at index column.
Returns if the columns were successfully removed; otherwise returns
See also removeRows().
Reimplemented from QAbstractItemModel::removeRows().
Removes count rows starting at row. Since this model does not support hierarchical structures,parent must
be an invalid model index.
When the edit strategy is OnManualSubmit,
deletion of rows from the database is delayed until submitAll()
is called.
For OnFieldChange and OnRowChange,
only one row may be deleted at a time and only if no other row has a cached change. Deletions are submitted immediately to the database. The model retains a blank row for successfully deleted row until refreshed with select().
After failed deletion, the operation is not reverted in the model. The application may resubmit or revert.
Inserted but not yet successfully submitted rows in the range to be removed are immediately removed from the model.
Before a row is deleted from the database, the beforeDelete()
signal is emitted.
If row < 0 or row + count > rowCount(), no action
is taken and false is returned. Returns
Detailed database error information can be retrieved using lastError().
See also removeColumns()
and insertRows().
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().
Reverts all pending changes.
See also revert(), revertRow(),
and submitAll().
Reverts all changes for the specified row.
See also revert(), revertAll(), submit(),
and submitAll().
Reimplemented from QAbstractItemModel::rowCount().
Populates the model with data from the table that was set via setTable(),
using the specified filter and sort condition, and returns
Note: Calling select() will revert any unsubmitted changes and remove any inserted columns.
See also setTable(), setFilter(),
and selectStatement().
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
This function was introduced in Qt 5.0.
See also select().
Returns the SQL
See also filter()
and orderByClause().
Reimplemented from QAbstractItemModel::setData().
Sets the data for the item index for the role role to value.
For edit strategy OnFieldChange, an index
may receive a change only if no other index has a cached change. Changes are submitted immediately. However, rows that have not yet been inserted in the database may be freely changed and are not submitted automatically. Submitted changes are not reverted
upon failure.
For OnRowChange, an index may receive
a change only if no other row has a cached change. Changes are not submitted automatically.
Returns
the value will not be submitted to the database.
Returns
out of bounds.
See also editStrategy(), data(), submit(), submitAll(),
and revertRow().
Sets the strategy for editing values in the database to strategy.
This will revert any pending changes.
See also editStrategy()
and revertAll().
Sets the current filter to filter.
The filter is a SQL
example,
If the model is already populated with data from a database, the model re-selects it with the new filter. Otherwise, the filter will be applied the next time select()
is called.
See also filter(), select(), selectStatement(),
and orderByClause().
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().
This function simply calls QSqlQueryModel::setQuery(query).
You should normally not call it on aQSqlTableModel. Instead,
use setTable(), setSort(), setFilter(),
etc., to set up the query.
See also selectStatement().
Applies values to the row in the model. The source and target fields are mapped by field name,
not by position in the record.
Note that the generated flags in values are preserved and determine whether the corresponding fields are used when changes are submitted to the database. The caller should remember
to set the generated flag to FALSE for fields where the database is meant to supply the value, such as an automatically incremented ID.
For edit strategies OnFieldChange and OnRowChange,
a row may receive a change only if no other row has a cached change. Changes are submitted immediately. Submitted changes are not reverted upon failure.
Returns
See also record()
and editStrategy().
Sets the sort order for column to order. This does not affect the current data, to refresh the
data using the new sort order, call select().
See also sort(), select(),
and orderByClause().
Sets the database table on which the model operates to tableName. Does not select data from the table, but fetches its field information.
To populate the model with the table's data, call select().
Error information can be retrieved with lastError().
See also select(), setFilter(),
and lastError().
Reimplemented from QAbstractItemModel::sort().
Sorts the data by column with the sort order order. This will immediately select data, usesetSort()
to set a sort order without populating the model with data.
See also setSort(), select(),
and orderByClause().
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
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().
Submits all pending changes and returns
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().
Returns the name of the currently selected table.
Updates the given row in the currently active database table with the specified values. Returns
successful; otherwise returns
This is a low-level method that operates directly on the database and should not be called directly. Use setData()
to update values. The model will decide depending on its edit strategy when to modify the database.
Note that only values that have the generated-flag set are updated. The generated-flag can be set with QSqlRecord::setGenerated()
and tested with QSqlRecord::isGenerated().
See also QSqlRecord::isGenerated()
and setData().
1 小结
2 翻译:
QSqlTableModel Class
The QSqlTableModel class provides an editable data modelfor a single database table. More...
Header: | #include <QSqlTableModel> |
qmake: | QT += sql |
Inherits: | QSqlQueryModel |
Inherited By: | QSqlRelationalTableModel |
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) |
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() |
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) |
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 |
19 protected functions inherited from QAbstractItemModel
9 protected functions inherited from QObject
Additional Inherited Members
1 property inherited from QObject11 static public members inherited from QObject
1 protected slot inherited from QAbstractItemModel
Detailed Description
The QSqlTableModel class provides an editable data modelfor 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 ID view->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
salaryfield from record 4 in the result set of the query
SELECT * 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. |
OnFieldChangewill behave like
OnRowChangefor
newly inserted rows.
See also setEditStrategy().
Member Function Documentation
QSqlTableModel::QSqlTableModel(QObject *parent =
Q_NULLPTR,QSqlDatabase db =
QSqlDatabase())
Creates an empty QSqlTableModel and sets the parent to parent andthe database connection to db. If db is not valid, the default database connection will be used.
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)
This signal is emitted by insertRowIntoTable()before a new row is inserted into the currently active database table. The values that are about to be inserted are stored in record and can be modified before they will be inserted.
[signal]
void QSqlTableModel::beforeUpdate(int row, QSqlRecord &record)
This signal is emitted by updateRowInTable()before the row is updated in the currently active database table with the values from record.
Note that only values that are marked as generated will be updated. The generated flag can be set with QSqlRecord::setGenerated()
and checked with QSqlRecord::isGenerated().
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().See also setData().
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
trueif the row was deleted; otherwise returns
false.
See also removeRow()
and removeRows().
EditStrategy QSqlTableModel::editStrategy()
const
Returns the current edit strategy.See also setEditStrategy().
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.
The return value is identical to item if no columns or rows have been inserted, removed, or moved around.
Returns an invalid model index if item is out of bounds or if item does not point to a value
in the result set.
See also QSqlQueryModel::indexInQuery().
bool QSqlTableModel::insertRecord(int row,
const QSqlRecord &record)
Inserts the record at position row. If row isnegative, the record will be appended to the end. Calls insertRows()
and setRecord() internally.
Returns
trueif the record could be inserted, otherwise false.
Changes are submitted immediately for OnFieldChange and OnRowChange.
Failure does not leave a new row in the model.
See also insertRows(), removeRows(),
and setRecord().
[virtual
protected]
bool QSqlTableModel::insertRowIntoTable(constQSqlRecord &values)
Inserts the values values into the currently active database table.This is a low-level method that operates directly on the database and should not be called directly. Use insertRow()
and setData() to insert values. The model will
decide depending on its edit strategy when to modify the database.
Returns
trueif the values could be inserted, otherwise false. Error information can be retrieved with lastError().
See also lastError(), insertRow(),
and insertRows().
[virtual]
bool QSqlTableModel::insertRows(int row, int count,
constQModelIndex &parent =
QModelIndex())
Reimplemented from QAbstractItemModel::insertRows().Inserts count empty rows at position row. Note that parent must
be invalid, since this model does not support parent-child relations.
For edit strategies OnFieldChange and OnRowChange,
only one row may be inserted at a time and the model may not contain other cached changes.
The primeInsert() signal will be emitted for
each new row. Connect to it if you want to initialize the new row with default values.
Does not submit rows, regardless of edit strategy.
Returns
falseif the parameters are out of bounds or the row cannot be inserted; otherwise returns
true.
See also primeInsert()
and insertRecord().
bool QSqlTableModel::isDirty(const QModelIndex &index)
const
Returns trueif the value at the index index is dirty, otherwise
false. Dirty values are values that were modified in the model but not yet written into the database.
If index is invalid or points to a non-existing row, false is returned.
bool QSqlTableModel::isDirty()
const
This is an overloaded function.Returns
trueif 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 BYclause based on the currently set sort order.
See also setSort()
and selectStatement().
QSqlIndex QSqlTableModel::primaryKey()
const
Returns the primary key for the current table, or an empty QSqlIndex ifthe table is not set or has no primary key.
See also setTable(), setPrimaryKey(),
and QSqlDatabase::primaryIndex().
[protected]
QSqlRecord QSqlTableModel::primaryValues(int row)
const
Returns a record containing the fields represented in the primary key set to the values at row. If no primary key is defined, the returned record will contain all fields.This function was introduced in Qt 5.1.
See also primaryKey().
[signal]
void QSqlTableModel::primeInsert(int row, QSqlRecord &record)
This signal is emitted by insertRows(), whenan insertion is initiated in the given row of the currently active database table. The record parameter
can be written to (since it is a reference), for example to populate some fields with default values and set the generated flags of the fields. Do not try to edit the record via other means such as setData()
or setRecord() while handling this signal.
QSqlRecord QSqlTableModel::record()
const
This is an overloaded function.It returns an empty record, having only the field names. This function can be used to retrieve the field names of a record.
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.
See also removeRows().
[virtual]
bool QSqlTableModel::removeRows(int row, int count,
constQModelIndex &parent =
QModelIndex())
Reimplemented from QAbstractItemModel::removeRows().Removes count rows starting at row. Since this model does not support hierarchical structures,parent must
be an invalid model index.
When the edit strategy is OnManualSubmit,
deletion of rows from the database is delayed until submitAll()
is called.
For OnFieldChange and OnRowChange,
only one row may be deleted at a time and only if no other row has a cached change. Deletions are submitted immediately to the database. The model retains a blank row for successfully deleted row until refreshed with select().
After failed deletion, the operation is not reverted in the model. The application may resubmit or revert.
Inserted but not yet successfully submitted rows in the range to be removed are immediately removed from the model.
Before a row is deleted from the database, the beforeDelete()
signal is emitted.
If row < 0 or row + count > rowCount(), no action
is taken and false is returned. Returns
trueif all rows could be removed; otherwise returns
false.
Detailed database error information can be retrieved using lastError().
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
trueif 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
trueif successful; otherwise returns
false.
This function was introduced in Qt 5.0.
See also select().
[virtual
protected]
QString QSqlTableModel::selectStatement()
const
Returns the SQL SELECTstatement used internally to populate the model. The statement includes the filter and the
ORDER BYclause.
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.
For edit strategy OnFieldChange, an index
may receive a change only if no other index has a cached change. Changes are submitted immediately. However, rows that have not yet been inserted in the database may be freely changed and are not submitted automatically. Submitted changes are not reverted
upon failure.
For OnRowChange, an index may receive
a change only if no other row has a cached change. Changes are not submitted automatically.
Returns
trueif value is equal to the current value. However,
the value will not be submitted to the database.
Returns
trueif 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
WHEREclause without the keyword
WHERE(for
example,
name='Josephine').
If the model is already populated with data from a database, the model re-selects it with the new filter. Otherwise, the filter will be applied the next time select()
is called.
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)
This function simply calls QSqlQueryModel::setQuery(query).You should normally not call it on aQSqlTableModel. Instead,
use setTable(), setSort(), setFilter(),
etc., to set up the query.
See also selectStatement().
bool QSqlTableModel::setRecord(int row,
const QSqlRecord &values)
Applies values to the row in the model. The source and target fields are mapped by field name,not by position in the record.
Note that the generated flags in values are preserved and determine whether the corresponding fields are used when changes are submitted to the database. The caller should remember
to set the generated flag to FALSE for fields where the database is meant to supply the value, such as an automatically incremented ID.
For edit strategies OnFieldChange and OnRowChange,
a row may receive a change only if no other row has a cached change. Changes are submitted immediately. Submitted changes are not reverted upon failure.
Returns
trueif all the values could be set; otherwise returns false.
See also record()
and editStrategy().
[virtual]
void QSqlTableModel::setSort(int column, Qt::SortOrder order)
Sets the sort order for column to order. This does not affect the current data, to refresh thedata using the new sort order, call select().
See also sort(), select(),
and orderByClause().
[virtual]
void QSqlTableModel::setTable(const QString &tableName)
Sets the database table on which the model operates to tableName. Does not select data from the table, but fetches its field information.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().Sorts the data by column with the sort order order. This will immediately select data, usesetSort()
to set a sort order without populating the model with data.
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
trueon 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 trueon success. Returns
falseon
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)
Updates the given row in the currently active database table with the specified values. Returnstrueif
successful; otherwise returns
false.
This is a low-level method that operates directly on the database and should not be called directly. Use setData()
to update values. The model will decide depending on its edit strategy when to modify the database.
Note that only values that have the generated-flag set are updated. The generated-flag can be set with QSqlRecord::setGenerated()
and tested with QSqlRecord::isGenerated().
See also QSqlRecord::isGenerated()
and setData().
相关文章推荐
- Qt数据库之QSqlTableModel
- Qt数据库 QSqlTableModel实例操作
- Qt数据库 QSqlTableModel实例操作(1)
- Qt 之 QSqlTableModel Class
- QT笔记:数据库总结(四)之SQL模型类-QSqlRelationalTableModel模型
- QT笔记:数据库总结(三)之SQL模型类-QSqlTableModel模型
- QT笔记:数据库总结(三)之SQL模型类-QSqlTableModel模型
- QT笔记:数据库总结(三)之SQL模型类-QSqlTableModel模型
- QT笔记:数据库总结(三)之SQL模型类-QSqlTableModel模型
- Qt使用 QSqlTableModel 模型操作数据库
- QT笔记:数据库总结(四)之SQL模型类-QSqlRelationalTableModel模型
- 2.关于QT中数据库操作,简单数据库连接操作,数据库的增删改查,QSqlTableModel和QTableView,事务操作,关于QItemDelegate 代理
- 菜鸟玩qt(2)---翻译QSqlTableModel Class帮助文档
- QT学习——数据库SQL模型类(QSqlTableModel模型)
- 十六、Qt数据库 之 QSqlQueryModel & QSqlTableModel
- QT笔记:数据库总结(三)之SQL模型类-QSqlTableModel模型
- 菜鸟玩qt(2)---翻译QSqlTableModel Class帮助文档
- QSqlTableModel +QTableView 无法更新数据库
- Qt 数据库之 QSqlRelationalTableModel
- Qt中Qtableview的使用1(QSqlTableModel模型)