Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 1dd17e0d683ef79b4bb6872bbf359d7f > files > 7360

qt4-doc-4.8.7-26.2.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- cachedtable.qdoc -->
  <title>Qt 4.8: Cached Table Example</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Cached Table Example</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#tableeditor-class-definition">TableEditor Class Definition</a></li>
<li class="level1"><a href="#tableeditor-class-implementation">TableEditor Class Implementation</a></li>
</ul>
</div>
<h1 class="title">Cached Table Example</h1>
<span class="subtitle"></span>
<!-- $$$sql/cachedtable-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="sql-cachedtable-tableeditor-cpp.html">sql/cachedtable/tableeditor.cpp</a></li>
<li><a href="sql-cachedtable-tableeditor-h.html">sql/cachedtable/tableeditor.h</a></li>
<li><a href="sql-cachedtable-main-cpp.html">sql/cachedtable/main.cpp</a></li>
<li><a href="sql-cachedtable-cachedtable-pro.html">sql/cachedtable/cachedtable.pro</a></li>
</ul>
<p>The Cached Table example shows how a table view can be used to access a database, caching any changes to the data until the user explicitly submits them using a push button.<p class="centerAlign"><img src="images/cachedtable-example.png" alt="" /></p><p>The example consists of a single class, <tt>TableEditor</tt>, which is a custom dialog widget that allows the user to modify data stored in a database. We will first review the class definiton and how to use the class, then we will take a look at the implementation.</p>
<a name="tableeditor-class-definition"></a>
<h2>TableEditor Class Definition</h2>
<p>The <tt>TableEditor</tt> class inherits <a href="qdialog.html">QDialog</a> making the table editor widget a top-level dialog window.</p>
<pre class="cpp"> <span class="keyword">class</span> TableEditor : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     TableEditor(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>tableName<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> submit();

 <span class="keyword">private</span>:
     <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>submitButton;
     <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>revertButton;
     <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>quitButton;
     <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span> <span class="operator">*</span>buttonBox;
     <span class="type"><a href="qsqltablemodel.html">QSqlTableModel</a></span> <span class="operator">*</span>model;
 };</pre>
<p>The <tt>TableEditor</tt> constructor takes two arguments: The first is a pointer to the parent widget and is passed on to the base class constructor. The other is a reference to the database table the <tt>TableEditor</tt> object will operate on.</p>
<p>Note the <a href="qsqltablemodel.html">QSqlTableModel</a> variable declaration: As we will see in this example, the <a href="qsqltablemodel.html">QSqlTableModel</a> class can be used to provide data to view classes such as <a href="qtableview.html">QTableView</a>. The <a href="qsqltablemodel.html">QSqlTableModel</a> class provides an editable data model making it possible to read and write database records from a single table. It is build on top of the lower-level <a href="qsqlquery.html">QSqlQuery</a> class which provides means of executing and manipulating SQL statements.</p>
<p>We are also going to show how a table view can be used to cache any changes to the data until the user explicitly requests to submit them. For that reason we need to declare a <tt>submit()</tt> slot in additon to the model and the editor's buttons.</p>
<table class="generic" width="100%">
 <thead><tr class="qt-style"><th >Connecting to a Database</th></tr></thead>
<tr valign="top" class="odd"><td >Before we can use the <tt>TableEditor</tt> class, we must create a connection to the database containing the table we want to edit:<pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
 {
     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);
     <span class="keyword">if</span> (<span class="operator">!</span>createConnection())
         <span class="keyword">return</span> <span class="number">1</span>;

     TableEditor editor(<span class="string">&quot;person&quot;</span>);
 <span class="preprocessor">#if defined(Q_OS_SYMBIAN)</span>
     editor<span class="operator">.</span>showMaximized();
 <span class="preprocessor">#else</span>
     editor<span class="operator">.</span>show();
 <span class="preprocessor">#endif</span>
     <span class="keyword">return</span> app<span class="operator">.</span>exec();
 }</pre>
<p>The <tt>createConnection()</tt> function is a helper function provided for convenience. It is defined in the <tt>connection.h</tt> file which is located in the <tt>sql</tt> example directory (all the examples in the <tt>sql</tt> directory use this function to connect to a database).</p>
<pre class="cpp"> <span class="keyword">static</span> <span class="type">bool</span> createConnection()
 {
     <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span> db <span class="operator">=</span> <span class="type"><a href="qsqldatabase.html">QSqlDatabase</a></span><span class="operator">::</span>addDatabase(<span class="string">&quot;QSQLITE&quot;</span>);
     db<span class="operator">.</span>setDatabaseName(<span class="string">&quot;:memory:&quot;</span>);
     <span class="keyword">if</span> (<span class="operator">!</span>db<span class="operator">.</span>open()) {
         <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>critical(<span class="number">0</span><span class="operator">,</span> qApp<span class="operator">-</span><span class="operator">&gt;</span>tr(<span class="string">&quot;Cannot open database&quot;</span>)<span class="operator">,</span>
             qApp<span class="operator">-</span><span class="operator">&gt;</span>tr(<span class="string">&quot;Unable to establish a database connection.\n&quot;</span>
                      <span class="string">&quot;This example needs SQLite support. Please read &quot;</span>
                      <span class="string">&quot;the Qt SQL driver documentation for information how &quot;</span>
                      <span class="string">&quot;to build it.\n\n&quot;</span>
                      <span class="string">&quot;Click Cancel to exit.&quot;</span>)<span class="operator">,</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Cancel);
         <span class="keyword">return</span> <span class="keyword">false</span>;
     }

     <span class="type"><a href="qsqlquery.html">QSqlQuery</a></span> query;
     query<span class="operator">.</span>exec(<span class="string">&quot;create table person (id int primary key, &quot;</span>
                <span class="string">&quot;firstname varchar(20), lastname varchar(20))&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(101, 'Danny', 'Young')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(102, 'Christine', 'Holand')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(103, 'Lars', 'Gordon')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(104, 'Roberto', 'Robitaille')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into person values(105, 'Maria', 'Papadopoulos')&quot;</span>);

     query<span class="operator">.</span>exec(<span class="string">&quot;create table offices (id int primary key,&quot;</span>
                                              <span class="string">&quot;imagefile int,&quot;</span>
                                              <span class="string">&quot;location varchar(20),&quot;</span>
                                              <span class="string">&quot;country varchar(20),&quot;</span>
                                              <span class="string">&quot;description varchar(100))&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into offices &quot;</span>
                <span class="string">&quot;values(0, 0, 'Oslo', 'Norway',&quot;</span>
                <span class="string">&quot;'Oslo is home to more than 500 000 citizens and has a &quot;</span>
                <span class="string">&quot;lot to offer.It has been called \&quot;The city with the big &quot;</span>
                <span class="string">&quot;heart\&quot; and this is a nickname we are happy to live up to.')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into offices &quot;</span>
                <span class="string">&quot;values(1, 1, 'Brisbane', 'Australia',&quot;</span>
                <span class="string">&quot;'Brisbane is the capital of Queensland, the Sunshine State, &quot;</span>
                <span class="string">&quot;where it is beautiful one day, perfect the next.  &quot;</span>
                <span class="string">&quot;Brisbane is Australia''s 3rd largest city, being home &quot;</span>
                <span class="string">&quot;to almost 2 million people.')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into offices &quot;</span>
                <span class="string">&quot;values(2, 2, 'Redwood City', 'US',&quot;</span>
                <span class="string">&quot;'You find Redwood City in the heart of the Bay Area &quot;</span>
                <span class="string">&quot;just north of Silicon Valley. The largest nearby city is &quot;</span>
                <span class="string">&quot;San Jose which is the third largest city in California &quot;</span>
                <span class="string">&quot;and the 10th largest in the US.')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into offices &quot;</span>
                <span class="string">&quot;values(3, 3, 'Berlin', 'Germany',&quot;</span>
                <span class="string">&quot;'Berlin, the capital of Germany is dynamic, cosmopolitan &quot;</span>
                <span class="string">&quot;and creative, allowing for every kind of lifestyle. &quot;</span>
                <span class="string">&quot;East meets West in the metropolis at the heart of a &quot;</span>
                <span class="string">&quot;changing Europe.')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into offices &quot;</span>
                <span class="string">&quot;values(4, 4, 'Munich', 'Germany',&quot;</span>
                <span class="string">&quot;'Several technology companies are represented in Munich, &quot;</span>
                <span class="string">&quot;and the city is often called the \&quot;Bavarian Silicon Valley\&quot;. &quot;</span>
                <span class="string">&quot;The exciting city is also filled with culture, &quot;</span>
                <span class="string">&quot;art and music. ')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into offices &quot;</span>
                <span class="string">&quot;values(5, 5, 'Beijing', 'China',&quot;</span>
                <span class="string">&quot;'Beijing as a capital city has more than 3000 years of &quot;</span>
                <span class="string">&quot;history. Today the city counts 12 million citizens, and &quot;</span>
                <span class="string">&quot;is the political, economic and cultural centre of China.')&quot;</span>);

     query<span class="operator">.</span>exec(<span class="string">&quot;create table images (locationid int, file varchar(20))&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into images values(0, 'images/oslo.png')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into images values(1, 'images/brisbane.png')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into images values(2, 'images/redwood.png')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into images values(3, 'images/berlin.png')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into images values(4, 'images/munich.png')&quot;</span>);
     query<span class="operator">.</span>exec(<span class="string">&quot;insert into images values(5, 'images/beijing.png')&quot;</span>);

     <span class="keyword">return</span> <span class="keyword">true</span>;
 }</pre>
<p>The <tt>createConnection</tt> function opens a connection to an in-memory SQLITE database and creates a test table. If you want to use another database, simply modify this function's code.</p>
</td></tr>
</table>
<a name="tableeditor-class-implementation"></a>
<h2>TableEditor Class Implementation</h2>
<p>The class implementation consists of only two functions, the constructor and the <tt>submit()</tt> slot. In the constructor we create and customize the data model and the various window elements:</p>
<pre class="cpp"> TableEditor<span class="operator">::</span>TableEditor(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>tableName<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
 {
     model <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsqltablemodel.html">QSqlTableModel</a></span>(<span class="keyword">this</span>);
     model<span class="operator">-</span><span class="operator">&gt;</span>setTable(tableName);
     model<span class="operator">-</span><span class="operator">&gt;</span>setEditStrategy(<span class="type"><a href="qsqltablemodel.html">QSqlTableModel</a></span><span class="operator">::</span>OnManualSubmit);
     model<span class="operator">-</span><span class="operator">&gt;</span>select();

     model<span class="operator">-</span><span class="operator">&gt;</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">&quot;ID&quot;</span>));
     model<span class="operator">-</span><span class="operator">&gt;</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">&quot;First name&quot;</span>));
     model<span class="operator">-</span><span class="operator">&gt;</span>setHeaderData(<span class="number">2</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">&quot;Last name&quot;</span>));</pre>
<p>First we create the data model and set the SQL database table we want the model to operate on. Note that the <a href="qsqltablemodel.html#setTable">QSqlTableModel::setTable</a>() function does not select data from the table; it only fetches its field information. For that reason we call the <a href="qsqltablemodel.html#select">QSqlTableModel::select</a>() function later on, populating the model with data from the table. The selection can be customized by specifying filters and sort conditions (see the <a href="qsqltablemodel.html">QSqlTableModel</a> class documentation for more details).</p>
<p>We also set the model's edit strategy. The edit strategy dictates when the changes done by the user in the view, are actually applied to the database. Since we want to cache the changes in the table view (i.e&#x2e; in the model) until the user explicitly submits them, we choose the <a href="qsqltablemodel.html#EditStrategy-enum">QSqlTableModel::OnManualSubmit</a> strategy. The alternatives are <a href="qsqltablemodel.html#EditStrategy-enum">QSqlTableModel::OnFieldChange</a> and <a href="qsqltablemodel.html#EditStrategy-enum">QSqlTableModel::OnRowChange</a>.</p>
<p>Finally, we set up the labels displayed in the view header using the <a href="qsqlquerymodel.html#setHeaderData">setHeaderData()</a> function that the model inherits from the <a href="qsqlquerymodel.html">QSqlQueryModel</a> class.</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">&gt;</span>setModel(model);
     view<span class="operator">-</span><span class="operator">&gt;</span>resizeColumnsToContents();</pre>
<p>Then we create a table view. The <a href="qtableview.html">QTableView</a> class provides a default model/view implementation of a table view, i.e&#x2e; it implements a table view that displays items from a model. It also allows the user to edit the items, storing the changes in the model. To create a read only view, set the proper flag using the <a href="qabstractitemview.html#editTriggers-prop">editTriggers</a> property the view inherits from the <a href="qabstractitemview.html">QAbstractItemView</a> class.</p>
<p>To make the view present our data, we pass our model to the view using the <a href="qabstractitemview.html#setModel">setModel()</a> function.</p>
<pre class="cpp">     submitButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;Submit&quot;</span>));
     submitButton<span class="operator">-</span><span class="operator">&gt;</span>setDefault(<span class="keyword">true</span>);
     revertButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;&amp;Revert&quot;</span>));
     quitButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;Quit&quot;</span>));

     buttonBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Vertical);
     buttonBox<span class="operator">-</span><span class="operator">&gt;</span>addButton(submitButton<span class="operator">,</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>ActionRole);
     buttonBox<span class="operator">-</span><span class="operator">&gt;</span>addButton(revertButton<span class="operator">,</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>ActionRole);
     buttonBox<span class="operator">-</span><span class="operator">&gt;</span>addButton(quitButton<span class="operator">,</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>RejectRole);</pre>
<p>The <tt>TableEditor</tt>'s buttons are regular <a href="qpushbutton.html">QPushButton</a> objects. We add them to a button box to ensure that the buttons are presented in a layout that is appropriate to the current widget style. The rationale for this is that dialogs and message boxes typically present buttons in a layout that conforms to the interface guidelines for that platform. Invariably, different platforms have different layouts for their dialogs. <a href="qdialogbuttonbox.html">QDialogButtonBox</a> allows a developer to add buttons to it and will automatically use the appropriate layout for the user's desktop environment.</p>
<p>Most buttons for a dialog follow certain roles. When adding a button to a button box using the <a href="qdialogbuttonbox.html">addButton()</a> function, the button's role must be specified using the <a href="qdialogbuttonbox.html#ButtonRole-enum">QDialogButtonBox::ButtonRole</a> enum. Alternatively, <a href="qdialogbuttonbox.html">QDialogButtonBox</a> provides several standard buttons (e.g&#x2e; <b>OK</b>, <b>Cancel</b>, <b>Save</b>) that you can use. They exist as flags so you can OR them together in the constructor.</p>
<pre class="cpp">     connect(submitButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(submit()));
     connect(revertButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> model<span class="operator">,</span> SLOT(revertAll()));
     connect(quitButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(close()));</pre>
<p>We connect the <b>Quit</b> button to the table editor's <a href="qwidget.html#close">close()</a> slot, and the <b>Submit</b> button to our private <tt>submit()</tt> slot. The latter slot will take care of the data transactions. Finally, we connect the <b>Revert</b> button to our model's <a href="qsqltablemodel.html#revertAll">revertAll()</a> slot, reverting all pending changes (i.e&#x2e;, restoring the original data).</p>
<pre class="cpp">     <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span>;
     mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(view);
     mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(buttonBox);
     setLayout(mainLayout);

     setWindowTitle(tr(<span class="string">&quot;Cached Table&quot;</span>));
 }</pre>
<p>In the end we add the button box and the table view to a layout, install the layout on the table editor widget, and set the editor's window title.</p>
<pre class="cpp"> <span class="type">void</span> TableEditor<span class="operator">::</span>submit()
 {
     model<span class="operator">-</span><span class="operator">&gt;</span>database()<span class="operator">.</span>transaction();
     <span class="keyword">if</span> (model<span class="operator">-</span><span class="operator">&gt;</span>submitAll()) {
         model<span class="operator">-</span><span class="operator">&gt;</span>database()<span class="operator">.</span>commit();
     } <span class="keyword">else</span> {
         model<span class="operator">-</span><span class="operator">&gt;</span>database()<span class="operator">.</span>rollback();
         <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Cached Table&quot;</span>)<span class="operator">,</span>
                              tr(<span class="string">&quot;The database reported an error: %1&quot;</span>)
                              <span class="operator">.</span>arg(model<span class="operator">-</span><span class="operator">&gt;</span>lastError()<span class="operator">.</span>text()));
     }
 }</pre>
<p>The <tt>submit()</tt> slot is called whenever the users hit the <b>Submit</b> button to save their changes.</p>
<p>First, we begin a transaction on the database using the <a href="qsqldatabase.html#transaction">QSqlDatabase::transaction</a>() function. A database transaction is a unit of interaction with a database management system or similar system that is treated in a coherent and reliable way independent of other transactions. A pointer to the used database can be obtained using the <a href="qsqltablemodel.html#database">QSqlTableModel::database</a>() function.</p>
<p>Then, we try to submit all the pending changes, i.e&#x2e; the model's modified items. If no error occurs, we commit the transaction to the database using the <a href="qsqldatabase.html#commit">QSqlDatabase::commit</a>() function (note that on some databases, this function will not work if there is an active <a href="qsqlquery.html">QSqlQuery</a> on the database). Otherwise we perform a rollback of the transaction using the <a href="qsqldatabase.html#rollback">QSqlDatabase::rollback</a>() function and post a warning to the user.</p>
<table class="generic" width="100%">
 <tr valign="top" class="odd"><td ><b>See also:</b><p>A complete list of Qt's SQL <a href="database.html">Database Classes</a>, and the <a href="model-view-programming.html">Model/View Programming</a> documentation.</p>
</td></tr>
</table>
</div>
<!-- @@@sql/cachedtable -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2015 The Qt Company Ltd.
      Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      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.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      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. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>