Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 4068

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- spinboxdelegate.qdoc -->
  <title>Spin Box Delegate Example | Qt Widgets 5.9</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.9</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="examples-itemviews.html">Item Views Examples</a></td><td >Spin Box Delegate Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#spinboxdelegate-class-definition">SpinBoxDelegate Class Definition</a></li>
<li class="level1"><a href="#spinboxdelegate-class-implementation">SpinBoxDelegate Class Implementation</a></li>
<li class="level1"><a href="#the-main-function">The Main Function</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Spin Box Delegate Example</h1>
<span class="subtitle"></span>
<!-- $$$itemviews/spinboxdelegate-description -->
<div class="descr"> <a name="details"></a>
<p>The model/view framework provides a standard delegate that is used by default with the standard view classes. For most purposes, the selection of editor widgets available through this delegate is sufficient for editing text, boolean values, and other simple data types. However, for specific data types, it is sometimes necessary to use a custom delegate to either display the data in a specific way, or allow the user to edit it with a custom control.</p>
<p class="centerAlign"><img src="images/spinboxdelegate-example.png" alt="" /></p><p>This concepts behind this example are covered in the <a href="model-view-programming.html#delegate-classes">Delegate Classes</a> chapter of the <a href="model-view-programming.html">Model/View Programming</a> overview.</p>
<a name="spinboxdelegate-class-definition"></a>
<h2 id="spinboxdelegate-class-definition">SpinBoxDelegate Class Definition</h2>
<p>The definition of the delegate is as follows:</p>
<pre class="cpp">

  <span class="keyword">class</span> SpinBoxDelegate : <span class="keyword">public</span> <span class="type"><a href="qstyleditemdelegate.html">QStyledItemDelegate</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      SpinBoxDelegate(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

      <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>createEditor(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a></span> <span class="operator">&amp;</span>option<span class="operator">,</span>
                            <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span> override;

      <span class="type">void</span> setEditorData(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>editor<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span> override;
      <span class="type">void</span> setModelData(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>editor<span class="operator">,</span> <span class="type"><a href="../qtcore/qabstractitemmodel.html">QAbstractItemModel</a></span> <span class="operator">*</span>model<span class="operator">,</span>
                        <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span> override;

      <span class="type">void</span> updateEditorGeometry(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>editor<span class="operator">,</span>
          <span class="keyword">const</span> <span class="type"><a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a></span> <span class="operator">&amp;</span>option<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span> override;
  };

</pre>
<p>The delegate class declares only those functions that are needed to create an editor widget, display it at the correct location in a view, and communicate with a model. Custom delegates can also provide their own painting code by reimplementing the <code>paintEvent()</code> function. Furthermore it is also possible to reuse (and avoid deleting) the editor widget by reimplementing the <i>destroyEditor()</i> function. A reused widget could be a mutable member created in the constructor and deleted in the destructor.</p>
<a name="spinboxdelegate-class-implementation"></a>
<h2 id="spinboxdelegate-class-implementation">SpinBoxDelegate Class Implementation</h2>
<p>Delegates are often stateless. The constructor only needs to call the base class's constructor with the parent <a href="../qtcore/qobject.html">QObject</a> as its argument:</p>
<pre class="cpp">

  SpinBoxDelegate<span class="operator">::</span>SpinBoxDelegate(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qstyleditemdelegate.html">QStyledItemDelegate</a></span>(parent)
  {
  }

</pre>
<p>Since the delegate is a subclass of <a href="qstyleditemdelegate.html">QStyledItemDelegate</a>, the data it retrieves from the model is displayed in a default style, and we do not need to provide a custom <code>paintEvent()</code>.</p>
<p>The <code>createEditor()</code> function returns an editor widget, in this case a spin box that restricts values from the model to integers from 0 to 100 inclusive.</p>
<pre class="cpp">

  <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>SpinBoxDelegate<span class="operator">::</span>createEditor(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent<span class="operator">,</span>
      <span class="keyword">const</span> <span class="type"><a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a></span> <span class="operator">&amp;</span><span class="comment">/* option */</span><span class="operator">,</span>
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span><span class="comment">/* index */</span>) <span class="keyword">const</span>
  {
      <span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>editor <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qspinbox.html">QSpinBox</a></span>(parent);
      editor<span class="operator">-</span><span class="operator">&gt;</span>setFrame(<span class="keyword">false</span>);
      editor<span class="operator">-</span><span class="operator">&gt;</span>setMinimum(<span class="number">0</span>);
      editor<span class="operator">-</span><span class="operator">&gt;</span>setMaximum(<span class="number">100</span>);

      <span class="keyword">return</span> editor;
  }

</pre>
<p>We install an event filter on the spin box to ensure that it behaves in a way that is consistent with other delegates. The implementation for the event filter is provided by the base class.</p>
<p>The <code>setEditorData()</code> function reads data from the model, converts it to an integer value, and writes it to the editor widget.</p>
<pre class="cpp">

  <span class="type">void</span> SpinBoxDelegate<span class="operator">::</span>setEditorData(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>editor<span class="operator">,</span>
                                      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span>
  {
      <span class="type">int</span> value <span class="operator">=</span> index<span class="operator">.</span>model()<span class="operator">-</span><span class="operator">&gt;</span>data(index<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>EditRole)<span class="operator">.</span>toInt();

      <span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>spinBox <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qspinbox.html">QSpinBox</a></span><span class="operator">*</span><span class="operator">&gt;</span>(editor);
      spinBox<span class="operator">-</span><span class="operator">&gt;</span>setValue(value);
  }

</pre>
<p>Since the view treats delegates as ordinary <a href="qwidget.html">QWidget</a> instances, we have to use a static cast before we can set the value in the spin box.</p>
<p>The <code>setModelData()</code> function reads the contents of the spin box, and writes it to the model.</p>
<pre class="cpp">

  <span class="type">void</span> SpinBoxDelegate<span class="operator">::</span>setModelData(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>editor<span class="operator">,</span> <span class="type"><a href="../qtcore/qabstractitemmodel.html">QAbstractItemModel</a></span> <span class="operator">*</span>model<span class="operator">,</span>
                                     <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span>
  {
      <span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>spinBox <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qspinbox.html">QSpinBox</a></span><span class="operator">*</span><span class="operator">&gt;</span>(editor);
      spinBox<span class="operator">-</span><span class="operator">&gt;</span>interpretText();
      <span class="type">int</span> value <span class="operator">=</span> spinBox<span class="operator">-</span><span class="operator">&gt;</span>value();

      model<span class="operator">-</span><span class="operator">&gt;</span>setData(index<span class="operator">,</span> value<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>EditRole);
  }

</pre>
<p>We call <a href="qabstractspinbox.html#interpretText">interpretText()</a> to make sure that we obtain the most up-to-date value in the spin box.</p>
<p>The <code>updateEditorGeometry()</code> function updates the editor widget's geometry using the information supplied in the style option. This is the minimum that the delegate must do in this case.</p>
<pre class="cpp">

  <span class="type">void</span> SpinBoxDelegate<span class="operator">::</span>updateEditorGeometry(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>editor<span class="operator">,</span>
      <span class="keyword">const</span> <span class="type"><a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a></span> <span class="operator">&amp;</span>option<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span><span class="comment">/* index */</span>) <span class="keyword">const</span>
  {
      editor<span class="operator">-</span><span class="operator">&gt;</span>setGeometry(option<span class="operator">.</span>rect);
  }

</pre>
<p>More complex editor widgets may divide the rectangle available in <code>option.rect</code> between different child widgets if required.</p>
<a name="the-main-function"></a>
<h2 id="the-main-function">The Main Function</h2>
<p>This example is written in a slightly different way to many of the other examples supplied with Qt. To demonstrate the use of a custom editor widget in a standard view, it is necessary to set up a model containing some arbitrary data and a view to display it.</p>
<p>We set up the application in the normal way, construct a standard item model to hold some data, set up a table view to use the data in the model, and construct a custom delegate to use for editing:</p>
<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="type"><a href="../qtgui/qstandarditemmodel.html">QStandardItemModel</a></span> model(<span class="number">4</span><span class="operator">,</span> <span class="number">2</span>);
      <span class="type"><a href="qtableview.html">QTableView</a></span> tableView;
      tableView<span class="operator">.</span>setModel(<span class="operator">&amp;</span>model);

      SpinBoxDelegate delegate;
      tableView<span class="operator">.</span>setItemDelegate(<span class="operator">&amp;</span>delegate);

</pre>
<p>The table view is informed about the delegate, and will use it to display each of the items. Since the delegate is a subclass of <a href="qstyleditemdelegate.html">QStyledItemDelegate</a>, each cell in the table will be rendered using standard painting operations.</p>
<p>We insert some arbitrary data into the model for demonstration purposes:</p>
<pre class="cpp">

      <span class="keyword">for</span> (<span class="type">int</span> row <span class="operator">=</span> <span class="number">0</span>; row <span class="operator">&lt;</span> <span class="number">4</span>; <span class="operator">+</span><span class="operator">+</span>row) {
          <span class="keyword">for</span> (<span class="type">int</span> column <span class="operator">=</span> <span class="number">0</span>; column <span class="operator">&lt;</span> <span class="number">2</span>; <span class="operator">+</span><span class="operator">+</span>column) {
              <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> index <span class="operator">=</span> model<span class="operator">.</span>index(row<span class="operator">,</span> column<span class="operator">,</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span>());
              model<span class="operator">.</span>setData(index<span class="operator">,</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>((row <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> (column <span class="operator">+</span> <span class="number">1</span>)));
          }
      }

</pre>
<p>Finally, the table view is displayed with a window title, and we start the application's event loop:</p>
<pre class="cpp">

      tableView<span class="operator">.</span>setWindowTitle(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">::</span>tr(<span class="string">&quot;Spin Box Delegate&quot;</span>));
      tableView<span class="operator">.</span>show();
      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<p>Each of the cells in the table can now be edited in the usual way, but the spin box ensures that the data returned to the model is always constrained by the values allowed by the spin box delegate.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-itemviews-spinboxdelegate-delegate-cpp.html">itemviews/spinboxdelegate/delegate.cpp</a></li>
<li><a href="qtwidgets-itemviews-spinboxdelegate-delegate-h.html">itemviews/spinboxdelegate/delegate.h</a></li>
<li><a href="qtwidgets-itemviews-spinboxdelegate-main-cpp.html">itemviews/spinboxdelegate/main.cpp</a></li>
<li><a href="qtwidgets-itemviews-spinboxdelegate-spinboxdelegate-pro.html">itemviews/spinboxdelegate/spinboxdelegate.pro</a></li>
</ul>
</div>
<!-- @@@itemviews/spinboxdelegate -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 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>