Sophie

Sophie

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

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" />
<!-- pixelator.qdoc -->
  <title>Pixelator 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 >Pixelator 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="#imagemodel-class-definition">ImageModel Class Definition</a></li>
<li class="level1"><a href="#imagemodel-class-implementation">ImageModel Class Implementation</a></li>
<li class="level1"><a href="#pixeldelegate-class-definition">PixelDelegate Class Definition</a></li>
<li class="level1"><a href="#pixeldelegate-class-implementation">PixelDelegate Class Implementation</a></li>
<li class="level1"><a href="#using-the-custom-delegate">Using The Custom Delegate</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Pixelator Example</h1>
<span class="subtitle"></span>
<!-- $$$itemviews/pixelator-brief -->
<p>The Pixelator example shows how delegates can be used to customize the way that items are rendered in standard item views.</p>
<!-- @@@itemviews/pixelator -->
<!-- $$$itemviews/pixelator-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/pixelator-example.png" alt="" /></p><p>By default, <a href="qtreeview.html">QTreeView</a>, <a href="qtableview.html">QTableView</a>, and <a href="qlistview.html">QListView</a> use a standard item delegate to display and edit a set of common data types that are sufficient for many applications. However, an application may need to represent items of data in a particular way, or provide support for rendering more specialized data types, and this often requires the use of a custom delegate.</p>
<p>In this example, we show how to use custom delegates to modify the appearance of standard views. To do this, we implement the following components:</p>
<ul>
<li>A model which represents each pixel in an image as an item of data, where each item contains a value for the brightness of the corresponding pixel.</li>
<li>A custom delegate that uses the information supplied by the model to represent each pixel as a black circle on a white background, where the radius of the circle corresponds to the darkness of the pixel.</li>
</ul>
<p>This example may be useful for developers who want to implement their own table models or custom delegates. The process of creating custom delegates for editing item data is covered in the <a href="qtwidgets-itemviews-spinboxdelegate-example.html">Spin Box Delegate</a> example.</p>
<a name="imagemodel-class-definition"></a>
<h2 id="imagemodel-class-definition">ImageModel Class Definition</h2>
<p>The <code>ImageModel</code> class is defined as follows:</p>
<pre class="cpp">

  <span class="keyword">class</span> ImageModel : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qabstracttablemodel.html">QAbstractTableModel</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      ImageModel(<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">void</span> setImage(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> <span class="operator">&amp;</span>image);

      <span class="type">int</span> rowCount(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>parent <span class="operator">=</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span>()) <span class="keyword">const</span> override;
      <span class="type">int</span> columnCount(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>parent <span class="operator">=</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span>()) <span class="keyword">const</span> override;

      <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> data(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index<span class="operator">,</span> <span class="type">int</span> role <span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DisplayRole) <span class="keyword">const</span> override;
      <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> headerData(<span class="type">int</span> section<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Orientation orientation<span class="operator">,</span> <span class="type">int</span> role <span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DisplayRole) <span class="keyword">const</span> override;

  <span class="keyword">private</span>:
      <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> modelImage;
  };

</pre>
<p>Since we only require a simple, read-only table model, we only need to implement functions to indicate the dimensions of the image and supply data to other components.</p>
<a name="imagemodel-class-implementation"></a>
<h2 id="imagemodel-class-implementation">ImageModel Class Implementation</h2>
<p>The constructor is trivial:</p>
<pre class="cpp">

  ImageModel<span class="operator">::</span>ImageModel(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="../qtcore/qabstracttablemodel.html">QAbstractTableModel</a></span>(parent)
  {
  }

</pre>
<p>The <code>setImage()</code> function sets the image that will be used by the model:</p>
<pre class="cpp">

  <span class="type">void</span> ImageModel<span class="operator">::</span>setImage(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> <span class="operator">&amp;</span>image)
  {
      beginResetModel();
      modelImage <span class="operator">=</span> image;
      endResetModel();
  }

</pre>
<p>The QAbstractItemModel::reset() call tells the view(s) that the model has changed.</p>
<p>The <code>rowCount()</code> and <code>columnCount()</code> functions return the height and width of the image respectively:</p>
<pre class="cpp">

  <span class="type">int</span> ImageModel<span class="operator">::</span>rowCount(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span> <span class="comment">/* parent */</span>) <span class="keyword">const</span>
  {
      <span class="keyword">return</span> modelImage<span class="operator">.</span>height();
  }

  <span class="type">int</span> ImageModel<span class="operator">::</span>columnCount(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span> <span class="comment">/* parent */</span>) <span class="keyword">const</span>
  {
      <span class="keyword">return</span> modelImage<span class="operator">.</span>width();
  }

</pre>
<p>Since the image is a simple two-dimensional structure, the <code>parent</code> arguments to these functions are unused. They both simply return the relevant size from the underlying image object.</p>
<p>The <code>data()</code> function returns data for the item that corresponds to a given model index in a format that is suitable for a particular role:</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> ImageModel<span class="operator">::</span>data(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index<span class="operator">,</span> <span class="type">int</span> role) <span class="keyword">const</span>
  {
      <span class="keyword">if</span> (<span class="operator">!</span>index<span class="operator">.</span>isValid() <span class="operator">|</span><span class="operator">|</span> role <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DisplayRole)
          <span class="keyword">return</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>();
      <span class="keyword">return</span> <a href="../qtgui/qcolor.html#qGray-1">qGray</a>(modelImage<span class="operator">.</span>pixel(index<span class="operator">.</span>column()<span class="operator">,</span> index<span class="operator">.</span>row()));
  }

</pre>
<p>In this implementation, we only check that the model index is valid, and that the role requested is the <a href="../qtcore/qt.html#ItemDataRole-enum">DisplayRole</a>. If so, the function returns the grayscale value of the relevant pixel in the image; otherwise, a null model index is returned.</p>
<p>This model can be used with <a href="qtableview.html">QTableView</a> to display the integer brightness values for the pixels in the image. However, we will implement a custom delegate to display this information in a more artistic way.</p>
<p>The <code>headerData()</code> function is also reimplemented:</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> ImageModel<span class="operator">::</span>headerData(<span class="type">int</span> <span class="comment">/* section */</span><span class="operator">,</span>
                                  <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Orientation <span class="comment">/* orientation */</span><span class="operator">,</span>
                                  <span class="type">int</span> role) <span class="keyword">const</span>
  {
      <span class="keyword">if</span> (role <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>SizeHintRole)
          <span class="keyword">return</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(<span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
      <span class="keyword">return</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>();
  }

</pre>
<p>We return (1, 1) as the size hint for a header item. If we didn't, the headers would default to a larger size, preventing us from displaying really small items (which can be specified using the <b>Pixel size</b> combobox).</p>
<a name="pixeldelegate-class-definition"></a>
<h2 id="pixeldelegate-class-definition">PixelDelegate Class Definition</h2>
<p>The <code>PixelDelegate</code> class is defined as follows:</p>
<pre class="cpp">

  <span class="keyword">class</span> PixelDelegate : <span class="keyword">public</span> <span class="type"><a href="qabstractitemdelegate.html">QAbstractItemDelegate</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      PixelDelegate(<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">void</span> paint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> <span class="operator">*</span>painter<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"><a href="../qtcore/qsize.html">QSize</a></span> sizeHint(<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="keyword">public</span> <span class="keyword">slots</span>:
      <span class="type">void</span> setPixelSize(<span class="type">int</span> size);

  <span class="keyword">private</span>:
      <span class="type">int</span> pixelSize;
  };

</pre>
<p>This class provides only basic features for a delegate so, unlike the <a href="qtwidgets-itemviews-spinboxdelegate-example.html">Spin Box Delegate</a> example, we subclass <a href="qabstractitemdelegate.html">QAbstractItemDelegate</a> instead of <a href="qitemdelegate.html">QItemDelegate</a>.</p>
<p>We only need to reimplement <a href="qabstractitemdelegate.html#paint">paint()</a> and <a href="qabstractitemdelegate.html#sizeHint">sizeHint()</a> in this class. However, we also provide a delegate-specific <code>setPixelSize()</code> function so that we can change the delegate's behavior via the signals and slots mechanism.</p>
<a name="pixeldelegate-class-implementation"></a>
<h2 id="pixeldelegate-class-implementation">PixelDelegate Class Implementation</h2>
<p>The <code>PixelDelegate</code> constructor is used to set up a default value for the size of each &quot;pixel&quot; that it renders. The base class constructor is also called to ensure that the delegate is set up with a parent object, if one is supplied:</p>
<pre class="cpp">

  PixelDelegate<span class="operator">::</span>PixelDelegate(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qabstractitemdelegate.html">QAbstractItemDelegate</a></span>(parent)
  {
      pixelSize <span class="operator">=</span> <span class="number">12</span>;
  }

</pre>
<p>Each item is rendered by the delegate's <a href="qabstractitemdelegate.html#paint">paint()</a> function. The view calls this function with a ready-to-use <a href="../qtgui/qpainter.html">QPainter</a> object, style information that the delegate should use to correctly draw the item, and an index to the item in the model:</p>
<pre class="cpp">

  <span class="type">void</span> PixelDelegate<span class="operator">::</span>paint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> <span class="operator">*</span>painter<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>
  {
      <span class="keyword">if</span> (option<span class="operator">.</span>state <span class="operator">&amp;</span> <span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>State_Selected)
          painter<span class="operator">-</span><span class="operator">&gt;</span>fillRect(option<span class="operator">.</span>rect<span class="operator">,</span> option<span class="operator">.</span>palette<span class="operator">.</span>highlight());

</pre>
<p>The first task the delegate has to perform is to draw the item's background correctly. Usually, selected items appear differently to non-selected items, so we begin by testing the state passed in the style option and filling the background if necessary.</p>
<p>The radius of each circle is calculated in the following lines of code:</p>
<pre class="cpp">

      <span class="type">int</span> size <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMin">qMin</a>(option<span class="operator">.</span>rect<span class="operator">.</span>width()<span class="operator">,</span> option<span class="operator">.</span>rect<span class="operator">.</span>height());
      <span class="type">int</span> brightness <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>DisplayRole)<span class="operator">.</span>toInt();
      <span class="type">double</span> radius <span class="operator">=</span> (size <span class="operator">/</span> <span class="number">2.0</span>) <span class="operator">-</span> (brightness <span class="operator">/</span> <span class="number">255.0</span> <span class="operator">*</span> size <span class="operator">/</span> <span class="number">2.0</span>);
      <span class="keyword">if</span> (radius <span class="operator">=</span><span class="operator">=</span> <span class="number">0.0</span>)
          <span class="keyword">return</span>;

</pre>
<p>First, the largest possible radius of the circle is determined by taking the smallest dimension of the style option's <code>rect</code> attribute. Using the model index supplied, we obtain a value for the brightness of the relevant pixel in the image. The radius of the circle is calculated by scaling the brightness to fit within the item and subtracting it from the largest possible radius.</p>
<pre class="cpp">

      painter<span class="operator">-</span><span class="operator">&gt;</span>save();
      painter<span class="operator">-</span><span class="operator">&gt;</span>setRenderHint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing<span class="operator">,</span> <span class="keyword">true</span>);
      painter<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoPen);

</pre>
<p>We save the painter's state, turn on antialiasing (to obtain smoother curves), and turn off the pen.</p>
<pre class="cpp">

      <span class="keyword">if</span> (option<span class="operator">.</span>state <span class="operator">&amp;</span> <span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>State_Selected)
          painter<span class="operator">-</span><span class="operator">&gt;</span>setBrush(option<span class="operator">.</span>palette<span class="operator">.</span>highlightedText());
      <span class="keyword">else</span>
          painter<span class="operator">-</span><span class="operator">&gt;</span>setBrush(option<span class="operator">.</span>palette<span class="operator">.</span>text());

</pre>
<p>The foreground of the item (the circle representing a pixel) must be rendered using an appropriate brush. For unselected items, we will use a solid black brush; selected items are drawn using a predefined brush from the style option's palette.</p>
<pre class="cpp">

      painter<span class="operator">-</span><span class="operator">&gt;</span>drawEllipse(<span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span>(option<span class="operator">.</span>rect<span class="operator">.</span>x() <span class="operator">+</span> option<span class="operator">.</span>rect<span class="operator">.</span>width() <span class="operator">/</span> <span class="number">2</span> <span class="operator">-</span> radius<span class="operator">,</span>
                                  option<span class="operator">.</span>rect<span class="operator">.</span>y() <span class="operator">+</span> option<span class="operator">.</span>rect<span class="operator">.</span>height() <span class="operator">/</span> <span class="number">2</span> <span class="operator">-</span> radius<span class="operator">,</span>
                                  <span class="number">2</span> <span class="operator">*</span> radius<span class="operator">,</span> <span class="number">2</span> <span class="operator">*</span> radius));
      painter<span class="operator">-</span><span class="operator">&gt;</span>restore();
  }

</pre>
<p>Finally, we paint the circle within the rectangle specified by the style option and we call <a href="../qtgui/qpainter.html#restore">restore()</a> on the painter.</p>
<p>The <code>paint()</code> function does not have to be particularly complicated; it is only necessary to ensure that the state of the painter when the function returns is the same as it was when it was called. This usually means that any transformations applied to the painter must be preceded by a call to <a href="../qtgui/qpainter.html#save">QPainter::save</a>() and followed by a call to <a href="../qtgui/qpainter.html#restore">QPainter::restore</a>().</p>
<p>The delegate's <a href="qabstractitemdelegate.html#sizeHint">sizeHint()</a> function returns a size for the item based on the predefined pixel size, initially set up in the constructor:</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> PixelDelegate<span class="operator">::</span>sizeHint(<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="keyword">return</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(pixelSize<span class="operator">,</span> pixelSize);
  }

</pre>
<p>The delegate's size is updated whenever the pixel size is changed. We provide a custom slot to do this:</p>
<pre class="cpp">

  <span class="type">void</span> PixelDelegate<span class="operator">::</span>setPixelSize(<span class="type">int</span> size)
  {
      pixelSize <span class="operator">=</span> size;
  }

</pre>
<a name="using-the-custom-delegate"></a>
<h2 id="using-the-custom-delegate">Using The Custom Delegate</h2>
<p>In this example, we use a main window to display a table of data, using the custom delegate to render each cell in a particular way. Much of the <code>MainWindow</code> class performs tasks that are not related to item views. Here, we only quote the parts that are relevant. You can look at the rest of the implementation by following the links to the code at the top of this document.</p>
<p>In the constructor, we set up a table view, turn off its grid, and hide its headers:</p>
<pre class="cpp">

  MainWindow<span class="operator">::</span>MainWindow()
  {
      ...
      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>setShowGrid(<span class="keyword">false</span>);
      view<span class="operator">-</span><span class="operator">&gt;</span>horizontalHeader()<span class="operator">-</span><span class="operator">&gt;</span>hide();
      view<span class="operator">-</span><span class="operator">&gt;</span>verticalHeader()<span class="operator">-</span><span class="operator">&gt;</span>hide();
      view<span class="operator">-</span><span class="operator">&gt;</span>horizontalHeader()<span class="operator">-</span><span class="operator">&gt;</span>setMinimumSectionSize(<span class="number">1</span>);
      view<span class="operator">-</span><span class="operator">&gt;</span>verticalHeader()<span class="operator">-</span><span class="operator">&gt;</span>setMinimumSectionSize(<span class="number">1</span>);
      view<span class="operator">-</span><span class="operator">&gt;</span>setModel(model);

</pre>
<p>This enables the items to be drawn without any gaps between them. Removing the headers also prevents the user from adjusting the sizes of individual rows and columns.</p>
<p>We also set the minimum section size to 1 on the headers. If we didn't, the headers would default to a larger size, preventing us from displaying really small items (which can be specified using the <b>Pixel size</b> combobox).</p>
<p>The custom delegate is constructed with the main window as its parent, so that it will be deleted correctly later, and we set it on the table view.</p>
<pre class="cpp">

      PixelDelegate <span class="operator">*</span>delegate <span class="operator">=</span> <span class="keyword">new</span> PixelDelegate(<span class="keyword">this</span>);
      view<span class="operator">-</span><span class="operator">&gt;</span>setItemDelegate(delegate);

</pre>
<p>Each item in the table view will be rendered by the <code>PixelDelegate</code> instance.</p>
<p>We construct a spin box to allow the user to change the size of each &quot;pixel&quot; drawn by the delegate:</p>
<pre class="cpp">

      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>pixelSizeLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Pixel size:&quot;</span>));
      <span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>pixelSizeSpinBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qspinbox.html">QSpinBox</a></span>;
      pixelSizeSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setMinimum(<span class="number">4</span>);
      pixelSizeSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setMaximum(<span class="number">32</span>);
      pixelSizeSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setValue(<span class="number">12</span>);

</pre>
<p>This spin box is connected to the custom slot we implemented in the <code>PixelDelegate</code> class. This ensures that the delegate always draws each pixel at the currently specified size:</p>
<pre class="cpp">

      connect(pixelSizeSpinBox<span class="operator">,</span> <span class="type">QOverload</span><span class="operator">&lt;</span><span class="type">int</span><span class="operator">&gt;</span><span class="operator">::</span>of(<span class="operator">&amp;</span><span class="type"><a href="qspinbox.html">QSpinBox</a></span><span class="operator">::</span>valueChanged)<span class="operator">,</span>
              delegate<span class="operator">,</span> <span class="operator">&amp;</span>PixelDelegate<span class="operator">::</span>setPixelSize);
      connect(pixelSizeSpinBox<span class="operator">,</span> <span class="type">QOverload</span><span class="operator">&lt;</span><span class="type">int</span><span class="operator">&gt;</span><span class="operator">::</span>of(<span class="operator">&amp;</span><span class="type"><a href="qspinbox.html">QSpinBox</a></span><span class="operator">::</span>valueChanged)<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>updateView);
      ...
  }

</pre>
<p>We also connect the spin box to a slot in the <code>MainWindow</code> class. This forces the view to take into account the new size hints for each item; these are provided by the delegate in its <code>sizeHint()</code> function.</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>updateView()
  {
      view<span class="operator">-</span><span class="operator">&gt;</span>resizeColumnsToContents();
      view<span class="operator">-</span><span class="operator">&gt;</span>resizeRowsToContents();
  }

</pre>
<p>We explicitly resize the columns and rows to match the <b>Pixel size</b> combobox.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-itemviews-pixelator-imagemodel-cpp.html">itemviews/pixelator/imagemodel.cpp</a></li>
<li><a href="qtwidgets-itemviews-pixelator-imagemodel-h.html">itemviews/pixelator/imagemodel.h</a></li>
<li><a href="qtwidgets-itemviews-pixelator-images-qrc.html">itemviews/pixelator/images.qrc</a></li>
<li><a href="qtwidgets-itemviews-pixelator-main-cpp.html">itemviews/pixelator/main.cpp</a></li>
<li><a href="qtwidgets-itemviews-pixelator-mainwindow-cpp.html">itemviews/pixelator/mainwindow.cpp</a></li>
<li><a href="qtwidgets-itemviews-pixelator-mainwindow-h.html">itemviews/pixelator/mainwindow.h</a></li>
<li><a href="qtwidgets-itemviews-pixelator-pixelator-pro.html">itemviews/pixelator/pixelator.pro</a></li>
<li><a href="qtwidgets-itemviews-pixelator-pixeldelegate-cpp.html">itemviews/pixelator/pixeldelegate.cpp</a></li>
<li><a href="qtwidgets-itemviews-pixelator-pixeldelegate-h.html">itemviews/pixelator/pixeldelegate.h</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/itemviews/pixelator/images/qt.png">itemviews/pixelator/images/qt.png</a></li>
</ul>
</div>
<!-- @@@itemviews/pixelator -->
        </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>