1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
<?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>QSqlRelationalTableModel 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">QSqlRelationalTableModel Class Reference<br /><sup><sup>[<a href="qtsql.html">QtSql</a> module]</sup></sup></h1><p>The QSqlRelationalTableModel class provides an editable data
model for a single database table, with foreign key support.
<a href="#details">More...</a></p>
<p>Inherits <a href="qsqltablemodel.html">QSqlTableModel</a>.</p><h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qsqlrelationaltablemodel.html#JoinMode-enum">JoinMode</a></b> { InnerJoin, LeftJoin }</li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#QSqlRelationalTableModel">__init__</a></b> (<i>self</i>, QObject <i>parent</i> = None, QSqlDatabase <i>db</i> = QSqlDatabase())</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#clear">clear</a></b> (<i>self</i>)</li><li><div class="fn" />QVariant <b><a href="qsqlrelationaltablemodel.html#data">data</a></b> (<i>self</i>, QModelIndex <i>index</i>, int <i>role</i> = Qt.DisplayRole)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.html#insertRowIntoTable">insertRowIntoTable</a></b> (<i>self</i>, QSqlRecord <i>values</i>)</li><li><div class="fn" />QString <b><a href="qsqlrelationaltablemodel.html#orderByClause">orderByClause</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlRelation <b><a href="qsqlrelationaltablemodel.html#relation">relation</a></b> (<i>self</i>, int <i>column</i>)</li><li><div class="fn" />QSqlTableModel <b><a href="qsqlrelationaltablemodel.html#relationModel">relationModel</a></b> (<i>self</i>, int <i>column</i>)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.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" /><b><a href="qsqlrelationaltablemodel.html#revertRow">revertRow</a></b> (<i>self</i>, int <i>row</i>)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.html#select">select</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqlrelationaltablemodel.html#selectStatement">selectStatement</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.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="qsqlrelationaltablemodel.html#setJoinMode">setJoinMode</a></b> (<i>self</i>, JoinMode <i>joinMode</i>)</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a></b> (<i>self</i>, int <i>column</i>, QSqlRelation <i>relation</i>)</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#setTable">setTable</a></b> (<i>self</i>, QString <i>tableName</i>)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.html#updateRowInTable">updateRowInTable</a></b> (<i>self</i>, int <i>row</i>, QSqlRecord <i>values</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSqlRelationalTableModel class provides an editable data
model for a single database table, with foreign key support.</p>
<p>QSqlRelationalTableModel acts like <a href="qsqltablemodel.html">QSqlTableModel</a>, but allows columns to be
set as foreign keys into other database tables.</p>
<table class="generic">
<tr class="odd" valign="top">
<td><img alt="" src="images/noforeignkeys.png" /></td>
<td><img alt="" src="images/foreignkeys.png" /></td>
</tr>
</table>
<p>The screenshot on the left shows a plain <a href="qsqltablemodel.html">QSqlTableModel</a> in a <a href="qtableview.html">QTableView</a>. Foreign keys (<tt>city</tt> and
<tt>country</tt>) aren't resolved to human-readable values. The
screenshot on the right shows a QSqlRelationalTableModel, with
foreign keys resolved into human-readable text strings.</p>
<p>The following code snippet shows how the
QSqlRelationalTableModel was set up:</p>
<pre class="cpp">
model<span class="operator">-</span><span class="operator">></span>setTable(<span class="string">"employee"</span>);
model<span class="operator">-</span><span class="operator">></span>setRelation(<span class="number">2</span><span class="operator">,</span> <span class="type"><a href="qsqlrelation.html">QSqlRelation</a></span>(<span class="string">"city"</span><span class="operator">,</span> <span class="string">"id"</span><span class="operator">,</span> <span class="string">"name"</span>));
model<span class="operator">-</span><span class="operator">></span>setRelation(<span class="number">3</span><span class="operator">,</span> <span class="type"><a href="qsqlrelation.html">QSqlRelation</a></span>(<span class="string">"country"</span><span class="operator">,</span> <span class="string">"id"</span><span class="operator">,</span> <span class="string">"name"</span>));
</pre>
<p>The <a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a>()
function calls establish a relationship between two tables. The
first call specifies that column 2 in table <tt>employee</tt> is a
foreign key that maps with field <tt>id</tt> of table
<tt>city</tt>, and that the view should present the <tt>city</tt>'s
<tt>name</tt> field to the user. The second call does something
similar with column 3.</p>
<p>If you use a read-write QSqlRelationalTableModel, you probably
want to use <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> on the
view. Unlike the default delegate, <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> provides a
combobox for fields that are foreign keys into other tables. To use
the class, simply call <a href="qabstractitemview.html#setItemDelegate">QAbstractItemView.setItemDelegate</a>()
on the view with an instance of <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a>:</p>
<pre class="cpp">
<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>setItemDelegate(<span class="keyword">new</span> <span class="type"><a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a></span>(view));
</pre>
<p>The <a href="sql-relationaltablemodel.html">sql/relationaltablemodel</a>
example illustrates how to use QSqlRelationalTableModel in
conjunction with <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> to provide
tables with foreigh key support.</p>
<p class="centerAlign"><img alt="" src="images/relationaltable.png" /></p>
<p>Notes:</p>
<ul>
<li>The table must have a primary key declared.</li>
<li>The table's primary key may not contain a relation to another
table.</li>
<li>If a relational table contains keys that refer to non-existent
rows in the referenced table, the rows containing the invalid keys
will not be exposed through the model. The user or the database is
responsible for keeping referential integrity.</li>
<li>If a relation's display column name is also used as a column
name in the main table, or if it is used as display column name in
more than one relation it will be aliased. The alias is is the
relation's table name and display column name joined by an
underscore (e.g. tablename_columnname). All occurrences of the
duplicate display column name are aliased when duplication is
detected, but no aliasing is done to the column names in the main
table. The aliasing doesn't affect <a href="qsqlrelation.html">QSqlRelation</a>, so <a href="qsqlrelation.html#displayColumn">QSqlRelation.displayColumn</a>()
will return the original display column name, but <a href="qsqlrecord.html#fieldName">QSqlRecord.fieldName</a>() will return
aliases.</li>
<li>When using <a href="qsqlrelationaltablemodel.html#setData">setData</a>() the role
should always be <a href="qt.html#ItemDataRole-enum">Qt.EditRole</a>, and when using
<a href="qsqlrelationaltablemodel.html#data">data</a>() the role
should always be <a href="qt.html#ItemDataRole-enum">Qt.DisplayRole</a>.</li>
</ul>
<hr /><h2>Type Documentation</h2><h3 class="fn"><a name="JoinMode-enum" />QSqlRelationalTableModel.JoinMode</h3><p>This enum specifies the type of mode to use when joining two
tables.</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>QSqlRelationalTableModel.InnerJoin</tt></td>
<td class="topAlign"><tt>0</tt></td>
<td class="topAlign">Inner join mode, return rows when there is at
least one match in both tables.</td>
</tr>
<tr>
<td class="topAlign">
<tt>QSqlRelationalTableModel.LeftJoin</tt></td>
<td class="topAlign"><tt>1</tt></td>
<td class="topAlign">Left join mode, returns all rows from the left
table (table_name1), even if there are no matches in the right
table (table_name2).</td>
</tr>
</table>
<p>This enum was introduced or modified in Qt 4.8.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#setJoinMode">QSqlRelationalTableModel.setJoinMode</a>().</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSqlRelationalTableModel" />QSqlRelationalTableModel.__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="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</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>
<h3 class="fn"><a name="clear" />QSqlRelationalTableModel.clear (<i>self</i>)</h3><p>Reimplemented from <a href="qsqlquerymodel.html#clear">QSqlQueryModel.clear</a>().</p>
<h3 class="fn"><a name="data" />QVariant QSqlRelationalTableModel.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="qsqlrelationaltablemodel.html#setData">setData</a>().</p>
<h3 class="fn"><a name="insertRowIntoTable" />bool QSqlRelationalTableModel.insertRowIntoTable (<i>self</i>, <a href="qsqlrecord.html">QSqlRecord</a> <i>values</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#insertRowIntoTable">QSqlTableModel.insertRowIntoTable</a>().</p>
<h3 class="fn"><a name="orderByClause" />QString QSqlRelationalTableModel.orderByClause (<i>self</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#orderByClause">QSqlTableModel.orderByClause</a>().</p>
<h3 class="fn"><a name="relation" /><a href="qsqlrelation.html">QSqlRelation</a> QSqlRelationalTableModel.relation (<i>self</i>, int <i>column</i>)</h3><p>Returns the relation for the column <i>column</i>, or an invalid
relation if no relation is set.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a>() and
<a href="qsqlrelation.html#isValid">QSqlRelation.isValid</a>().</p>
<h3 class="fn"><a name="relationModel" /><a href="qsqltablemodel.html">QSqlTableModel</a> QSqlRelationalTableModel.relationModel (<i>self</i>, int <i>column</i>)</h3><p>Returns a <a href="qsqltablemodel.html">QSqlTableModel</a>
object for accessing the table for which <i>column</i> is a foreign
key, or 0 if there is no relation for the given <i>column</i>.</p>
<p>The returned object is owned by the <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a>.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a>() and
<a href="qsqlrelationaltablemodel.html#relation">relation</a>().</p>
<h3 class="fn"><a name="removeColumns" />bool QSqlRelationalTableModel.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>
<h3 class="fn"><a name="revertRow" />QSqlRelationalTableModel.revertRow (<i>self</i>, int <i>row</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#revertRow">QSqlTableModel.revertRow</a>().</p>
<h3 class="fn"><a name="select" />bool QSqlRelationalTableModel.select (<i>self</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#select">QSqlTableModel.select</a>().</p>
<h3 class="fn"><a name="selectStatement" />QString QSqlRelationalTableModel.selectStatement (<i>self</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#selectStatement">QSqlTableModel.selectStatement</a>().</p>
<h3 class="fn"><a name="setData" />bool QSqlRelationalTableModel.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 <i>role</i> in the item with the specified
<i>index</i> to the <i>value</i> given. Depending on the edit
strategy, the value might be applied to the database at once, or it
may be 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>For relational columns, <i>value</i> must be the index, not the
display value. The index must also exist in the referenced table,
otherwise the function returns false.</p>
<p><b>See also</b> <a href="qsqltablemodel.html#editStrategy">editStrategy</a>(), <a href="qsqlrelationaltablemodel.html#data">data</a>(), <a href="qsqltablemodel.html#submit">submit</a>(), and <a href="qsqlrelationaltablemodel.html#revertRow">revertRow</a>().</p>
<h3 class="fn"><a name="setJoinMode" />QSqlRelationalTableModel.setJoinMode (<i>self</i>, <a href="qsqlrelationaltablemodel.html#JoinMode-enum">JoinMode</a> <i>joinMode</i>)</h3><p>Sets the SQL join mode to the value given by <i>joinMode</i> to
show or hide rows with NULL foreign keys.</p>
<p>In <a href="qsqlrelationaltablemodel.html#JoinMode-enum">InnerJoin</a> mode
(the default) these rows will not be shown; use the <a href="qsqlrelationaltablemodel.html#JoinMode-enum">LeftJoin</a> mode if
you want to show them.</p>
<p>This function was introduced in Qt 4.8.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#JoinMode-enum">QSqlRelationalTableModel.JoinMode</a>.</p>
<h3 class="fn"><a name="setRelation" />QSqlRelationalTableModel.setRelation (<i>self</i>, int <i>column</i>, <a href="qsqlrelation.html">QSqlRelation</a> <i>relation</i>)</h3><p>Lets the specified <i>column</i> be a foreign index specified by
<i>relation</i>.</p>
<p>Example:</p>
<pre class="cpp">
model<span class="operator">-</span><span class="operator">></span>setTable(<span class="string">"employee"</span>);
model<span class="operator">-</span><span class="operator">></span>setRelation(<span class="number">2</span><span class="operator">,</span> <span class="type"><a href="qsqlrelation.html">QSqlRelation</a></span>(<span class="string">"city"</span><span class="operator">,</span> <span class="string">"id"</span><span class="operator">,</span> <span class="string">"name"</span>));
</pre>
<p>The setRelation() call specifies that column 2 in table
<tt>employee</tt> is a foreign key that maps with field <tt>id</tt>
of table <tt>city</tt>, and that the view should present the
<tt>city</tt>'s <tt>name</tt> field to the user.</p>
<p>Note: The table's primary key may not contain a relation to
another table.</p>
<p><b>See also</b> <a href="qsqlrelationaltablemodel.html#relation">relation</a>().</p>
<h3 class="fn"><a name="setTable" />QSqlRelationalTableModel.setTable (<i>self</i>, QString <i>tableName</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#setTable">QSqlTableModel.setTable</a>().</p>
<h3 class="fn"><a name="updateRowInTable" />bool QSqlRelationalTableModel.updateRowInTable (<i>self</i>, int <i>row</i>, <a href="qsqlrecord.html">QSqlRecord</a> <i>values</i>)</h3><p>Reimplemented from <a href="qsqltablemodel.html#updateRowInTable">QSqlTableModel.updateRowInTable</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.9.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.2</td></tr></table></div></address></body></html>
|