<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QSqlTableModel Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSqlTableModel Class Reference<br /><sup><sup>[<a href="qtsql.html">QtSql</a> module]</sup></sup></h1><p>The QSqlTableModel class provides an editable data model for a single database table. <a href="#details">More...</a></p> <p>Inherits <a href="qsqlquerymodel.html">QSqlQueryModel</a>.</p><p>Inherited by <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a>.</p><h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qsqltablemodel.html#EditStrategy-enum">EditStrategy</a></b> { OnFieldChange, OnRowChange, OnManualSubmit }</li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsqltablemodel.html#QSqlTableModel">__init__</a></b> (<i>self</i>, QObject <i>parent</i> = None, QSqlDatabase <i>db</i> = QSqlDatabase())</li><li><div class="fn" /><b><a href="qsqltablemodel.html#clear">clear</a></b> (<i>self</i>)</li><li><div class="fn" />QVariant <b><a href="qsqltablemodel.html#data">data</a></b> (<i>self</i>, QModelIndex <i>index</i>, int <i>role</i> = Qt.DisplayRole)</li><li><div class="fn" />QSqlDatabase <b><a href="qsqltablemodel.html#database">database</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#deleteRowFromTable">deleteRowFromTable</a></b> (<i>self</i>, int <i>row</i>)</li><li><div class="fn" />EditStrategy <b><a href="qsqltablemodel.html#editStrategy">editStrategy</a></b> (<i>self</i>)</li><li><div class="fn" />int <b><a href="qsqltablemodel.html#fieldIndex">fieldIndex</a></b> (<i>self</i>, QString <i>fieldName</i>)</li><li><div class="fn" />QString <b><a href="qsqltablemodel.html#filter">filter</a></b> (<i>self</i>)</li><li><div class="fn" />Qt.ItemFlags <b><a href="qsqltablemodel.html#flags">flags</a></b> (<i>self</i>, QModelIndex <i>index</i>)</li><li><div class="fn" />QVariant <b><a href="qsqltablemodel.html#headerData">headerData</a></b> (<i>self</i>, int <i>section</i>, Qt.Orientation <i>orientation</i>, int <i>role</i> = Qt.DisplayRole)</li><li><div class="fn" />QModelIndex <b><a href="qsqltablemodel.html#indexInQuery">indexInQuery</a></b> (<i>self</i>, QModelIndex <i>item</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#insertRecord">insertRecord</a></b> (<i>self</i>, int <i>row</i>, QSqlRecord <i>record</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#insertRowIntoTable">insertRowIntoTable</a></b> (<i>self</i>, QSqlRecord <i>values</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#insertRows">insertRows</a></b> (<i>self</i>, int <i>row</i>, int <i>count</i>, QModelIndex <i>parent</i> = QModelIndex())</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#isDirty">isDirty</a></b> (<i>self</i>, QModelIndex <i>index</i>)</li><li><div class="fn" />QString <b><a href="qsqltablemodel.html#orderByClause">orderByClause</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlIndex <b><a href="qsqltablemodel.html#primaryKey">primaryKey</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#removeColumns">removeColumns</a></b> (<i>self</i>, int <i>column</i>, int <i>count</i>, QModelIndex <i>parent</i> = QModelIndex())</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#removeRows">removeRows</a></b> (<i>self</i>, int <i>row</i>, int <i>count</i>, QModelIndex <i>parent</i> = QModelIndex())</li><li><div class="fn" /><b><a href="qsqltablemodel.html#revert">revert</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsqltablemodel.html#revertAll">revertAll</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsqltablemodel.html#revertRow">revertRow</a></b> (<i>self</i>, int <i>row</i>)</li><li><div class="fn" />int <b><a href="qsqltablemodel.html#rowCount">rowCount</a></b> (<i>self</i>, QModelIndex <i>parent</i> = QModelIndex())</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#select">select</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqltablemodel.html#selectStatement">selectStatement</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#setData">setData</a></b> (<i>self</i>, QModelIndex <i>index</i>, QVariant <i>value</i>, int <i>role</i> = Qt.EditRole)</li><li><div class="fn" /><b><a href="qsqltablemodel.html#setEditStrategy">setEditStrategy</a></b> (<i>self</i>, EditStrategy <i>strategy</i>)</li><li><div class="fn" /><b><a href="qsqltablemodel.html#setFilter">setFilter</a></b> (<i>self</i>, QString <i>filter</i>)</li><li><div class="fn" /><b><a href="qsqltablemodel.html#setPrimaryKey">setPrimaryKey</a></b> (<i>self</i>, QSqlIndex <i>key</i>)</li><li><div class="fn" /><b><a href="qsqltablemodel.html#setQuery">setQuery</a></b> (<i>self</i>, QSqlQuery <i>query</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#setRecord">setRecord</a></b> (<i>self</i>, int <i>row</i>, QSqlRecord <i>record</i>)</li><li><div class="fn" /><b><a href="qsqltablemodel.html#setSort">setSort</a></b> (<i>self</i>, int <i>column</i>, Qt.SortOrder <i>order</i>)</li><li><div class="fn" /><b><a href="qsqltablemodel.html#setTable">setTable</a></b> (<i>self</i>, QString <i>tableName</i>)</li><li><div class="fn" /><b><a href="qsqltablemodel.html#sort">sort</a></b> (<i>self</i>, int <i>column</i>, Qt.SortOrder <i>order</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#submit">submit</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#submitAll">submitAll</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqltablemodel.html#tableName">tableName</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqltablemodel.html#updateRowInTable">updateRowInTable</a></b> (<i>self</i>, int <i>row</i>, QSqlRecord <i>values</i>)</li></ul><h3>Qt Signals</h3><ul><li><div class="fn" />void <b><a href="qsqltablemodel.html#beforeDelete">beforeDelete</a></b> (int)</li><li><div class="fn" />void <b><a href="qsqltablemodel.html#beforeInsert">beforeInsert</a></b> (QSqlRecord&)</li><li><div class="fn" />void <b><a href="qsqltablemodel.html#beforeUpdate">beforeUpdate</a></b> (int,QSqlRecord&)</li><li><div class="fn" />void <b><a href="qsqltablemodel.html#primeInsert">primeInsert</a></b> (int,QSqlRecord&)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSqlTableModel class provides an editable data model for a single database table.</p> <p>QSqlTableModel is a high-level interface for reading and writing database records from a single table. It is build on top of the lower-level <a href="qsqlquery.html">QSqlQuery</a> and can be used to provide data to view classes such as <a href="qtableview.html">QTableView</a>. For example:</p> <pre class="cpp"> <span class="type">QSqlTableModel</span> <span class="operator">*</span>model <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QSqlTableModel</span>(parentObject<span class="operator">,</span> database); model<span class="operator">-</span><span class="operator">></span>setTable(<span class="string">"employee"</span>); model<span class="operator">-</span><span class="operator">></span>setEditStrategy(<span class="type">QSqlTableModel</span><span class="operator">.</span>OnManualSubmit); model<span class="operator">-</span><span class="operator">></span>select(); model<span class="operator">-</span><span class="operator">></span>setHeaderData(<span class="number">0</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">.</span>Horizontal<span class="operator">,</span> tr(<span class="string">"Name"</span>)); model<span class="operator">-</span><span class="operator">></span>setHeaderData(<span class="number">1</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">.</span>Horizontal<span class="operator">,</span> tr(<span class="string">"Salary"</span>)); <span class="type"><a href="qtableview.html">QTableView</a></span> <span class="operator">*</span>view <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtableview.html">QTableView</a></span>; view<span class="operator">-</span><span class="operator">></span>setModel(model); view<span class="operator">-</span><span class="operator">></span>hideColumn(<span class="number">0</span>); <span class="comment">// don't show the ID</span> view<span class="operator">-</span><span class="operator">></span>show(); </pre> <p>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 <a href="qsqltablemodel.html#EditStrategy-enum">OnFieldChange</a>, <a href="qsqltablemodel.html#EditStrategy-enum">OnRowChange</a>, and <a href="qsqltablemodel.html#EditStrategy-enum">OnManualSubmit</a>.</p> <p>QSqlTableModel can also be used to access a database programmatically, without binding it to a view:</p> <pre class="cpp"> <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span> model; model<span class="operator">.</span>setQuery(<span class="string">"SELECT * FROM employee"</span>); <span class="type">int</span> salary <span class="operator">=</span> model<span class="operator">.</span>record(<span class="number">4</span>)<span class="operator">.</span>value(<span class="string">"salary"</span>)<span class="operator">.</span>toInt(); </pre> <p>The code snippet above extracts the <tt>salary</tt> field from record 4 in the result set of the query <tt>SELECT * from employee</tt>.</p> <p>It is possible to set filters using <a href="qsqltablemodel.html#setFilter">setFilter</a>(), or modify the sort order using <a href="qsqltablemodel.html#setSort">setSort</a>(). At the end, you must call <a href="qsqltablemodel.html#select">select</a>() to populate the model with data.</p> <p>The <a href="sql-tablemodel.html">sql/tablemodel</a> example illustrates how to use QSqlTableModel as the data source for a <a href="qtableview.html">QTableView</a>.</p> <p>QSqlTableModel provides no direct support for foreign keys. Use the <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> and <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> if you want to resolve foreign keys.</p> <hr /><h2>Type Documentation</h2><h3 class="fn"><a name="EditStrategy-enum" />QSqlTableModel.EditStrategy</h3><p>This enum type describes which strategy to choose when editing values in the database.</p> <table class="valuelist"> <tr class="odd" valign="top"> <th class="tblConst">Constant</th> <th class="tblval">Value</th> <th class="tbldscr">Description</th> </tr> <tr> <td class="topAlign"><tt>QSqlTableModel.OnFieldChange</tt></td> <td class="topAlign"><tt>0</tt></td> <td class="topAlign">All changes to the model will be applied immediately to the database.</td> </tr> <tr> <td class="topAlign"><tt>QSqlTableModel.OnRowChange</tt></td> <td class="topAlign"><tt>1</tt></td> <td class="topAlign">Changes to a row will be applied when the user selects a different row.</td> </tr> <tr> <td class="topAlign"><tt>QSqlTableModel.OnManualSubmit</tt></td> <td class="topAlign"><tt>2</tt></td> <td class="topAlign">All changes will be cached in the model until either <a href="qsqltablemodel.html#submitAll">submitAll</a>() or <a href="qsqltablemodel.html#revertAll">revertAll</a>() is called.</td> </tr> </table> <p>Note: To prevent inserting only partly initialized rows into the database, <tt>OnFieldChange</tt> will behave like <tt>OnRowChange</tt> for newly inserted rows.</p> <p><b>See also</b> <a href="qsqltablemodel.html#setEditStrategy">setEditStrategy</a>().</p> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSqlTableModel" />QSqlTableModel.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None, <a href="qsqldatabase.html">QSqlDatabase</a> <i>db</i> = QSqlDatabase())</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Creates an empty <a href="qsqltablemodel.html">QSqlTableModel</a> and sets the parent to <i>parent</i> and the database connection to <i>db</i>. If <i>db</i> is not valid, the default database connection will be used.</p> <p>The default edit strategy is <a href="qsqltablemodel.html#EditStrategy-enum">OnRowChange</a>.</p> <h3 class="fn"><a name="clear" />QSqlTableModel.clear (<i>self</i>)</h3><p>Reimplemented from <a href="qsqlquerymodel.html#clear">QSqlQueryModel.clear</a>().</p> <h3 class="fn"><a name="data" />QVariant QSqlTableModel.data (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>, int <i>role</i> = Qt.DisplayRole)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#data">QAbstractItemModel.data</a>().</p> <p><b>See also</b> <a href="qsqltablemodel.html#setData">setData</a>().</p> <h3 class="fn"><a name="database" /><a href="qsqldatabase.html">QSqlDatabase</a> QSqlTableModel.database (<i>self</i>)</h3><p>Returns a pointer to the used <a href="qsqldatabase.html">QSqlDatabase</a> or 0 if no database was set.</p> <h3 class="fn"><a name="deleteRowFromTable" />bool QSqlTableModel.deleteRowFromTable (<i>self</i>, int <i>row</i>)</h3><p>Deletes the given <i>row</i> from the currently active database table.</p> <p>This is a low-level method that operates directly on the database and should not be called directly. Use <a href="qabstractitemmodel.html#removeRow">removeRow</a>() or <a href="qsqltablemodel.html#removeRows">removeRows</a>() to delete values. The model will decide depending on its edit strategy when to modify the database.</p> <p>Returns true if the row was deleted; otherwise returns false.</p> <p><b>See also</b> <a href="qabstractitemmodel.html#removeRow">removeRow</a>() and <a href="qsqltablemodel.html#removeRows">removeRows</a>().</p> <h3 class="fn"><a name="editStrategy" /><a href="qsqltablemodel.html#EditStrategy-enum">EditStrategy</a> QSqlTableModel.editStrategy (<i>self</i>)</h3><p>Returns the current edit strategy.</p> <p><b>See also</b> <a href="qsqltablemodel.html#setEditStrategy">setEditStrategy</a>().</p> <h3 class="fn"><a name="fieldIndex" />int QSqlTableModel.fieldIndex (<i>self</i>, QString <i>fieldName</i>)</h3><p>Returns the index of the field <i>fieldName</i>, or -1 if no corresponding field exists in the model.</p> <h3 class="fn"><a name="filter" />QString QSqlTableModel.filter (<i>self</i>)</h3><p>Returns the currently set filter.</p> <p><b>See also</b> <a href="qsqltablemodel.html#setFilter">setFilter</a>() and <a href="qsqltablemodel.html#select">select</a>().</p> <h3 class="fn"><a name="flags" /><a href="qt-itemflags.html">Qt.ItemFlags</a> QSqlTableModel.flags (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#flags">QAbstractItemModel.flags</a>().</p> <h3 class="fn"><a name="headerData" />QVariant QSqlTableModel.headerData (<i>self</i>, int <i>section</i>, <a href="qt.html#Orientation-enum">Qt.Orientation</a> <i>orientation</i>, int <i>role</i> = Qt.DisplayRole)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#headerData">QAbstractItemModel.headerData</a>().</p> <h3 class="fn"><a name="indexInQuery" /><a href="qmodelindex.html">QModelIndex</a> QSqlTableModel.indexInQuery (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a> <i>item</i>)</h3><p>Returns the index of the value in the database result set for the given <i>item</i> in the model.</p> <p>The return value is identical to <i>item</i> if no columns or rows have been inserted, removed, or moved around.</p> <p>Returns an invalid model index if <i>item</i> is out of bounds or if <i>item</i> does not point to a value in the result set.</p> <p><b>See also</b> <a href="qsqlquerymodel.html#indexInQuery">QSqlQueryModel.indexInQuery</a>().</p> <h3 class="fn"><a name="insertRecord" />bool QSqlTableModel.insertRecord (<i>self</i>, int <i>row</i>, <a href="qsqlrecord.html">QSqlRecord</a> <i>record</i>)</h3><p>Inserts the <i>record</i> after <i>row</i>. If <i>row</i> is negative, the record will be appended to the end. Calls <a href="qsqltablemodel.html#insertRows">insertRows</a>() and <a href="qsqltablemodel.html#setRecord">setRecord</a>() internally.</p> <p>Returns true if the row could be inserted, otherwise false.</p> <p><b>See also</b> <a href="qsqltablemodel.html#insertRows">insertRows</a>() and <a href="qsqltablemodel.html#removeRows">removeRows</a>().</p> <h3 class="fn"><a name="insertRowIntoTable" />bool QSqlTableModel.insertRowIntoTable (<i>self</i>, <a href="qsqlrecord.html">QSqlRecord</a> <i>values</i>)</h3><p>Inserts the values <i>values</i> into the currently active database table.</p> <p>This is a low-level method that operates directly on the database and should not be called directly. Use <a href="qabstractitemmodel.html#insertRow">insertRow</a>() and <a href="qsqltablemodel.html#setData">setData</a>() to insert values. The model will decide depending on its edit strategy when to modify the database.</p> <p>Returns true if the values could be inserted, otherwise false. Error information can be retrieved with <a href="qsqlquerymodel.html#lastError">lastError</a>().</p> <p><b>See also</b> <a href="qsqlquerymodel.html#lastError">lastError</a>(), <a href="qabstractitemmodel.html#insertRow">insertRow</a>(), and <a href="qsqltablemodel.html#insertRows">insertRows</a>().</p> <h3 class="fn"><a name="insertRows" />bool QSqlTableModel.insertRows (<i>self</i>, int <i>row</i>, int <i>count</i>, <a href="qmodelindex.html">QModelIndex</a> <i>parent</i> = QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#insertRows">QAbstractItemModel.insertRows</a>().</p> <p>Inserts <i>count</i> empty rows at position <i>row</i>. Note that <i>parent</i> must be invalid, since this model does not support parent-child relations.</p> <p>Only one row at a time can be inserted when using the <a href="qsqltablemodel.html#EditStrategy-enum">OnFieldChange</a> or <a href="qsqltablemodel.html#EditStrategy-enum">OnRowChange</a> update strategies.</p> <p>The <a href="qsqltablemodel.html#primeInsert">primeInsert</a>() signal will be emitted for each new row. Connect to it if you want to initialize the new row with default values.</p> <p>Returns false if the parameters are out of bounds; otherwise returns true.</p> <p><b>See also</b> <a href="qsqltablemodel.html#primeInsert">primeInsert</a>() and <a href="qsqltablemodel.html#insertRecord">insertRecord</a>().</p> <h3 class="fn"><a name="isDirty" />bool QSqlTableModel.isDirty (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>)</h3><p>Returns true if the value at the index <i>index</i> is dirty, otherwise false. Dirty values are values that were modified in the model but not yet written into the database.</p> <p>If <i>index</i> is invalid or points to a non-existing row, false is returned.</p> <h3 class="fn"><a name="orderByClause" />QString QSqlTableModel.orderByClause (<i>self</i>)</h3><p>Returns an SQL <tt>ORDER BY</tt> clause based on the currently set sort order.</p> <p><b>See also</b> <a href="qsqltablemodel.html#setSort">setSort</a>() and <a href="qsqltablemodel.html#selectStatement">selectStatement</a>().</p> <h3 class="fn"><a name="primaryKey" /><a href="qsqlindex.html">QSqlIndex</a> QSqlTableModel.primaryKey (<i>self</i>)</h3><p>Returns the primary key for the current table, or an empty <a href="qsqlindex.html">QSqlIndex</a> if the table is not set or has no primary key.</p> <p><b>See also</b> <a href="qsqltablemodel.html#setTable">setTable</a>(), <a href="qsqltablemodel.html#setPrimaryKey">setPrimaryKey</a>(), and <a href="qsqldatabase.html#primaryIndex">QSqlDatabase.primaryIndex</a>().</p> <h3 class="fn"><a name="removeColumns" />bool QSqlTableModel.removeColumns (<i>self</i>, int <i>column</i>, int <i>count</i>, <a href="qmodelindex.html">QModelIndex</a> <i>parent</i> = QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#removeColumns">QAbstractItemModel.removeColumns</a>().</p> <p>Removes <i>count</i> columns from the <i>parent</i> model, starting at index <i>column</i>.</p> <p>Returns if the columns were successfully removed; otherwise returns false.</p> <p><b>See also</b> <a href="qsqltablemodel.html#removeRows">removeRows</a>().</p> <h3 class="fn"><a name="removeRows" />bool QSqlTableModel.removeRows (<i>self</i>, int <i>row</i>, int <i>count</i>, <a href="qmodelindex.html">QModelIndex</a> <i>parent</i> = QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#removeRows">QAbstractItemModel.removeRows</a>().</p> <p>Removes <i>count</i> rows starting at <i>row</i>. Since this model does not support hierarchical structures, <i>parent</i> must be an invalid model index.</p> <p>Emits the <a href="qsqltablemodel.html#beforeDelete">beforeDelete</a>() signal before a row is deleted. When the edit strategy is <a href="qsqltablemodel.html#EditStrategy-enum">OnManualSubmit</a> signal emission is delayed until <a href="qsqltablemodel.html#submitAll">submitAll</a>() is called.</p> <p>Returns true if all rows could be removed; otherwise returns false. Detailed error information can be retrieved using <a href="qsqlquerymodel.html#lastError">lastError</a>().</p> <p><b>See also</b> <a href="qsqltablemodel.html#removeColumns">removeColumns</a>() and <a href="qsqltablemodel.html#insertRows">insertRows</a>().</p> <h3 class="fn"><a name="revert" />QSqlTableModel.revert (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void revert()</tt>.</p><p>Reimplemented from <a href="qabstractitemmodel.html#revert">QAbstractItemModel.revert</a>().</p> <p>This reimplemented slot is called by the item delegates when the user canceled editing the current row.</p> <p>Reverts the changes if the model's strategy is set to <a href="qsqltablemodel.html#EditStrategy-enum">OnRowChange</a>. Does nothing for the other edit strategies.</p> <p>Use <a href="qsqltablemodel.html#revertAll">revertAll</a>() to revert all pending changes for the <a href="qsqltablemodel.html#EditStrategy-enum">OnManualSubmit</a> strategy or <a href="qsqltablemodel.html#revertRow">revertRow</a>() to revert a specific row.</p> <p><b>See also</b> <a href="qsqltablemodel.html#submit">submit</a>(), <a href="qsqltablemodel.html#submitAll">submitAll</a>(), <a href="qsqltablemodel.html#revertRow">revertRow</a>(), and <a href="qsqltablemodel.html#revertAll">revertAll</a>().</p> <h3 class="fn"><a name="revertAll" />QSqlTableModel.revertAll (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void revertAll()</tt>.</p><p>Reverts all pending changes.</p> <p><b>See also</b> <a href="qsqltablemodel.html#revert">revert</a>(), <a href="qsqltablemodel.html#revertRow">revertRow</a>(), and <a href="qsqltablemodel.html#submitAll">submitAll</a>().</p> <h3 class="fn"><a name="revertRow" />QSqlTableModel.revertRow (<i>self</i>, int <i>row</i>)</h3><p>Reverts all changes for the specified <i>row</i>.</p> <p><b>See also</b> <a href="qsqltablemodel.html#revert">revert</a>(), <a href="qsqltablemodel.html#revertAll">revertAll</a>(), <a href="qsqltablemodel.html#submit">submit</a>(), and <a href="qsqltablemodel.html#submitAll">submitAll</a>().</p> <h3 class="fn"><a name="rowCount" />int QSqlTableModel.rowCount (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a> <i>parent</i> = QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#rowCount">QAbstractItemModel.rowCount</a>().</p> <h3 class="fn"><a name="select" />bool QSqlTableModel.select (<i>self</i>)</h3><p>Populates the model with data from the table that was set via <a href="qsqltablemodel.html#setTable">setTable</a>(), using the specified filter and sort condition, and returns true if successful; otherwise returns false.</p> <p><b>Note:</b> Calling select() will revert any unsubmitted changes and remove any inserted columns.</p> <p><b>See also</b> <a href="qsqltablemodel.html#setTable">setTable</a>(), <a href="qsqltablemodel.html#setFilter">setFilter</a>(), and <a href="qsqltablemodel.html#selectStatement">selectStatement</a>().</p> <h3 class="fn"><a name="selectStatement" />QString QSqlTableModel.selectStatement (<i>self</i>)</h3><p>Returns the SQL <tt>SELECT</tt> statement used internally to populate the model. The statement includes the filter and the <tt>ORDER BY</tt> clause.</p> <p><b>See also</b> <a href="qsqltablemodel.html#filter">filter</a>() and <a href="qsqltablemodel.html#orderByClause">orderByClause</a>().</p> <h3 class="fn"><a name="setData" />bool QSqlTableModel.setData (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a> <i>index</i>, QVariant <i>value</i>, int <i>role</i> = Qt.EditRole)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#setData">QAbstractItemModel.setData</a>().</p> <p>Sets the data for the item <i>index</i> for the role <i>role</i> to <i>value</i>. Depending on the edit strategy, the value might be applied to the database at once or cached in the model.</p> <p>Returns true if the value could be set or false on error, for example if <i>index</i> is out of bounds.</p> <p><b>See also</b> <a href="qsqltablemodel.html#editStrategy">editStrategy</a>(), <a href="qsqltablemodel.html#data">data</a>(), <a href="qsqltablemodel.html#submit">submit</a>(), <a href="qsqltablemodel.html#submitAll">submitAll</a>(), and <a href="qsqltablemodel.html#revertRow">revertRow</a>().</p> <h3 class="fn"><a name="setEditStrategy" />QSqlTableModel.setEditStrategy (<i>self</i>, <a href="qsqltablemodel.html#EditStrategy-enum">EditStrategy</a> <i>strategy</i>)</h3><p>Sets the strategy for editing values in the database to <i>strategy</i>.</p> <p>This will revert any pending changes.</p> <p><b>See also</b> <a href="qsqltablemodel.html#editStrategy">editStrategy</a>() and <a href="qsqltablemodel.html#revertAll">revertAll</a>().</p> <h3 class="fn"><a name="setFilter" />QSqlTableModel.setFilter (<i>self</i>, QString <i>filter</i>)</h3><p>Sets the current filter to <i>filter</i>.</p> <p>The filter is a SQL <tt>WHERE</tt> clause without the keyword <tt>WHERE</tt> (for example, <tt>name='Josephine')</tt>.</p> <p>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 <a href="qsqltablemodel.html#select">select</a>() is called.</p> <p><b>See also</b> <a href="qsqltablemodel.html#filter">filter</a>(), <a href="qsqltablemodel.html#select">select</a>(), <a href="qsqltablemodel.html#selectStatement">selectStatement</a>(), and <a href="qsqltablemodel.html#orderByClause">orderByClause</a>().</p> <h3 class="fn"><a name="setPrimaryKey" />QSqlTableModel.setPrimaryKey (<i>self</i>, <a href="qsqlindex.html">QSqlIndex</a> <i>key</i>)</h3><p>Protected method that allows subclasses to set the primary key to <i>key</i>.</p> <p>Normally, the primary index is set automatically whenever you call <a href="qsqltablemodel.html#setTable">setTable</a>().</p> <p><b>See also</b> <a href="qsqltablemodel.html#primaryKey">primaryKey</a>() and <a href="qsqldatabase.html#primaryIndex">QSqlDatabase.primaryIndex</a>().</p> <h3 class="fn"><a name="setQuery" />QSqlTableModel.setQuery (<i>self</i>, <a href="qsqlquery.html">QSqlQuery</a> <i>query</i>)</h3><p>This function simply calls QSqlQueryModel.setQuery(<i>query</i>). You should normally not call it on a <a href="qsqltablemodel.html">QSqlTableModel</a>. Instead, use <a href="qsqltablemodel.html#setTable">setTable</a>(), <a href="qsqltablemodel.html#setSort">setSort</a>(), <a href="qsqltablemodel.html#setFilter">setFilter</a>(), etc., to set up the query.</p> <p><b>See also</b> <a href="qsqltablemodel.html#selectStatement">selectStatement</a>().</p> <h3 class="fn"><a name="setRecord" />bool QSqlTableModel.setRecord (<i>self</i>, int <i>row</i>, <a href="qsqlrecord.html">QSqlRecord</a> <i>record</i>)</h3><p>Sets the values at the specified <i>row</i> to the values of <i>record</i>. Returns true if all the values could be set; otherwise returns false.</p> <p><b>See also</b> <a href="qsqlquerymodel.html#record">record</a>().</p> <h3 class="fn"><a name="setSort" />QSqlTableModel.setSort (<i>self</i>, int <i>column</i>, <a href="qt.html#SortOrder-enum">Qt.SortOrder</a> <i>order</i>)</h3><p>Sets the sort order for <i>column</i> to <i>order</i>. This does not affect the current data, to refresh the data using the new sort order, call <a href="qsqltablemodel.html#select">select</a>().</p> <p><b>See also</b> <a href="qsqltablemodel.html#sort">sort</a>(), <a href="qsqltablemodel.html#select">select</a>(), and <a href="qsqltablemodel.html#orderByClause">orderByClause</a>().</p> <h3 class="fn"><a name="setTable" />QSqlTableModel.setTable (<i>self</i>, QString <i>tableName</i>)</h3><p>Sets the database table on which the model operates to <i>tableName</i>. Does not select data from the table, but fetches its field information.</p> <p>To populate the model with the table's data, call <a href="qsqltablemodel.html#select">select</a>().</p> <p>Error information can be retrieved with <a href="qsqlquerymodel.html#lastError">lastError</a>().</p> <p><b>See also</b> <a href="qsqltablemodel.html#select">select</a>(), <a href="qsqltablemodel.html#setFilter">setFilter</a>(), and <a href="qsqlquerymodel.html#lastError">lastError</a>().</p> <h3 class="fn"><a name="sort" />QSqlTableModel.sort (<i>self</i>, int <i>column</i>, <a href="qt.html#SortOrder-enum">Qt.SortOrder</a> <i>order</i>)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#sort">QAbstractItemModel.sort</a>().</p> <p>Sorts the data by <i>column</i> with the sort order <i>order</i>. This will immediately select data, use <a href="qsqltablemodel.html#setSort">setSort</a>() to set a sort order without populating the model with data.</p> <p><b>See also</b> <a href="qsqltablemodel.html#setSort">setSort</a>(), <a href="qsqltablemodel.html#select">select</a>(), and <a href="qsqltablemodel.html#orderByClause">orderByClause</a>().</p> <h3 class="fn"><a name="submit" />bool QSqlTableModel.submit (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>bool submit()</tt>.</p><p>Reimplemented from <a href="qabstractitemmodel.html#submit">QAbstractItemModel.submit</a>().</p> <p>This reimplemented slot is called by the item delegates when the user stopped editing the current row.</p> <p>Submits the currently edited row if the model's strategy is set to <a href="qsqltablemodel.html#EditStrategy-enum">OnRowChange</a> or <a href="qsqltablemodel.html#EditStrategy-enum">OnFieldChange</a>. Does nothing for the <a href="qsqltablemodel.html#EditStrategy-enum">OnManualSubmit</a> strategy.</p> <p>Use <a href="qsqltablemodel.html#submitAll">submitAll</a>() to submit all pending changes for the <a href="qsqltablemodel.html#EditStrategy-enum">OnManualSubmit</a> strategy.</p> <p>Returns true on success; otherwise returns false. Use <a href="qsqlquerymodel.html#lastError">lastError</a>() to query detailed error information.</p> <p>On success the model will be repopulated. Any views presenting it will lose their selections.</p> <p><b>See also</b> <a href="qsqltablemodel.html#revert">revert</a>(), <a href="qsqltablemodel.html#revertRow">revertRow</a>(), <a href="qsqltablemodel.html#submitAll">submitAll</a>(), <a href="qsqltablemodel.html#revertAll">revertAll</a>(), and <a href="qsqlquerymodel.html#lastError">lastError</a>().</p> <h3 class="fn"><a name="submitAll" />bool QSqlTableModel.submitAll (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>bool submitAll()</tt>.</p><p>Submits all pending changes and returns true on success. Returns false on error, detailed error information can be obtained with <a href="qsqlquerymodel.html#lastError">lastError</a>().</p> <p>On success the model will be repopulated. Any views presenting it will lose their selections.</p> <p>Note: In <a href="qsqltablemodel.html#EditStrategy-enum">OnManualSubmit</a> mode, already submitted changes won't be cleared from the cache when submitAll() fails. This allows transactions to be rolled back and resubmitted again without losing data.</p> <p><b>See also</b> <a href="qsqltablemodel.html#revertAll">revertAll</a>() and <a href="qsqlquerymodel.html#lastError">lastError</a>().</p> <h3 class="fn"><a name="tableName" />QString QSqlTableModel.tableName (<i>self</i>)</h3><p>Returns the name of the currently selected table.</p> <h3 class="fn"><a name="updateRowInTable" />bool QSqlTableModel.updateRowInTable (<i>self</i>, int <i>row</i>, <a href="qsqlrecord.html">QSqlRecord</a> <i>values</i>)</h3><p>Updates the given <i>row</i> in the currently active database table with the specified <i>values</i>. Returns true if successful; otherwise returns false.</p> <p>This is a low-level method that operates directly on the database and should not be called directly. Use <a href="qsqltablemodel.html#setData">setData</a>() to update values. The model will decide depending on its edit strategy when to modify the database.</p> <p>Note that only values that have the generated-flag set are updated. The generated-flag can be set with <a href="qsqlrecord.html#setGenerated">QSqlRecord.setGenerated</a>() and tested with <a href="qsqlrecord.html#isGenerated">QSqlRecord.isGenerated</a>().</p> <p><b>See also</b> <a href="qsqlrecord.html#isGenerated">QSqlRecord.isGenerated</a>() and <a href="qsqltablemodel.html#setData">setData</a>().</p> <hr /><h2>Qt Signal Documentation</h2><h3 class="fn"><a name="beforeDelete" />void beforeDelete (int)</h3><p>This is the default overload of this signal.</p><p>This signal is emitted by <a href="qsqltablemodel.html#deleteRowFromTable">deleteRowFromTable</a>() before the <i>row</i> is deleted from the currently active database table.</p> <h3 class="fn"><a name="beforeInsert" />void beforeInsert (QSqlRecord&)</h3><p>This is the default overload of this signal.</p><p>This signal is emitted by <a href="qsqltablemodel.html#insertRowIntoTable">insertRowIntoTable</a>() before a new row is inserted into the currently active database table. The values that are about to be inserted are stored in <i>record</i> and can be modified before they will be inserted.</p> <h3 class="fn"><a name="beforeUpdate" />void beforeUpdate (int,QSqlRecord&)</h3><p>This is the default overload of this signal.</p><p>This signal is emitted by <a href="qsqltablemodel.html#updateRowInTable">updateRowInTable</a>() before the <i>row</i> is updated in the currently active database table with the values from <i>record</i>.</p> <p>Note that only values that are marked as generated will be updated. The generated flag can be set with <a href="qsqlrecord.html#setGenerated">QSqlRecord.setGenerated</a>() and checked with <a href="qsqlrecord.html#isGenerated">QSqlRecord.isGenerated</a>().</p> <p><b>See also</b> <a href="qsqlrecord.html#isGenerated">QSqlRecord.isGenerated</a>().</p> <h3 class="fn"><a name="primeInsert" />void primeInsert (int,QSqlRecord&)</h3><p>This is the default overload of this signal.</p><p>This signal is emitted by <a href="qsqltablemodel.html#insertRows">insertRows</a>(), when an insertion is initiated in the given <i>row</i> of the currently active database table. The <i>record</i> parameter can be written to (since it is a reference), for example to populate some fields with default values.</p> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.10.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>