Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > d5e62c01ae8d1e579463c6a871dd44bf > files > 4512

qtbase5-doc-5.12.6-2.mga7.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" />
<!-- coloreditorfactory.qdoc -->
  <title>Color Editor Factory Example | Qt Widgets 5.12.6</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="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="examples-itemviews.html">Item Views Examples</a></td><td >Color Editor Factory Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.6 Reference Documentation</a></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="#window-class-implementation">Window Class Implementation</a></li>
<li class="level1"><a href="#colorlisteditor-definition">ColorListEditor Definition</a></li>
<li class="level1"><a href="#colorlisteditor-implementation">ColorListEditor Implementation</a></li>
<li class="level1"><a href="#further-customization-of-item-view-editors">Further Customization of Item View Editors</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Color Editor Factory Example</h1>
<span class="subtitle"></span>
<!-- $$$itemviews/coloreditorfactory-brief -->
<p>This example shows how to create an editor that can be used by a <a href="qitemdelegate.html">QItemDelegate</a>.</p>
<!-- @@@itemviews/coloreditorfactory -->
<!-- $$$itemviews/coloreditorfactory-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/coloreditorfactoryimage.png" alt="" /></p><p>When editing data in a <a href="qlistview.html">QListView</a>, <a href="qtableview.html">QTableView</a>, or <a href="qtreeview.html">QTreeView</a>, editors are created and displayed by a <a href="model-view-programming.html#delegate-classes">delegate</a>. <a href="qitemdelegate.html">QItemDelegate</a>, which is the default delegate used by Qt's <a href="model-view-programming.html#view-classes">item views</a>, uses a <a href="qitemeditorfactory.html">QItemEditorFactory</a> to create editors for it. A unique instance provided by <a href="qitemeditorfactory.html">QItemEditorFactory</a> is by default installed on all item delegates.</p>
<p>An item editor factory contains a collection of <a href="qitemeditorcreatorbase.html">QItemEditorCreatorBase</a> instances, which are specialized factories that produce editors for one particular <a href="../qtcore/qvariant.html">QVariant</a> data type (all models in Qt store their data in <a href="../qtcore/qvariant.html">QVariant</a>s). An editor can be any Qt or custom widget.</p>
<p>In this example, we will create an editor (implemented in the <code>ColorListEditor</code> class) that can edit the <a href="../qtgui/qcolor.html">QColor</a> data type and be used by <a href="qitemdelegate.html">QItemDelegate</a>s. We do this by creating a new <a href="qitemeditorcreatorbase.html">QItemEditorCreatorBase</a> that produces <code>ColorListEditors</code> and register it with a new factory, which we set as the default editor item factory (the unique factory instance). To test our editor, we have implemented the <code>Window</code> class, which displays a <a href="qtablewidget.html">QTableWidget</a> in which <a href="../qtgui/qcolor.html">QColor</a>s can be edited.</p>
<a name="window-class-implementation"></a>
<h2 id="window-class-implementation">Window Class Implementation</h2>
<p>In the Window class, we create the item editor creator base for our color editor and add it to the default factory. We also create a <a href="qtablewidget.html">QTableWidget</a> in which our editor can be tested. It is filled with some data and displayed in a window.</p>
<p>We take a closer look at the constructor:</p>
<pre class="cpp">

  Window<span class="operator">::</span>Window()
  {
      <span class="type"><a href="qitemeditorfactory.html">QItemEditorFactory</a></span> <span class="operator">*</span>factory <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qitemeditorfactory.html">QItemEditorFactory</a></span>;

      <span class="type"><a href="qitemeditorcreatorbase.html">QItemEditorCreatorBase</a></span> <span class="operator">*</span>colorListCreator <span class="operator">=</span>
          <span class="keyword">new</span> <span class="type"><a href="qstandarditemeditorcreator.html">QStandardItemEditorCreator</a></span><span class="operator">&lt;</span>ColorListEditor<span class="operator">&gt;</span>();

      factory<span class="operator">-</span><span class="operator">&gt;</span>registerEditor(<span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Color<span class="operator">,</span> colorListCreator);

      <span class="type"><a href="qitemeditorfactory.html">QItemEditorFactory</a></span><span class="operator">::</span>setDefaultFactory(factory);

      createGUI();
  }

</pre>
<p>The <a href="qstandarditemeditorcreator.html">QStandardItemEditorCreator</a> is a convenience class that inherits <a href="qitemeditorcreatorbase.html">QItemEditorCreatorBase</a>. Its constructor takes a template class, of which instances are returned from <a href="qitemeditorcreatorbase.html#createWidget">createWidget()</a>. The creator uses a constructor that takes a <a href="qwidget.html">QWidget</a> as its only parameter; the template class must provide this. This way, there is no need to subclass <a href="qstandarditemeditorcreator.html">QStandardItemEditorCreator</a>.</p>
<p>After the new factory has been set, all standard item delegates will use it (i.e, also delegates that were created before the new default factory was set).</p>
<p>The <code>createGUI()</code> function sets up the table and fills it with data.</p>
<a name="colorlisteditor-definition"></a>
<h2 id="colorlisteditor-definition">ColorListEditor Definition</h2>
<p>The ColorListEditor inherits <a href="qcombobox.html">QComboBox</a> and lets the user select a <a href="../qtgui/qcolor.html">QColor</a> from its popup list.</p>
<pre class="cpp">

  <span class="keyword">class</span> ColorListEditor : <span class="keyword">public</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>
  {
      Q_OBJECT
      Q_PROPERTY(<span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color READ color WRITE setColor USER <span class="keyword">true</span>)

  <span class="keyword">public</span>:
      ColorListEditor(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> <span class="number">0</span>);

  <span class="keyword">public</span>:
      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color() <span class="keyword">const</span>;
      <span class="type">void</span> setColor(<span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> c);

  <span class="keyword">private</span>:
      <span class="type">void</span> populateList();
  };

</pre>
<p><a href="qitemdelegate.html">QItemDelegate</a> manages the interaction between the editor and the model, i.e&#x2e;, it retrieves data to edit from the model and store data from the editor in the model. The data that is edited by an editor is stored in the editor's user data property, and the delegate uses Qt's <a href="../qtcore/properties.html">property system</a> to access it by name. We declare our user data property with the <a href="../qtcore/qobject.html#Q_PROPERTY">Q_PROPERTY</a> macro. The property is set to be the user type with the USER keyword.</p>
<a name="colorlisteditor-implementation"></a>
<h2 id="colorlisteditor-implementation">ColorListEditor Implementation</h2>
<p>The constructor of <code>ColorListEditor</code> simply calls <code>populateList()</code>, which we will look at later. We move on to the <code>color()</code> function:</p>
<pre class="cpp">

  <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> ColorListEditor<span class="operator">::</span>color() <span class="keyword">const</span>
  {
      <span class="keyword">return</span> qvariant_cast<span class="operator">&lt;</span><span class="type"><a href="../qtgui/qcolor.html">QColor</a></span><span class="operator">&gt;</span>(itemData(currentIndex()<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DecorationRole));
  }

</pre>
<p>We return the data that is selected in the combobox. The data is stored in the <a href="../qtcore/qt.html#ItemDataRole-enum">Qt::DecorationRole</a> as the color is then also displayed in the popup list (as shown in the image above).</p>
<pre class="cpp">

  <span class="type">void</span> ColorListEditor<span class="operator">::</span>setColor(<span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color)
  {
      setCurrentIndex(findData(color<span class="operator">,</span> <span class="type">int</span>(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DecorationRole)));
  }

</pre>
<p>The <code>findData()</code> function searches the items in the combobox and returns the index of the item that has <code>color</code> in the Qt::Decoration role.</p>
<pre class="cpp">

  <span class="type">void</span> ColorListEditor<span class="operator">::</span>populateList()
  {
      <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> colorNames <span class="operator">=</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span><span class="operator">::</span>colorNames();

      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> colorNames<span class="operator">.</span>size(); <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color(colorNames<span class="operator">[</span>i<span class="operator">]</span>);

          insertItem(i<span class="operator">,</span> colorNames<span class="operator">[</span>i<span class="operator">]</span>);
          setItemData(i<span class="operator">,</span> color<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DecorationRole);
      }
  }

</pre>
<p>Qt knows some predefined colors by name. We simply loop through these to fill our editor with items.</p>
<a name="further-customization-of-item-view-editors"></a>
<h2 id="further-customization-of-item-view-editors">Further Customization of Item View Editors</h2>
<p>You can customize Qt's <a href="model-view-programming.html">model view framework</a> in many ways. The procedure shown in this example is usually sufficient to provide custom editors. Further customization is achieved by subclassing <a href="qitemeditorfactory.html">QItemEditorFactory</a> and <a href="qitemeditorcreatorbase.html">QItemEditorCreatorBase</a>. It is also possible to subclass <a href="qitemdelegate.html">QItemDelegate</a> if you don't wish to use a factory at all.</p>
<p>Possible suggestions are:</p>
<ul>
<li>If the editor widget has no user property defined, the delegate asks the factory for the property name, which it in turn asks the item editor creator for. In this case, you can use the <a href="qitemeditorcreator.html">QItemEditorCreator</a> class, which takes the property name to use for editing as a constructor argument.</li>
<li>If the editor requires other constructors or other initialization than provided by <a href="qitemeditorcreatorbase.html">QItemEditorCreatorBase</a>, you must reimplement <a href="qitemeditorcreatorbase.html#createWidget">QItemEditorCreatorBase::createWidget</a>().</li>
<li>You could also subclass <a href="qitemeditorfactory.html">QItemEditorFactory</a> if you only want to provide editors for certain kinds of data or use another method of creating the editors than using creator bases.</li>
</ul>
<p>In this example, we use a standard <a href="../qtcore/qvariant.html">QVariant</a> data type. You can also use custom types. In the <a href="qtwidgets-itemviews-stardelegate-example.html">Star Delegate Example</a>, we show how to store a custom data type in a <a href="../qtcore/qvariant.html">QVariant</a> and paint and edit it in a class that inherits <a href="qitemdelegate.html">QItemDelegate</a>.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-itemviews-coloreditorfactory-coloreditorfactory-pro.html">itemviews/coloreditorfactory/coloreditorfactory.pro</a></li>
<li><a href="qtwidgets-itemviews-coloreditorfactory-colorlisteditor-cpp.html">itemviews/coloreditorfactory/colorlisteditor.cpp</a></li>
<li><a href="qtwidgets-itemviews-coloreditorfactory-colorlisteditor-h.html">itemviews/coloreditorfactory/colorlisteditor.h</a></li>
<li><a href="qtwidgets-itemviews-coloreditorfactory-main-cpp.html">itemviews/coloreditorfactory/main.cpp</a></li>
<li><a href="qtwidgets-itemviews-coloreditorfactory-window-cpp.html">itemviews/coloreditorfactory/window.cpp</a></li>
<li><a href="qtwidgets-itemviews-coloreditorfactory-window-h.html">itemviews/coloreditorfactory/window.h</a></li>
</ul>
</div>
<!-- @@@itemviews/coloreditorfactory -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</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>