Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-updates > by-pkgid > 6e2327ca1c896c6d674ae53117299f21 > files > 1729

qtdeclarative5-doc-5.12.6-1.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>Qt Quick TableViews examples - Pixelator | Qt Quick 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="qtquick-index.html">Qt Quick</a></td><td >Qt Quick TableViews examples - Pixelator</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-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="#running-the-example">Running the Example</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick TableViews examples - Pixelator</h1>
<span class="subtitle"></span>
<!-- $$$tableview/pixelator-brief -->
<p>The Pixelator example shows how a QML <a href="qml-qtquick-tableview.html">TableView</a> and a delegate can be used for custom table models.</p>
<!-- @@@tableview/pixelator -->
<!-- $$$tableview/pixelator-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/qt-pixelator.png" alt="" /></p><a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p>
<pre class="cpp">

  <span class="keyword">class</span> ImageModel : <span class="keyword">public</span> <span class="type">QAbstractTableModel</span>
  {
      Q_OBJECT
      Q_PROPERTY(<span class="type">QString</span> source READ source WRITE setSource NOTIFY sourceChanged)
  <span class="keyword">public</span>:
      ImageModel(<span class="type">QObject</span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr);

      <span class="type">QString</span> source() <span class="keyword">const</span>;
      <span class="type">void</span> setSource(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&amp;</span>source);

      <span class="type">int</span> rowCount(<span class="keyword">const</span> <span class="type">QModelIndex</span> <span class="operator">&amp;</span>parent <span class="operator">=</span> <span class="type">QModelIndex</span>()) <span class="keyword">const</span> override;
      <span class="type">int</span> columnCount(<span class="keyword">const</span> <span class="type">QModelIndex</span> <span class="operator">&amp;</span>parent <span class="operator">=</span> <span class="type">QModelIndex</span>()) <span class="keyword">const</span> override;

      <span class="type">QVariant</span> data(<span class="keyword">const</span> <span class="type">QModelIndex</span> <span class="operator">&amp;</span>index<span class="operator">,</span> <span class="type">int</span> role <span class="operator">=</span> <span class="type">Qt</span><span class="operator">::</span>DisplayRole) <span class="keyword">const</span> override;

      <span class="type">QVariant</span> headerData(<span class="type">int</span> <span class="comment">/* section */</span><span class="operator">,</span> <span class="type">Qt</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> override;

  <span class="keyword">signals</span>:
      <span class="type">void</span> sourceChanged();

  <span class="keyword">private</span>:
      <span class="type">QString</span> m_source;
      <span class="type">QImage</span> m_image;
  };

</pre>
<p>We only require a simple, read-only table model. Thus, we need to implement functions to indicate the dimensions of the image and supply data to the <a href="qml-qtquick-tableview.html">TableView</a>. We use the Qt Property System and a source property as <code>QString</code> to set the path of the image.</p>
<pre class="cpp">

  <span class="type">void</span> ImageModel<span class="operator">::</span>setSource(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&amp;</span>source)
  {
      <span class="keyword">if</span> (m_source <span class="operator">=</span><span class="operator">=</span> source)
          <span class="keyword">return</span>;

      beginResetModel();
      m_source <span class="operator">=</span> source;
      m_image<span class="operator">.</span>load(m_source);
      endResetModel();
  }

</pre>
<p>Here we load the image when the source path is set. When the source path has changed, we need to call <code>beginResetModel()</code> before. After the image has been loaded, we need to call <code>endResetModel()</code>.</p>
<pre class="cpp">

  <span class="type">int</span> ImageModel<span class="operator">::</span>rowCount(<span class="keyword">const</span> <span class="type">QModelIndex</span> <span class="operator">&amp;</span>parent) <span class="keyword">const</span>
  {
      <span class="keyword">if</span> (parent<span class="operator">.</span>isValid())
          <span class="keyword">return</span> <span class="number">0</span>;
      <span class="keyword">return</span> m_image<span class="operator">.</span>height();
  }

  <span class="type">int</span> ImageModel<span class="operator">::</span>columnCount(<span class="keyword">const</span> <span class="type">QModelIndex</span> <span class="operator">&amp;</span>parent) <span class="keyword">const</span>
  {
      <span class="keyword">if</span> (parent<span class="operator">.</span>isValid())
          <span class="keyword">return</span> <span class="number">0</span>;
      <span class="keyword">return</span> m_image<span class="operator">.</span>width();
  }

</pre>
<p>The row and column count is set to image height and width, respectively.</p>
<pre class="cpp">

  <span class="type">QVariant</span> ImageModel<span class="operator">::</span>data(<span class="keyword">const</span> <span class="type">QModelIndex</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">Qt</span><span class="operator">::</span>DisplayRole)
          <span class="keyword">return</span> <span class="type">QVariant</span>();
      <span class="keyword">return</span> qGray(m_image<span class="operator">.</span>pixel(index<span class="operator">.</span>column()<span class="operator">,</span> index<span class="operator">.</span>row()));
  }

</pre>
<p>This overloaded function allows us to access the pixel data from the image. When we call this function with the display role, we return the pixel's gray value.</p>
<pre class="cpp">

  qmlRegisterType<span class="operator">&lt;</span>ImageModel<span class="operator">&gt;</span>(<span class="string">&quot;ImageModel&quot;</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">&quot;ImageModel&quot;</span>);

</pre>
<p>We need to register our model in the QML type system to be able to use it from the QML side.</p>
<pre class="qml">

  Component {
      id: pixelDelegate

      Item {
          readonly property real gray: model.display / 255.0
          readonly property real size: 16

          implicitWidth: size
          implicitHeight: size

</pre>
<p>Each pixel in the <code>TableView</code> is displayed via a delegate component. It contains an item that has an implicit height and width defining the cell size of the table. It also has a property for the gray value of the pixel that is retrieved from the model.</p>
<pre class="qml">

  Rectangle {
      id: rect
      anchors.centerIn: parent
      color: "#09102b"
      radius: size - gray * size
      implicitWidth: radius
      implicitHeight: radius

</pre>
<p>Inside the <code>Item</code>, there is a rounded <code>Rectangle</code> with the size and radius according to the pixel's gray value.</p>
<pre class="qml">

  MouseArea {
      anchors.fill: parent
      hoverEnabled: true
      onEntered: rect.color = "#cecfd5"
      onExited: colorAnimation.start()
  }

</pre>
<p>For a little bit of interaction, we place a <code>MouseArea</code> inside the <code>Item</code> and change the Rectangle's color on mouse over.</p>
<pre class="qml">

  ColorAnimation on color {
      id: colorAnimation
      running: false
      to: "#41cd52"
      duration: 1500
  }

</pre>
<p>The <code>Rectangle</code> also has a short color animation to fade between the colors when it is changed.</p>
<pre class="qml">

  TableView {
      id: tableView
      anchors.fill: parent
      model: ImageModel {
          source: ":/qt.png"
      }

      delegate: pixelDelegate
  }

</pre>
<p>The <code>TableView</code> spans over the whole window and has an instance of our custom <code>ImageModel</code> attached.</p>
<p>Files:</p>
<ul>
<li><a href="qtquick-tableview-pixelator-imagemodel-cpp.html">tableview/pixelator/imagemodel.cpp</a></li>
<li><a href="qtquick-tableview-pixelator-imagemodel-h.html">tableview/pixelator/imagemodel.h</a></li>
<li><a href="qtquick-tableview-pixelator-main-cpp.html">tableview/pixelator/main.cpp</a></li>
<li><a href="qtquick-tableview-pixelator-main-qml.html">tableview/pixelator/main.qml</a></li>
<li><a href="qtquick-tableview-pixelator-pixelator-pro.html">tableview/pixelator/pixelator.pro</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/tableview/pixelator/qt.png">tableview/pixelator/qt.png</a></li>
</ul>
</div>
<!-- @@@tableview/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>