Sophie

Sophie

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

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" />
<!-- custom-types.qdoc -->
  <title>Creating Custom Qt Types | Qt Core 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="qtcore-index.html">Qt Core</a></td><td >Creating Custom Qt Types</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtcore-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="#overview">Overview</a></li>
<li class="level1"><a href="#creating-a-custom-type">Creating a Custom Type</a></li>
<li class="level1"><a href="#declaring-the-type-with-qmetatype">Declaring the Type with QMetaType</a></li>
<li class="level1"><a href="#creating-and-destroying-custom-objects">Creating and Destroying Custom Objects</a></li>
<li class="level1"><a href="#making-the-type-printable">Making the Type Printable</a></li>
<li class="level1"><a href="#further-reading">Further Reading</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Creating Custom Qt Types</h1>
<span class="subtitle"></span>
<!-- $$$custom-types.html-description -->
<div class="descr"> <a name="details"></a>
<a name="overview"></a>
<h2 id="overview">Overview</h2>
<p>When creating user interfaces with Qt, particularly those with specialized controls and features, developers sometimes need to create new data types that can be used alongside or in place of Qt's existing set of value types.</p>
<p>Standard types such as <a href="qsize.html">QSize</a>, <a href="../qtgui/qcolor.html">QColor</a> and <a href="qstring.html">QString</a> can all be stored in <a href="qvariant.html">QVariant</a> objects, used as the types of properties in <a href="qobject.html">QObject</a>-based classes, and emitted in signal-slot communication.</p>
<p>In this document, we take a custom type and describe how to integrate it into Qt's object model so that it can be stored in the same way as standard Qt types. We then show how to register the custom type to allow it to be used in signals and slots connections.</p>
<a name="creating-a-custom-type"></a>
<h2 id="creating-a-custom-type">Creating a Custom Type</h2>
<p>Before we begin, we need to ensure that the custom type we are creating meets all the requirements imposed by <a href="qmetatype.html">QMetaType</a>. In other words, it must provide:</p>
<ul>
<li>a public default constructor,</li>
<li>a public copy constructor, and</li>
<li>a public destructor.</li>
</ul>
<p>The following <code>Message</code> class definition includes these members:</p>
<pre class="cpp">

  <span class="keyword">class</span> Message
  {
  <span class="keyword">public</span>:
      Message();
      Message(<span class="keyword">const</span> Message <span class="operator">&amp;</span>other);
      <span class="operator">~</span>Message();

      Message(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>body<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstringlist.html">QStringList</a></span> <span class="operator">&amp;</span>headers);

      <span class="type"><a href="qstring.html">QString</a></span> body() <span class="keyword">const</span>;
      <span class="type"><a href="qstringlist.html">QStringList</a></span> headers() <span class="keyword">const</span>;

  <span class="keyword">private</span>:
      <span class="type"><a href="qstring.html">QString</a></span> m_body;
      <span class="type"><a href="qstringlist.html">QStringList</a></span> m_headers;
  };

</pre>
<p>The class also provides a constructor for normal use and two public member functions that are used to obtain the private data.</p>
<a name="declaring-the-type-with-qmetatype"></a>
<h2 id="declaring-the-type-with-qmetatype">Declaring the Type with QMetaType</h2>
<p>The <code>Message</code> class only needs a suitable implementation in order to be usable. However, Qt's type system will not be able to understand how to store, retrieve and serialize instances of this class without some assistance. For example, we will be unable to store <code>Message</code> values in <a href="qvariant.html">QVariant</a>.</p>
<p>The class in Qt responsible for custom types is <a href="qmetatype.html">QMetaType</a>. To make the type known to this class, we invoke the <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>() macro on the class in the header file where it is defined:</p>
<pre class="cpp">

  Q_DECLARE_METATYPE(Message);

</pre>
<p>This now makes it possible for <code>Message</code> values to be stored in <a href="qvariant.html">QVariant</a> objects and retrieved later. See the <a href="qtcore-tools-customtype-example.html">Custom Type Example</a> for code that demonstrates this.</p>
<p>The <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>() macro also makes it possible for these values to be used as arguments to signals, but <i>only in direct signal-slot connections</i>. To make the custom type generally usable with the signals and slots mechanism, we need to perform some extra work.</p>
<a name="creating-and-destroying-custom-objects"></a>
<h2 id="creating-and-destroying-custom-objects">Creating and Destroying Custom Objects</h2>
<p>Although the declaration in the previous section makes the type available for use in direct signal-slot connections, it cannot be used for queued signal-slot connections, such as those that are made between objects in different threads. This is because the meta-object system does not know how to handle creation and destruction of objects of the custom type at run-time.</p>
<p>To enable creation of objects at run-time, call the <a href="qmetatype.html#qRegisterMetaType-1">qRegisterMetaType</a>() template function to register it with the meta-object system. This also makes the type available for queued signal-slot communication as long as you call it before you make the first connection that uses the type.</p>
<p>The <a href="qtcore-threads-queuedcustomtype-example.html">Queued Custom Type Example</a> declares a <code>Block</code> class which is registered in the <code>main.cpp</code> file:</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="../qtwidgets/qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);
      ...
      <a href="qmetatype.html#qRegisterMetaType-1">qRegisterMetaType</a><span class="operator">&lt;</span>Block<span class="operator">&gt;</span>();
      ...
      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<p>This type is later used in a signal-slot connection in the <code>window.cpp</code> file:</p>
<pre class="cpp">

  Window<span class="operator">::</span>Window()
  {
      thread <span class="operator">=</span> <span class="keyword">new</span> RenderThread();
      ...
      connect(thread<span class="operator">,</span> SIGNAL(sendBlock(Block))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(addBlock(Block)));
      ...
      setWindowTitle(tr(<span class="string">&quot;Queued Custom Type&quot;</span>));
  }

</pre>
<p>If a type is used in a queued connection without being registered, a warning will be printed at the console; for example:</p>
<pre class="cpp">

  <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect: Cannot queue arguments of type <span class="char">'Block'</span>
  (Make sure <span class="char">'Block'</span> is registered <span class="keyword">using</span> <a href="qmetatype.html#qRegisterMetaType-1">qRegisterMetaType</a>()<span class="operator">.</span>)

</pre>
<a name="making-the-type-printable"></a>
<h2 id="making-the-type-printable">Making the Type Printable</h2>
<p>It is often quite useful to make a custom type printable for debugging purposes, as in the following code:</p>
<pre class="cpp">

      Message message(body<span class="operator">,</span> headers);
      <a href="qtglobal.html#qDebug">qDebug</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Original:&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> message;

</pre>
<p>This is achieved by creating a streaming operator for the type, which is often defined in the header file for that type:</p>
<pre class="cpp">

  <span class="type"><a href="qdebug.html">QDebug</a></span> <span class="keyword">operator</span><span class="operator">&lt;</span><span class="operator">&lt;</span>(<span class="type"><a href="qdebug.html">QDebug</a></span> dbg<span class="operator">,</span> <span class="keyword">const</span> Message <span class="operator">&amp;</span>message);

</pre>
<p>The implementation for the <code>Message</code> type in the <a href="qtcore-tools-customtype-example.html">Custom Type Example</a> goes to some effort to make the printable representation as readable as possible:</p>
<pre class="cpp">

  <span class="type"><a href="qdebug.html">QDebug</a></span> <span class="keyword">operator</span><span class="operator">&lt;</span><span class="operator">&lt;</span>(<span class="type"><a href="qdebug.html">QDebug</a></span> dbg<span class="operator">,</span> <span class="keyword">const</span> Message <span class="operator">&amp;</span>message)
  {
      <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> body <span class="operator">=</span> message<span class="operator">.</span>body();
      <span class="type"><a href="qvector.html">QVector</a></span><span class="operator">&lt;</span><span class="type"><a href="qstringref.html">QStringRef</a></span><span class="operator">&gt;</span> pieces <span class="operator">=</span> body<span class="operator">.</span>splitRef(<span class="string">&quot;\r\n&quot;</span><span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span><span class="operator">::</span>SkipEmptyParts);
      <span class="keyword">if</span> (pieces<span class="operator">.</span>isEmpty())
          dbg<span class="operator">.</span>nospace() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Message()&quot;</span>;
      <span class="keyword">else</span> <span class="keyword">if</span> (pieces<span class="operator">.</span>size() <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>)
          dbg<span class="operator">.</span>nospace() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Message(&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> pieces<span class="operator">.</span>first() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;)&quot;</span>;
      <span class="keyword">else</span>
          dbg<span class="operator">.</span>nospace() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Message(&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> pieces<span class="operator">.</span>first() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot; ...)&quot;</span>;
      <span class="keyword">return</span> dbg<span class="operator">.</span>maybeSpace();
  }

</pre>
<p>The output sent to the debug stream can, of course, be made as simple or as complicated as you like. Note that the value returned by this function is the <a href="qdebug.html">QDebug</a> object itself, though this is often obtained by calling the <a href="qdebug.html#maybeSpace">maybeSpace()</a> member function of <a href="qdebug.html">QDebug</a> that pads out the stream with space characters to make it more readable.</p>
<a name="further-reading"></a>
<h2 id="further-reading">Further Reading</h2>
<p>The <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>() macro and <a href="qmetatype.html#qRegisterMetaType-1">qRegisterMetaType</a>() function documentation contain more detailed information about their uses and limitations.</p>
<p>The <a href="qtcore-tools-customtype-example.html">Custom Type</a> and <a href="qtcore-threads-queuedcustomtype-example.html">Queued Custom Type</a> examples show how to implement a custom type with the features outlined in this document.</p>
<p>The Debugging Techniques document provides an overview of the debugging mechanisms discussed above.</p>
</div>
<!-- @@@custom-types.html -->
        </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>