File: qsqlrelationaltablemodel.html

package info (click to toggle)
python-qt4 4.0.1-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 18,632 kB
  • ctags: 2,639
  • sloc: python: 29,409; sh: 5,646; cpp: 3,168; xml: 149; makefile: 109
file content (64 lines) | stat: -rw-r--r-- 11,650 bytes parent folder | download
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
<?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">&#160;&#160;</td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <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>Methods</h3><ul><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#QSqlRelationalTableModel">__init__</a></b> (<i>self</i>, QObject&#160;<i>parent</i>&#160;=&#160;None, QSqlDatabase&#160;<i>db</i>&#160;=&#160;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&#160;<i>item</i>, int&#160;<i>role</i>&#160;=&#160;Qt.DisplayRole)</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&#160;<i>column</i>)</li><li><div class="fn" />QSqlTableModel <b><a href="qsqlrelationaltablemodel.html#relationModel">relationModel</a></b> (<i>self</i>, int&#160;<i>column</i>)</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#revertRow">revertRow</a></b> (<i>self</i>, int&#160;<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&#160;<i>item</i>, QVariant&#160;<i>value</i>, int&#160;<i>role</i>&#160;=&#160;Qt.EditRole)</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a></b> (<i>self</i>, int&#160;<i>column</i>, QSqlRelation&#160;<i>relation</i>)</li><li><div class="fn" /><b><a href="qsqlrelationaltablemodel.html#setTable">setTable</a></b> (<i>self</i>, QString&#160;<i>tableName</i>)</li><li><div class="fn" />bool <b><a href="qsqlrelationaltablemodel.html#updateRowInTable">updateRowInTable</a></b> (<i>self</i>, int&#160;<i>row</i>, QSqlRecord&#160;<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 align="center" border="0" cellpadding="2" cellspacing="1">
<tr bgcolor="#F0F0F0" valign="top">
<td><img src="images/noforeignkeys.png" /></td>
<td><img src="images/foreignkeys.png" /></td>
</tr>
</table>
<br />
<br />
<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>
        model-&gt;setTable("employee");

        model-&gt;setRelation(2, QSqlRelation("city", "id", "name"));
        model-&gt;setRelation(3, QSqlRelation("country", "id", "name"));
</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>
        QTableView *view = new QTableView;
        view-&gt;setModel(model);
        view-&gt;setItemDelegate(new QSqlRelationalDelegate(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 align="center"><img src="images/relationaltable.png" /></p>
<p>Notes:</p>
<ul>
<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>
</ul>
<p>See also <a href="qsqlrelation.html">QSqlRelation</a> and <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a>.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSqlRelationalTableModel" />QSqlRelationalTableModel.__init__ (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None, <a href="qsqldatabase.html">QSqlDatabase</a>&#160;<i>db</i>&#160;=&#160;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><h3 class="fn"><a name="data" /><a href="qvariant.html">QVariant</a> QSqlRelationalTableModel.data (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>item</i>, int&#160;<i>role</i>&#160;=&#160;Qt.DisplayRole)</h3><h3 class="fn"><a name="orderByClause" /><a href="qstring.html">QString</a> QSqlRelationalTableModel.orderByClause (<i>self</i>)</h3><h3 class="fn"><a name="relation" /><a href="qsqlrelation.html">QSqlRelation</a> QSqlRelationalTableModel.relation (<i>self</i>, int&#160;<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>See also <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&#160;<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>See also <a href="qsqlrelationaltablemodel.html#setRelation">setRelation</a>() and <a href="qsqlrelationaltablemodel.html#relation">relation</a>().</p>
<h3 class="fn"><a name="revertRow" />QSqlRelationalTableModel.revertRow (<i>self</i>, int&#160;<i>row</i>)</h3><h3 class="fn"><a name="select" />bool QSqlRelationalTableModel.select (<i>self</i>)</h3><h3 class="fn"><a name="selectStatement" /><a href="qstring.html">QString</a> QSqlRelationalTableModel.selectStatement (<i>self</i>)</h3><h3 class="fn"><a name="setData" />bool QSqlRelationalTableModel.setData (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>item</i>, <a href="qvariant.html">QVariant</a>&#160;<i>value</i>, int&#160;<i>role</i>&#160;=&#160;Qt.EditRole)</h3><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.</p>
<p>Reimplemented from <a href="qabstractitemmodel.html#setData">QAbstractItemModel</a>.</p>
<p>See also <a href="qsqltablemodel.html#editStrategy">editStrategy</a>(), <a href="qabstractitemmodel.html#data">data</a>(), <a href="qsqltablemodel.html#submit">submit</a>(), and <a href="qsqltablemodel.html#revertRow">revertRow</a>().</p>
<h3 class="fn"><a name="setRelation" />QSqlRelationalTableModel.setRelation (<i>self</i>, int&#160;<i>column</i>, <a href="qsqlrelation.html">QSqlRelation</a>&#160;<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>
        model-&gt;setTable("employee");

        model-&gt;setRelation(2, QSqlRelation("city", "id", "name"));
</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>See also <a href="qsqlrelationaltablemodel.html#relation">relation</a>().</p>
<h3 class="fn"><a name="setTable" />QSqlRelationalTableModel.setTable (<i>self</i>, <a href="qstring.html">QString</a>&#160;<i>tableName</i>)</h3><h3 class="fn"><a name="updateRowInTable" />bool QSqlRelationalTableModel.updateRowInTable (<i>self</i>, int&#160;<i>row</i>, <a href="qsqlrecord.html">QSqlRecord</a>&#160;<i>values</i>)</h3><address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td width="25%">PyQt&#160;4.0.1 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.trolltech.com">Trolltech&#160;AS</a> 2006</td><td align="right" width="25%">Qt&#160;4.1.4</td></tr></table></div></address></body></html>