<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- sql-programming.qdoc --> <title>Presenting Data in a Table View | Qt SQL 5.12.2</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtsql-index.html">Qt SQL</a></td><td >Presenting Data in a Table View</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtsql-index.html">Qt 5.12.2 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <link rel="prev" href="sql-model.html" /> <link rel="next" href="sql-forms.html" /> <p class="naviNextPrevious headerNavi"> <a class="prevPage" href="sql-model.html">Using the SQL Model Classes</a> <span class="naviSeparator"> ◦ </span> <a class="nextPage" href="sql-forms.html">Creating Data-Aware Forms</a> </p><p/> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Presenting Data in a Table View</h1> <span class="subtitle"></span> <!-- $$$sql-presenting.html-description --> <div class="descr"> <a name="details"></a> <p>The <a href="qsqlquerymodel.html">QSqlQueryModel</a>, <a href="qsqltablemodel.html">QSqlTableModel</a>, and <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> classes can be used as a data source for Qt's view classes such as <a href="../qtwidgets/qlistview.html">QListView</a>, <a href="../qtwidgets/qtableview.html">QTableView</a>, and <a href="../qtwidgets/qtreeview.html">QTreeView</a>. In practice, <a href="../qtwidgets/qtableview.html">QTableView</a> is by far the most common choice, because an SQL result set is essentially a two-dimensional data structure.</p> <p class="centerAlign"><img src="images/relationaltable.png" alt="A table view displaying a QSqlTableModel" /></p><p>The following example creates a view based on an SQL data model:</p> <pre class="cpp"> <span class="type"><a href="../qtwidgets/qtableview.html">QTableView</a></span> <span class="operator">*</span>view <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/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>show(); </pre> <p>If the model is a read-write model (e.g., <a href="qsqltablemodel.html">QSqlTableModel</a>), the view lets the user edit the fields. You can disable this by calling</p> <pre class="cpp"> view<span class="operator">-</span><span class="operator">></span>setEditTriggers(<span class="type"><a href="../qtwidgets/qabstractitemview.html">QAbstractItemView</a></span><span class="operator">::</span>NoEditTriggers); </pre> <p>You can use the same model as a data source for multiple views. If the user edits the model through one of the views, the other views will reflect the changes immediately. The <a href="qtsql-tablemodel-example.html">Table Model</a> example shows how it works.</p> <p>View classes display a header at the top to label the columns. To change the header texts, call <a href="../qtcore/qabstractitemmodel.html#setHeaderData">setHeaderData()</a> on the model. The header's labels default to the table's field names. For example:</p> <pre class="cpp"> model<span class="operator">-</span><span class="operator">></span>setHeaderData(<span class="number">0</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal<span class="operator">,</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">::</span>tr(<span class="string">"ID"</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="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal<span class="operator">,</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">::</span>tr(<span class="string">"Name"</span>)); model<span class="operator">-</span><span class="operator">></span>setHeaderData(<span class="number">2</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal<span class="operator">,</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">::</span>tr(<span class="string">"City"</span>)); model<span class="operator">-</span><span class="operator">></span>setHeaderData(<span class="number">3</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal<span class="operator">,</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">::</span>tr(<span class="string">"Country"</span>)); </pre> <p><a href="../qtwidgets/qtableview.html">QTableView</a> also has a vertical header on the left with numbers identifying the rows. If you insert rows programmatically using <a href="qsqltablemodel.html#insertRows">QSqlTableModel::insertRows</a>(), the new rows will be marked with an asterisk (*) until they are submitted using <a href="qsqltablemodel.html#submitAll">submitAll()</a> or automatically when the user moves to another record (assuming the <a href="qsqltablemodel.html#EditStrategy-enum">edit strategy</a> is <a href="qsqltablemodel.html#EditStrategy-enum">QSqlTableModel::OnRowChange</a>).</p> <p class="centerAlign"><img src="images/insertrowinmodelview.png" alt="Inserting a row in a model" /></p><p>Likewise, if you remove rows using <a href="qsqltablemodel.html#removeRows">removeRows()</a>, the rows will be marked with an exclamation mark (!) until the change is submitted.</p> <p>The items in the view are rendered using a delegate. The default delegate, <a href="../qtwidgets/qitemdelegate.html">QItemDelegate</a>, handles the most common data types (<code>int</code>, <a href="../qtcore/qstring.html">QString</a>, QImage, etc.). The delegate is also responsible for providing editor widgets (e.g., a combobox) when the user starts editing an item in the view. You can create your own delegates by subclassing <a href="../qtwidgets/qabstractitemdelegate.html">QAbstractItemDelegate</a> or <a href="../qtwidgets/qitemdelegate.html">QItemDelegate</a>. See <a href="../qtwidgets/model-view-programming.html">Model/View Programming</a> for more information.</p> <p><a href="qsqltablemodel.html">QSqlTableModel</a> is optimized to operate on a single table at a time. If you need a read-write model that operates on an arbitrary result set, you can subclass <a href="qsqlquerymodel.html">QSqlQueryModel</a> and reimplement <a href="../qtcore/qabstractitemmodel.html#flags">flags()</a> and <a href="../qtcore/qabstractitemmodel.html#setData">setData()</a> to make it read-write. The following two functions make fields 1 and 2 of a query model editable:</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ItemFlags EditableSqlModel<span class="operator">::</span>flags( <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&</span>index) <span class="keyword">const</span> { <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ItemFlags flags <span class="operator">=</span> <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span><span class="operator">::</span>flags(index); <span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span> <span class="operator">|</span><span class="operator">|</span> index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>) flags <span class="operator">|</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ItemIsEditable; <span class="keyword">return</span> flags; } bool EditableSqlModel<span class="operator">::</span>setData(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&</span>index<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> <span class="operator">&</span>value<span class="operator">,</span> <span class="type">int</span> <span class="comment">/* role */</span>) { <span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator"><</span> <span class="number">1</span> <span class="operator">|</span><span class="operator">|</span> index<span class="operator">.</span>column() <span class="operator">></span> <span class="number">2</span>) <span class="keyword">return</span> <span class="keyword">false</span>; <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> primaryKeyIndex <span class="operator">=</span> <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span><span class="operator">::</span>index(index<span class="operator">.</span>row()<span class="operator">,</span> <span class="number">0</span>); <span class="type">int</span> id <span class="operator">=</span> data(primaryKeyIndex)<span class="operator">.</span>toInt(); clear(); bool ok; <span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) { ok <span class="operator">=</span> setFirstName(id<span class="operator">,</span> value<span class="operator">.</span>toString()); } <span class="keyword">else</span> { ok <span class="operator">=</span> setLastName(id<span class="operator">,</span> value<span class="operator">.</span>toString()); } refresh(); <span class="keyword">return</span> ok; } </pre> <p>The setFirstName() helper function is defined as follows:</p> <pre class="cpp"> bool EditableSqlModel<span class="operator">::</span>setFirstName(<span class="type">int</span> personId<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>firstName) { <span class="type"><a href="qsqlquery.html">QSqlQuery</a></span> query; query<span class="operator">.</span>prepare(<span class="string">"update person set firstname = ? where id = ?"</span>); query<span class="operator">.</span>addBindValue(firstName); query<span class="operator">.</span>addBindValue(personId); <span class="keyword">return</span> query<span class="operator">.</span>exec(); } </pre> <p>The setLastName() function is similar. See the <a href="qtsql-querymodel-example.html">Query Model</a> example for the complete source code.</p> <p>Subclassing a model makes it possible to customize it in many ways: You can provide tooltips for the items, change the background color, provide calculated values, provide different values for viewing and editing, handle null values specially, and more. See <a href="../qtwidgets/model-view-programming.html">Model/View Programming</a> as well as the <a href="../qtwidgets/qabstractitemview.html">QAbstractItemView</a> reference documentation for details.</p> <p>If all you need is to resolve a foreign key to a more human-friendly string, you can use <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a>. For best results, you should also use <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a>, a delegate that provides combobox editors for editing foreign keys.</p> <p class="centerAlign"><img src="images/relationaltable.png" alt="Editing a foreign key in a relational table" /></p><p>The <a href="qtsql-relationaltablemodel-example.html">Relational Table Model</a> example illustrates how to use <a href="qsqlrelationaltablemodel.html">QSqlRelationalTableModel</a> in conjunction with <a href="qsqlrelationaldelegate.html">QSqlRelationalDelegate</a> to provide tables with foreign key support.</p> </div> <!-- @@@sql-presenting.html --> <p class="naviNextPrevious footerNavi"> <a class="prevPage" href="sql-model.html">Using the SQL Model Classes</a> <span class="naviSeparator"> ◦ </span> <a class="nextPage" href="sql-forms.html">Creating Data-Aware Forms</a> </p> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>