CopperSpice API  1.7.2
QSqlTableModel Class Reference

Provides an editable data model for a single database table. More...

Inheritance diagram for QSqlTableModel:
QSqlQueryModel QAbstractTableModel QAbstractItemModel QObject QSqlRelationalTableModel

Public Types

enum  EditStrategy
 

Public Signals

void beforeDelete (int row)
 
void beforeInsert (QSqlRecord &record)
 
void beforeUpdate (int row, QSqlRecord &record)
 
void primeInsert (int row, QSqlRecord &record)
 
- Public Signals inherited from QAbstractItemModel
void columnsAboutToBeInserted (const QModelIndex &parent, int first, int last)
 
void columnsAboutToBeMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
 
void columnsAboutToBeRemoved (const QModelIndex &parent, int first, int last)
 
void columnsInserted (const QModelIndex &parent, int first, int last)
 
void columnsMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
 
void columnsRemoved (const QModelIndex &parent, int first, int last)
 
void dataChanged (const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles=QVector< int >())
 
void headerDataChanged (Qt::Orientation orientation, int first, int last)
 
void layoutAboutToBeChanged (const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
 
void layoutChanged (const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
 
void modelAboutToBeReset ()
 
void modelReset ()
 
void rowsAboutToBeInserted (const QModelIndex &parent, int first, int last)
 
void rowsAboutToBeMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
 
void rowsAboutToBeRemoved (const QModelIndex &parent, int first, int last)
 
void rowsInserted (const QModelIndex &parent, int first, int last)
 
void rowsMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
 
void rowsRemoved (const QModelIndex &parent, int first, int last)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Slots

void revert ()
 
void revertAll ()
 
virtual bool select ()
 
bool submit ()
 
bool submitAll ()
 
- Public Slots inherited from QAbstractItemModel
virtual void revert ()
 
virtual bool submit ()
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QSqlTableModel (QObject *parent=nullptr, QSqlDatabase db=QSqlDatabase ())
 
virtual ~QSqlTableModel ()
 
void clear () override
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
QSqlDatabase database () const
 
EditStrategy editStrategy () const
 
int fieldIndex (const QString &fieldName) const
 
QString filter () const
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
 
bool insertRecord (int row, const QSqlRecord &record)
 
bool insertRows (int row, int count, const QModelIndex &parent=QModelIndex ()) override
 
bool isDirty (const QModelIndex &index) const
 
QSqlIndex primaryKey () const
 
bool removeColumns (int column, int count, const QModelIndex &parent=QModelIndex ()) override
 
bool removeRows (int row, int count, const QModelIndex &parent=QModelIndex ()) override
 
virtual void revertRow (int row)
 
int rowCount (const QModelIndex &parent=QModelIndex ()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
 
virtual void setEditStrategy (EditStrategy strategy)
 
virtual void setFilter (const QString &filter)
 
bool setRecord (int row, const QSqlRecord &record)
 
virtual void setSort (int column, Qt::SortOrder order)
 
virtual void setTable (const QString &tableName)
 
void sort (int column, Qt::SortOrder order) override
 
QString tableName () const
 
- Public Methods inherited from QSqlQueryModel
 QSqlQueryModel (QObject *parent=nullptr)
 
virtual ~QSqlQueryModel ()
 
bool canFetchMore (const QModelIndex &parent=QModelIndex ()) const override
 
int columnCount (const QModelIndex &index=QModelIndex ()) const override
 
void fetchMore (const QModelIndex &parent=QModelIndex ()) override
 
bool insertColumns (int column, int count, const QModelIndex &parent=QModelIndex ()) override
 
QSqlError lastError () const
 
QSqlQuery query () const
 
QSqlRecord record () const
 
QSqlRecord record (int row) const
 
bool setHeaderData (int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole) override
 
void setQuery (const QSqlQuery &query)
 
void setQuery (const QString &query, const QSqlDatabase &db=QSqlDatabase ())
 
- Public Methods inherited from QAbstractTableModel
 QAbstractTableModel (QObject *parent=nullptr)
 
 ~QAbstractTableModel ()
 
bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex ()) const override
 
QObjectparent () const
 
QModelIndex sibling (int row, int column, const QModelIndex &index) const override
 
- Public Methods inherited from QAbstractItemModel
 QAbstractItemModel (QObject *parent=nullptr)
 
virtual ~QAbstractItemModel ()
 
virtual QModelIndex buddy (const QModelIndex &index) const
 
virtual bool canDropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
 
bool hasIndex (int row, int column, const QModelIndex &parent=QModelIndex ()) const
 
bool insertColumn (int column, const QModelIndex &parent=QModelIndex ())
 
bool insertRow (int row, const QModelIndex &parent=QModelIndex ())
 
virtual QMap< int, QVariantitemData (const QModelIndex &index) const
 
virtual QModelIndexList match (const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags (Qt::MatchStartsWith|Qt::MatchWrap)) const
 
virtual QMimeDatamimeData (const QModelIndexList &indexes) const
 
virtual QStringList mimeTypes () const
 
bool moveColumn (const QModelIndex &sourceParent, int sourceColumn, const QModelIndex &destinationParent, int destinationChild)
 
virtual bool moveColumns (const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild)
 
bool moveRow (const QModelIndex &sourceParent, int sourceRow, const QModelIndex &destinationParent, int destinationChild)
 
virtual bool moveRows (const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
 
QObjectparent () const
 
bool removeColumn (int column, const QModelIndex &parent=QModelIndex ())
 
bool removeRow (int row, const QModelIndex &parent=QModelIndex ())
 
virtual QMultiHash< int, QStringroleNames () const
 
virtual bool setItemData (const QModelIndex &index, const QMap< int, QVariant > &roles)
 
virtual QSize span (const QModelIndex &index) const
 
virtual Qt::DropActions supportedDragActions () const
 
virtual Qt::DropActions supportedDropActions () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &objName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Protected Methods

virtual bool deleteRowFromTable (int row)
 
QModelIndex indexInQuery (const QModelIndex &item) const override
 
virtual bool insertRowIntoTable (const QSqlRecord &values)
 
virtual QString orderByClause () const
 
virtual QString selectStatement () const
 
void setPrimaryKey (const QSqlIndex &key)
 
void setQuery (const QSqlQuery &query)
 
virtual bool updateRowInTable (int row, const QSqlRecord &values)
 
- Protected Methods inherited from QSqlQueryModel
virtual void queryChange ()
 
void setLastError (const QSqlError &error)
 
- Protected Methods inherited from QAbstractItemModel
void beginInsertColumns (const QModelIndex &parent, int first, int last)
 
void beginInsertRows (const QModelIndex &parent, int first, int last)
 
bool beginMoveColumns (const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn)
 
bool beginMoveRows (const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow)
 
void beginRemoveColumns (const QModelIndex &parent, int first, int last)
 
void beginRemoveRows (const QModelIndex &parent, int first, int last)
 
void beginResetModel ()
 
void changePersistentIndex (const QModelIndex &from, const QModelIndex &to)
 
void changePersistentIndexList (const QModelIndexList &from, const QModelIndexList &to)
 
QModelIndex createIndex (int row, int column, quintptr id) const
 
QModelIndex createIndex (int row, int column, void *data=nullptr) const
 
void endInsertColumns ()
 
void endInsertRows ()
 
void endMoveColumns ()
 
void endMoveRows ()
 
void endRemoveColumns ()
 
void endRemoveRows ()
 
void endResetModel ()
 
QModelIndexList persistentIndexList () const
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Additional Inherited Members

- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Protected Slots inherited from QAbstractItemModel
void resetInternalData ()
 
- Properties inherited from QObject
 objectName
 

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); // do not 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, and OnManualSubmit.

QSqlTableModel can also be used to access a database programmatically, without binding it to a view:

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.

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.

QSqlTableModel provides no direct support for foreign keys. Use the QSqlRelationalTableModel and QSqlRelationalDelegate if you want to resolve foreign keys.

See also
QSqlRelationalTableModel, QSqlQuery, Model/View Programming

Member Enumeration Documentation

This enum type describes which strategy to choose when editing values in the database.

ConstantValueDescription
QSqlTableModel::OnFieldChange0All changes to the model will be applied immediately to the database.
QSqlTableModel::OnRowChange1Changes to a row will be applied when the user selects a different row.
QSqlTableModel::OnManualSubmit2All 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()

Constructor & Destructor Documentation

QSqlTableModel::QSqlTableModel ( QObject parent = nullptr,
QSqlDatabase  db = QSqlDatabase() 
)
explicit

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.

QSqlTableModel::~QSqlTableModel ( )
virtual

Destroys the object and frees any allocated resources.

Method Documentation

void QSqlTableModel::beforeDelete ( int  row)
signal

This signal is emitted by deleteRowFromTable() before the row is deleted from the currently active database table.

void QSqlTableModel::beforeInsert ( QSqlRecord record)
signal

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.

void QSqlTableModel::beforeUpdate ( int  row,
QSqlRecord record 
)
signal

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()
void QSqlTableModel::clear ( )
overridevirtual

Reimplemented from QSqlQueryModel::clear().

Reimplemented from QSqlQueryModel.

QVariant QSqlTableModel::data ( const QModelIndex index,
int  role = Qt::DisplayRole 
) const
overridevirtual

Reimplemented from QAbstractItemModel::data().

See also
setData()

Reimplemented from QSqlQueryModel.

QSqlDatabase QSqlTableModel::database ( ) const

Returns a pointer to the used QSqlDatabase or 0 if no database was set.

bool QSqlTableModel::deleteRowFromTable ( int  row)
protectedvirtual

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(), 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(), select()
Qt::ItemFlags QSqlTableModel::flags ( const QModelIndex index) const
overridevirtual

Reimplemented from QAbstractItemModel::flags().

Reimplemented from QAbstractTableModel.

QVariant QSqlTableModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role = Qt::DisplayRole 
) const
overridevirtual

Reimplemented from QAbstractItemModel::headerData().

Reimplemented from QSqlQueryModel.

QModelIndex QSqlTableModel::indexInQuery ( const QModelIndex item) const
overrideprotectedvirtual

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()

Reimplemented from QSqlQueryModel.

bool QSqlTableModel::insertRecord ( int  row,
const QSqlRecord record 
)

Inserts the record after row. If row is negative, the record will be appended to the end. Calls insertRows() and setRecord() internally.

Returns true if the row could be inserted, otherwise false.

See also
insertRows(), removeRows()
bool QSqlTableModel::insertRowIntoTable ( const QSqlRecord values)
protectedvirtual

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 true if the values could be inserted, otherwise false. Error information can be retrieved with lastError().

See also
lastError(), insertRow(), insertRows()

Reimplemented in QSqlRelationalTableModel.

bool QSqlTableModel::insertRows ( int  row,
int  count,
const QModelIndex parent = QModelIndex() 
)
overridevirtual

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.

Only one row at a time can be inserted when using the OnFieldChange or OnRowChange update strategies.

The primeInsert() signal will be emitted for each new row. Connect to it if you want to initialize the new row with default values.

Returns false if the parameters are out of bounds, otherwise returns true.

See also
primeInsert(), insertRecord()

Reimplemented from QAbstractItemModel.

bool QSqlTableModel::isDirty ( const QModelIndex index) const

Returns true if 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.

QString QSqlTableModel::orderByClause ( ) const
protectedvirtual

Returns an SQL ORDER BY clause based on the currently set sort order.

See also
setSort(), selectStatement()

Reimplemented in QSqlRelationalTableModel.

QSqlIndex QSqlTableModel::primaryKey ( ) const

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(), QSqlDatabase::primaryIndex()
void QSqlTableModel::primeInsert ( int  row,
QSqlRecord record 
)
signal

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.

bool QSqlTableModel::removeColumns ( int  column,
int  count,
const QModelIndex parent = QModelIndex() 
)
overridevirtual

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()

Reimplemented from QSqlQueryModel.

bool QSqlTableModel::removeRows ( int  row,
int  count,
const QModelIndex parent = QModelIndex() 
)
overridevirtual

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.

Emits the beforeDelete() signal before a row is deleted. When the edit strategy is OnManualSubmit signal emission is delayed until submitAll() is called.

Returns true if all rows could be removed, otherwise returns false. Detailed error information can be retrieved using lastError().

See also
removeColumns(), insertRows()

Reimplemented from QAbstractItemModel.

void QSqlTableModel::revert ( )
slot

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. Does nothing for the other edit strategies.

Use revertAll() to revert all pending changes for the OnManualSubmit strategy or revertRow() to revert a specific row.

See also
submit(), submitAll(), revertRow(), revertAll()
void QSqlTableModel::revertAll ( )
slot

Reverts all pending changes.

See also
revert(), revertRow(), submitAll()
void QSqlTableModel::revertRow ( int  row)
virtual

Reverts all changes for the specified row.

See also
revert(), revertAll(), submit(), submitAll()
int QSqlTableModel::rowCount ( const QModelIndex parent = QModelIndex()) const
overridevirtual

Reimplemented from QAbstractItemModel::rowCount().

Reimplemented from QSqlQueryModel.

bool QSqlTableModel::select ( )
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.

Note
Calling select() will revert any unsubmitted changes and remove any inserted columns.
See also
setTable(), setFilter(), selectStatement()
QString QSqlTableModel::selectStatement ( ) const
protectedvirtual

Returns the SQL SELECT statement used internally to populate the model. The statement includes the filter and the ORDER BY clause.

See also
filter(), orderByClause()

Reimplemented in QSqlRelationalTableModel.

bool QSqlTableModel::setData ( const QModelIndex index,
const QVariant value,
int  role = Qt::EditRole 
)
overridevirtual

Reimplemented from QAbstractItemModel::setData().

Sets the data for the item index for the role role to value. Depending on the edit strategy, the value might be applied to the database at once or cached in the model.

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(), revertRow()

Reimplemented from QAbstractItemModel.

void QSqlTableModel::setEditStrategy ( EditStrategy  strategy)
virtual

Sets the strategy for editing values in the database to strategy.

This will revert any pending changes.

See also
editStrategy(), revertAll()
void QSqlTableModel::setFilter ( const QString filter)
virtual

Sets the current filter to filter.

The filter is a SQL WHERE clause 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(), orderByClause()
void QSqlTableModel::setPrimaryKey ( const QSqlIndex key)
protected

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(), QSqlDatabase::primaryIndex()
void QSqlTableModel::setQuery ( const QSqlQuery query)
protected

This function simply calls QSqlQueryModel::setQuery(query). You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.

See also
selectStatement()
bool QSqlTableModel::setRecord ( int  row,
const QSqlRecord record 
)

Sets the values at the specified row to the values of record. Returns true if all the values could be set, otherwise returns false.

See also
record()
void QSqlTableModel::setSort ( int  column,
Qt::SortOrder  order 
)
virtual

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(), orderByClause()
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.

To populate the model with the table's data, call select().

Error information can be retrieved with lastError().

See also
select(), setFilter(), lastError()

Reimplemented in QSqlRelationalTableModel.

void QSqlTableModel::sort ( int  column,
Qt::SortOrder  order 
)
overridevirtual

Reimplemented from QAbstractItemModel::sort().

Sorts the data by column with the sort order order. This will immediately select data, use setSort() to set a sort order without populating the model with data.

See also
setSort(), select(), orderByClause()

Reimplemented from QAbstractItemModel.

bool QSqlTableModel::submit ( )
slot

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 or OnFieldChange. 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.

On success the model will be repopulated. Any views presenting it will lose their selections.

See also
revert(), revertRow(), submitAll(), revertAll(), lastError()
bool QSqlTableModel::submitAll ( )
slot

Submits all pending changes and returns true on success. Returns false on error, detailed error information can be obtained with lastError().

On success the model will be repopulated. Any views presenting it will lose their selections.

Note: In OnManualSubmit mode, already submitted changes will not be cleared from the cache when submitAll() fails. This allows transactions to be rolled back and resubmitted again without losing data.

See also
revertAll(), lastError()
QString QSqlTableModel::tableName ( ) const

Returns the name of the currently selected table.

bool QSqlTableModel::updateRowInTable ( int  row,
const QSqlRecord values 
)
protectedvirtual

Updates the given row in the currently active database table with the specified values. Returns true if 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(), setData()

Reimplemented in QSqlRelationalTableModel.