<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Misinahaiya</title>
    <description>The latest articles on DEV Community by Misinahaiya (@misinahaiya).</description>
    <link>https://dev.to/misinahaiya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2768342%2F1ad8b61c-9a75-4fdf-9d9d-abc866dc13b0.png</url>
      <title>DEV Community: Misinahaiya</title>
      <link>https://dev.to/misinahaiya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/misinahaiya"/>
    <language>en</language>
    <item>
      <title>[Qt] VIII. Smart Memory Managements</title>
      <dc:creator>Misinahaiya</dc:creator>
      <pubDate>Sat, 01 Mar 2025 03:52:14 +0000</pubDate>
      <link>https://dev.to/misinahaiya/qt-viii-smart-memory-managements-4i5l</link>
      <guid>https://dev.to/misinahaiya/qt-viii-smart-memory-managements-4i5l</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"&lt;em&gt;Pointers are simple creatures. They were just like ions: if you make them be in somewhere, they'll be in somewhere, and that's it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Me&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I think you'll be frustrated if you see the following messages:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9nuxdutwxha89wagxrgk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9nuxdutwxha89wagxrgk.png" alt="SIGSEGV" width="643" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It might be the result of some kind of weird &lt;a href="https://en.wikipedia.org/wiki/Logic_error" rel="noopener noreferrer"&gt;runtime logic errors&lt;/a&gt;. Quoting &lt;em&gt;Beginning C++23&lt;/em&gt; by Mr. Ivor Horton and Mr. Peter Van Weert at Apress™ publishers verbatim (legal disclaimer: &lt;strong&gt;not&lt;/strong&gt; for commercial use! Just for educational and tutorial purposes only),&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dangling pointer&lt;/strong&gt; is a pointer variable that still contains the address to free store memory that has already been deallocated by either &lt;code&gt;delete&lt;/code&gt; or &lt;code&gt;delete[]&lt;/code&gt;. Dereferencing a dangling pointer makes you read from or, often worse, write to memory that might already be allocated to and used by other parts of your program, resulting in all kinds of unpredictable and unexpected results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, I assume that you all guys have been tortured by dangling pointers and null pointers, right? The Standard gives some solutions, such as include the &lt;code&gt;&amp;lt;memory&amp;gt;&lt;/code&gt; and using the &lt;code&gt;std::unique_ptr&lt;/code&gt; and &lt;code&gt;std::shared_ptr&lt;/code&gt;, etc. But, (personally,) I do not really like the design: they are complicated in definitions, the errors like &lt;code&gt;Implicit template instantiation&lt;/code&gt; emerge everywhere, and I am not into &lt;em&gt;class names&lt;/em&gt; with any underscores (this is just a style; but, man, we're doing &lt;em&gt;C++&lt;/em&gt;, not &lt;em&gt;Pascal&lt;/em&gt;, provided that even Python suggests you doing class names without underscores)&lt;/p&gt;

&lt;p&gt;You may of course blame me for being to fastidious, I accept it by a hundred and twenty percent. But that's me. I am not satisfied. I must find a better alternative.&lt;/p&gt;

&lt;p&gt;To master this topic, we must first know what is the C++ philosophy regarding resources allocations, or even better, how every mainstream compiler works under the hood. So, as mentioned by the creator of C++, Bjarne Stroustrup, C++ advocates allocating objects &lt;em&gt;in&lt;/em&gt; the constructors of an object, and freeing objects &lt;em&gt;in&lt;/em&gt; the destructors of such object. This is commonly known as RAII (Resource Acquisition Is Initialization). Given that there are no garbage collectors in C++ that would automatically release unused objects, we programmers must be very careful on checking tediously the memory leaks (i.e. in simple English, memory wastage). Will there be a better solution?&lt;/p&gt;

&lt;p&gt;Fortunately, Qt (in many and at most time) just stood there as a life-savior. The munificent developers meticulously crafted classes such as &lt;code&gt;QPointer&lt;/code&gt;, &lt;code&gt;QSharedPointer&lt;/code&gt;, &lt;code&gt;QWeakPointer&lt;/code&gt;, etc. Let us dive into them now, in this article.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you ask me the reason why I cover this topic, is that I just mentioned the pointers and memory managements of the Qt framework several times in the past articles. They're somewhat important, but they're not as crucial a topic to be covered as the dynamic properties (that the uncovered yet mentioned features when introducing the meta-object systems). However, as a continuation to learn the meta-object system, I eventually found out that you would prefer it if you learned the smart pointers and did a good practice on them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;QPointer&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Close analogy&lt;/strong&gt;: &lt;code&gt;std::auto_ptr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A typical template class &lt;code&gt;QPointer&lt;/code&gt; is responsible for supervising the pointers of &lt;code&gt;QObject&lt;/code&gt; (and its derivatives, of course). You could use it as an absolutely ordinary C/C++ pointer using the operator &lt;code&gt;-&amp;gt;&lt;/code&gt;, and also you could access the pointer's meta information via the &lt;code&gt;.&lt;/code&gt; operator and the methods with self-explanatory names like &lt;code&gt;QPointer&amp;lt;T&amp;gt;::clear&lt;/code&gt;, &lt;code&gt;QPointer&amp;lt;T&amp;gt;::data&lt;/code&gt;, &lt;code&gt;QPointer&amp;lt;T&amp;gt;::swap&lt;/code&gt; and &lt;code&gt;QPointer&amp;lt;T&amp;gt;::isNull&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;Particularly the method &lt;code&gt;QPointer&amp;lt;T&amp;gt;::isNull&lt;/code&gt; checks that if the supervised &lt;code&gt;QObject&lt;/code&gt; instance is usable. Under that circumstance, the method will return &lt;code&gt;true&lt;/code&gt;. Otherwise, it will return &lt;code&gt;false&lt;/code&gt; and the &lt;code&gt;QPointer&lt;/code&gt;'s data will automatically be &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The magic happens when &lt;code&gt;QPointer&lt;/code&gt; overrode operators like &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;!=&lt;/code&gt;, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Practicality and usages
&lt;/h2&gt;

&lt;p&gt;If you kept a primitive C++ pointer to an object of any type and when the pointer is deleted (that it would become a raw pointer), if you accessed it, a segmentation fault error would arise (as shown in the above picture). Instead, you found out that it is more convenient and practical to use the &lt;code&gt;QPointer&lt;/code&gt; class (or other Qt-made memory managements classes). Here's a small example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="n"&gt;QPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;QLabel&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;QLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Testing"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// ... (may be destructive operations to the label object)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, remember not to add an asterisk after the &lt;code&gt;QLabel&lt;/code&gt; lexeme (as in &lt;code&gt;QPointer&amp;lt;QLabel*&amp;gt;&lt;/code&gt;, which is wrong and produces a &lt;code&gt;QLabl**&lt;/code&gt; class).&lt;/p&gt;

&lt;p&gt;Also, the &lt;code&gt;label&lt;/code&gt; is automatically converted to &lt;code&gt;bool&lt;/code&gt;, which is approximately similar to determining the accessibility of an ordinary and primitive C++ pointer, except that the former one uses Qt code-bases and use smarter methods (for example, the above code will be converted to &lt;code&gt;if ((!label.isNull()) &amp;amp;&amp;amp; (label != 0))&lt;/code&gt;), while the latter one just check if the pointer is equal to &lt;code&gt;0x0&lt;/code&gt; or not, which might not be effective in checking dangling pointers.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;QSharedPointer&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Close analogy&lt;/strong&gt;: &lt;code&gt;std::shared_ptr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There's an even more meticulous class called &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It uses reference counting internally, i.e. (for beginners) if the object belonging to &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt; was being deleted, provided that no other objects referenced it (i.e. its reference count was 0), then such object (which was pointed by the pointer) was deleted.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; If you gave a pointer to &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt;, do &lt;em&gt;not&lt;/em&gt; pass it as an argument directly as a &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt; (e.g. given the code &lt;code&gt;Obj*obj=new Obj; QSharedPointer&amp;lt;Obj&amp;gt; objPtr(obj); void func(QSharedPointer&amp;lt;Obj&amp;gt;&amp;amp;)&lt;/code&gt;, do call &lt;code&gt;func(objPtr)&lt;/code&gt; instead of &lt;code&gt;func(obj)&lt;/code&gt;). Also, do &lt;em&gt;not&lt;/em&gt; delete the pointer elsewhere throughout your codebase. Do &lt;em&gt;not&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For the reason why, as you might have already figured out, is that this would cause the reference counting system to moan. Given that it could not handle deletion of primitive pointers using the primitive &lt;code&gt;delete&lt;/code&gt; operator elsewhere, the reference counting system hence omitted the &lt;em&gt;existence&lt;/em&gt; of such code. Then, accessing it might cause &lt;code&gt;SIGSEGV&lt;/code&gt; (as you all have been... well, I never rub salt into the wound).&lt;/p&gt;

&lt;p&gt;Instead, you could use the &lt;code&gt;=&lt;/code&gt; operator to copy a &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt; type to another, and they both shared the same pointer. This time, the internal reference counting will be correctly increased by 1, and Qt could assure that you're pointer-safe and never encounter any &lt;code&gt;SIGSEGV&lt;/code&gt; (as you... well, well).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Practicality and usages
&lt;/h2&gt;

&lt;p&gt;If you do not want to care about when and where your object will be deleted (in the meantime you hope that it will not be deleted), &lt;em&gt;and&lt;/em&gt; the accomplish the garbage collection feature, use &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt;. Here's a simple example, and I just need a &lt;code&gt;main.cpp&lt;/code&gt; file with &lt;code&gt;&amp;lt;QtCore&amp;gt;&lt;/code&gt; only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QDebug&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QSharedPointer&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Debugger&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Debugger constructor called."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Debugger destructor called."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Debugger debugged called:"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"    "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ImportantObject&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;ImportantObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;ImportantObject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;m_debugger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%1 exit"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setDebugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QSharedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;m_debugger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;QSharedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m_debugger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Scopes:&lt;/span&gt;
    &lt;span class="c1"&gt;//     Think like functions, where the variables are not usable outside of it&lt;/span&gt;
    &lt;span class="c1"&gt;//     Sometimes called closures&lt;/span&gt;
    &lt;span class="n"&gt;ImportantObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ImportantObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Important Object I"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;QSharedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;QSharedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;copiedDebugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setDebugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;ImportantObject&lt;/span&gt; &lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Important Object II"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setDebugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Debugger constructor called.

Debugger debugged called:
     "Important Object II exit"
Debugger debugged called:
     "Important Object I exit"
Debugger destructor called.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is evident that the memory is automatically managed.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;QWeakPointer&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Close analogy&lt;/strong&gt;: &lt;code&gt;std::weak_ptr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Everyone does not want to be weak, except for memory management. A &lt;em&gt;weak pointer&lt;/em&gt; is weak, because you could not access the pointer directly (only possible through &lt;code&gt;QWeakPointer&amp;lt;T&amp;gt;::data&lt;/code&gt;); it is not guaranteed to be safe to access the pointer (i.e. there are chances that the referenced pointer was already deleted).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;QWeakPointer&amp;lt;T&amp;gt;&lt;/code&gt; was largely identical to &lt;code&gt;QPointer&amp;lt;T&amp;gt;&lt;/code&gt;, except that it could not use solely, and it must be paired up with &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt;. Also, though it could be converted to an equivalent &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt; object through &lt;code&gt;QWeakPointer&amp;lt;T&amp;gt;::toStrongRef&lt;/code&gt;, it is too inconvenient that you still need to check if the upgraded &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt; object is null or not.&lt;/p&gt;

&lt;p&gt;If it is &lt;em&gt;that&lt;/em&gt; troublesome, why some of us still opt to use it?&lt;/p&gt;

&lt;p&gt;That's approximately equal to the relationship of &lt;code&gt;std::string_view&lt;/code&gt; and &lt;code&gt;std::string&lt;/code&gt;. In the former one, you could not assign, modify, twist and bend the string. You could &lt;em&gt;only&lt;/em&gt; view it. However, some of us still opt to use it because it's &lt;em&gt;faster&lt;/em&gt;, as the indexing and modification processes are largely omitted. There's a similar analogy between a &lt;code&gt;tuple&lt;/code&gt; and a &lt;code&gt;list&lt;/code&gt; in Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practicality and usages
&lt;/h2&gt;

&lt;p&gt;It's speedy and satisfactory enough if we only need an &lt;em&gt;observer&lt;/em&gt; to see if the object is null or not, rather than doing something to it.&lt;/p&gt;

&lt;p&gt;So, here's a modified example from the example above, incorporating &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt; and &lt;code&gt;QWeakPointer&amp;lt;T&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QDebug&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QSharedPointer&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Debugger&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Debugger constructor called."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Debugger destructor called."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Debugger debugged called:"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"    "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ImportantObject&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;ImportantObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;ImportantObject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;m_debugger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%1 exit"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setDebugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QSharedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;m_debugger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;QSharedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m_debugger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Scopes:&lt;/span&gt;
    &lt;span class="c1"&gt;//     Think like functions, where the variables are not usable outside of it&lt;/span&gt;
    &lt;span class="c1"&gt;//     Sometimes called closures&lt;/span&gt;
    &lt;span class="n"&gt;ImportantObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ImportantObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Important Object I"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;QSharedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;QWeakPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;weakReference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;weakReference&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isNull&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"At I: Weak reference is not null&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;QSharedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;copiedDebugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;weakReference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;copiedDebugger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setDebugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;ImportantObject&lt;/span&gt; &lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Important Object II"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setDebugger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;weakReference&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isNull&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"At II: Weak reference is not null&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Debugger constructor called.

At I: Weak reference is not null

Debugger debugged called:
     "Important Object II exit"

At II: Weak reference is not null

Debugger debugged called:
     "Important Object I exit"

Debugger destructor called.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;code&gt;QScopedPointer&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Close analogy&lt;/strong&gt;: &lt;code&gt;std::unique_ptr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;QScopedPointer&amp;lt;T&amp;gt;&lt;/code&gt; uses the operator &lt;code&gt;new&lt;/code&gt; to dynamically allocate objects on the stack, and it assures that at any time the dynamically created objects could be deleted successfully.&lt;/p&gt;

&lt;p&gt;If you give the ownership of a certain object to &lt;code&gt;QScopedPointer&amp;lt;T&amp;gt;&lt;/code&gt;, retrieve it back is not possible. Also, it never transfers ownership to any other object or smart pointer.&lt;/p&gt;

&lt;p&gt;Note that you could even assign the method of deletion during construction.&lt;/p&gt;

&lt;p&gt;There are four built-in allocation helpers provided by &lt;code&gt;QScopedPointer&lt;/code&gt;, namely:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;QScopedPointerDeleter&lt;/code&gt; uses &lt;code&gt;delete&lt;/code&gt; and &lt;code&gt;new&lt;/code&gt; to allocate objects;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;QScopedPointerArrayDeleter&lt;/code&gt; uses &lt;code&gt;delete[]&lt;/code&gt; and &lt;code&gt;new[]&lt;/code&gt; to allocate objects;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;QScopedPointerPodDeleter&lt;/code&gt; (where &lt;code&gt;Pod&lt;/code&gt; means "Plain Old Data") uses C's (that's why it's &lt;em&gt;plain&lt;/em&gt; and &lt;em&gt;old&lt;/em&gt;) &lt;code&gt;free&lt;/code&gt; and &lt;code&gt;malloc&lt;/code&gt; to allocate objects; and&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;QScopedPointerDeleteLater&lt;/code&gt; uses &lt;code&gt;QObject::deleteLater&lt;/code&gt; to delete objects wisely. Note that (presumably) it raises error on scoped pointers of non-&lt;code&gt;QObject&lt;/code&gt;-descendant.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;They could be used like (from the official Qt manual):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;QScopedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QScopedPointerArrayDeleter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arrayPointer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="n"&gt;QScopedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QScopedPointerPodDeleter&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;podPointer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I said &lt;em&gt;built-in&lt;/em&gt;, because the mighty Qt framework even allows you to create your own allocation helpers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;ScopedPointerCustomDeleter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MyCustomClass&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;myCustomDeallocator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;QScopedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyCustomClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ScopedPointerCustomDeleter&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;customPointer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;MyCustomClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... Where there &lt;em&gt;must&lt;/em&gt; be a method &lt;code&gt;static public inline void cleanup(TargetClass *pointer)&lt;/code&gt; &lt;del&gt;(Not Java though!)&lt;/del&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Practicality and usage
&lt;/h2&gt;

&lt;p&gt;If you had many control statements that were way too complex, you might want to use a &lt;code&gt;QScopedPointer&amp;lt;T&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Enum&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QScopedPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;externalProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;finalProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anotherProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;errorProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yetAnother&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;externalProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;yetAnotherProcessOtherThanThose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;errorProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you do not use &lt;code&gt;QScopedPointer&amp;lt;T&amp;gt;&lt;/code&gt;, it would be a mess.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;QObject&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;QObject&lt;/code&gt; is sometimes considered as also a half smart pointer. It's because that &lt;code&gt;QObject&lt;/code&gt; shows partial memory managements, but it hardly demonstrated as mighty management methods as of &lt;code&gt;QScopedPointer&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code&gt;QSharedPointer&amp;lt;T&amp;gt;&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;You have already seen many &lt;code&gt;QObject&lt;/code&gt; descendants having the following parameter at their constructors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;Constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As an addendum to the "Hello, World!" example, let me now elucidate why setting the parent object transfers the ownership of itself to its parent, and also when the parent got destroyed, it also got destroyed.&lt;/p&gt;

&lt;p&gt;Here's the complete destructor of The Base Class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*!
    Destroys the object, deleting all its child objects.

    All signals to and from the object are automatically disconnected, and
    any pending posted events for the object are removed from the event
    queue. However, it is often safer to use deleteLater() rather than
    deleting a QObject subclass directly.

    \warning All child objects are deleted. If any of these objects
    are on the stack or global, sooner or later your program will
    crash. We do not recommend holding pointers to child objects from
    outside the parent. If you still do, the destroyed() signal gives
    you an opportunity to detect when an object is destroyed.

    \warning Deleting a QObject while it is handling an event
    delivered to it can cause a crash. You must not delete the QObject
    directly if it exists in a different thread than the one currently
    executing. Use deleteLater() instead, which will cause the event
    loop to delete the object after all pending events have been
    delivered to it.

    \sa deleteLater()
*/&lt;/span&gt;

&lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="o"&gt;::~&lt;/span&gt;&lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Q_D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;wasDeleted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;blockSig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// unblock signals so we always emit destroyed()&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bindingStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// this might be the case after an incomplete thread-move&lt;/span&gt;
        &lt;span class="c1"&gt;// remove this object from the pending list in that case&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QThread&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ownThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;privThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;QThreadPrivate&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                        &lt;span class="n"&gt;QObjectPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ownThread&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="n"&gt;privThread&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;removeObjectWithPendingBindingStatusChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// If we reached this point, we need to clear the binding data&lt;/span&gt;
    &lt;span class="c1"&gt;// as the corresponding properties are no longer useful&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;clearBindingStorage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;QtSharedPointer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ExternalRefCountData&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sharedRefcount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;sharedRefcount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadRelaxed&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sharedRefcount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sharedRefcount&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;strongref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadRelaxed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;qWarning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"QObject: shared QObject was deleted directly. The program is malformed and may crash."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="c1"&gt;// but continue deleting, it's too late to stop anyway&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// indicate to all QWeakPointers that this QObject has now been deleted&lt;/span&gt;
        &lt;span class="n"&gt;sharedRefcount&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;strongref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;storeRelaxed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;sharedRefcount&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;weakref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deref&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;sharedRefcount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;wasWidget&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;isSignalConnected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;destroyed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;isDeletingChildren&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;declarativeData&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;QAbstractDeclarativeData&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;destroyed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;QAbstractDeclarativeData&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;destroyed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;declarativeData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;QObjectPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConnectionData&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;connections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadAcquire&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;currentSender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;currentSender&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;receiverDeleted&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;currentSender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;QBasicMutex&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;signalSlotMutex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signalSlotLock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;QMutexLocker&lt;/span&gt; &lt;span class="nf"&gt;locker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signalSlotMutex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// disconnect all receivers&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;receiverCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;signalVectorCount&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;receiverCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;QObjectPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConnectionList&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;connectionList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;connectionsForSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QObjectPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;connectionList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadRelaxed&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;Q_ASSERT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadAcquire&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

                &lt;span class="n"&gt;QBasicMutex&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signalSlotLock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadRelaxed&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
                &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;needToUnlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;QOrderedMutexLocker&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;relock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signalSlotMutex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;connectionList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadAcquire&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadAcquire&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;removeConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                    &lt;span class="n"&gt;Q_ASSERT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;connectionList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadRelaxed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;needToUnlock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;unlock&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="cm"&gt;/* Disconnect all senders:
         */&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QObjectPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;senders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Q_ASSERT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadAcquire&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
            &lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="c1"&gt;// Send disconnectNotify before removing the connection from sender's connection list.&lt;/span&gt;
            &lt;span class="c1"&gt;// This ensures any eventual destructor of sender will block on getting receiver's lock&lt;/span&gt;
            &lt;span class="c1"&gt;// and not finish until we release it.&lt;/span&gt;
            &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;disconnectNotify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QMetaObjectPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;metaObject&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;signal_index&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="n"&gt;QBasicMutex&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signalSlotLock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;needToUnlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;QOrderedMutexLocker&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;relock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signalSlotMutex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="c1"&gt;//the node has maybe been removed while the mutex was unlocked in relock?&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;senders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// We hold the wrong mutex&lt;/span&gt;
                &lt;span class="n"&gt;Q_ASSERT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;needToUnlock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;unlock&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;QObjectPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConnectionData&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;senderData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;d_func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;connections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadRelaxed&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;Q_ASSERT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;senderData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;QtPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;QSlotObjectBase&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;slotObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;isSlotObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;slotObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;slotObj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;isSlotObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;senderData&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;removeConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="cm"&gt;/*
              When we unlock, another thread has the chance to delete/modify sender data.
              Thus we need to call cleanOrphanedConnections before unlocking. We use the
              variant of the function which assumes that the lock is already held to avoid
              a deadlock.
              We need to hold m, the sender lock. Considering that we might execute arbitrary user
              code, we should already release the signalSlotMutex here – unless they are the same.
            */&lt;/span&gt;
            &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;locksAreTheSame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signalSlotMutex&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;locksAreTheSame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="n"&gt;locker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unlock&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;senderData&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;cleanOrphanedConnections&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                        &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;QObjectPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ConnectionData&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AlreadyLockedAndTemporarilyReleasingLock&lt;/span&gt;
                        &lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;needToUnlock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;unlock&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;locksAreTheSame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// otherwise already unlocked&lt;/span&gt;
                &lt;span class="n"&gt;locker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unlock&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slotObj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="n"&gt;slotObj&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;destroyIfLastRef&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;locker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relock&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// invalidate all connections on the object and make sure&lt;/span&gt;
        &lt;span class="c1"&gt;// activate() will skip them&lt;/span&gt;
        &lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;currentConnectionId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;storeRelaxed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deref&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;connections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;storeRelaxed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;deleteChildren&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Q_UNLIKELY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qtHookData&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;QHooks&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;RemoveQObject&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
        &lt;span class="k"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;QHooks&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;RemoveQObjectCallback&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qtHookData&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;QHooks&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;RemoveQObject&lt;/span&gt;&lt;span class="p"&gt;])(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;Q_TRACE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QObject_dtor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// remove it from parent object&lt;/span&gt;
        &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setParent_helper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In simple English, there are five crucial steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dereferenciation of the &lt;code&gt;QWeakPointer&amp;lt;T&amp;gt;&lt;/code&gt; weak references;&lt;/li&gt;
&lt;li&gt;Emit the signal &lt;code&gt;QObject::destroyed&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;Disconnect all signal-slots connections established;&lt;/li&gt;
&lt;li&gt;Delete the children; and&lt;/li&gt;
&lt;li&gt;Remove itself from the parent object.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, if we have a class that could automatically manages such processes, it will be much more convenient. The class should supervise as much &lt;code&gt;QObject&lt;/code&gt; as possible just like &lt;code&gt;QPointer&amp;lt;T&amp;gt;&lt;/code&gt;, and it could demonstrate automatic memory managements just like &lt;code&gt;QScopedPointer&amp;lt;T&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Introducing: &lt;code&gt;QObjectCleanupHandler&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You could use &lt;code&gt;QObjectCleanupHandler::add&lt;/code&gt; to add object, &lt;code&gt;isEmpty&lt;/code&gt; to determine if there is still living &lt;code&gt;QObject&lt;/code&gt;'s, and you could also clear all objects using &lt;code&gt;QObjectCleanupHandler::clear&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practicality and usage
&lt;/h2&gt;

&lt;p&gt;I would normally make this class a resource manager, and I suggest you doing so as well. If you have multiple &lt;code&gt;QObject&lt;/code&gt;'s that are roughly identical, you use &lt;code&gt;QObjectCleanupHandler&lt;/code&gt;. Personally, I think that this one is useful.&lt;/p&gt;

&lt;p&gt;Here's a minimalistic example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;SomethingsManager&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Manager&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Manager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="n"&gt;QObjectCleanupHandler&lt;/span&gt; &lt;span class="n"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="n"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do take a break and refer to some &lt;a href="https://en.cppreference.com/w/" rel="noopener noreferrer"&gt;C++ docs&lt;/a&gt; in order to grasp the concept of &lt;code&gt;std::unique_ptr&lt;/code&gt; and &lt;code&gt;std::shared_ptr&lt;/code&gt;, etc.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>[Qt] VII. Multi-threads Signals and Slots</title>
      <dc:creator>Misinahaiya</dc:creator>
      <pubDate>Tue, 25 Feb 2025 11:22:46 +0000</pubDate>
      <link>https://dev.to/misinahaiya/qt-vii-multi-threads-signals-and-slots-4oka</link>
      <guid>https://dev.to/misinahaiya/qt-vii-multi-threads-signals-and-slots-4oka</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"&lt;em&gt;Multitasking is a part of my everyday life.&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;Monica Denise Brown&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I could feel your brain's absolute pain of binge-eating those information last article. To simplify things, before getting into the topic, I provide some useful tips in using signals:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A signal can connect to a slot;&lt;/li&gt;
&lt;li&gt;A slot can receive more than one signal;&lt;/li&gt;
&lt;li&gt;A signal can connect to more than one slot;&lt;/li&gt;
&lt;li&gt;Number and types of parameters in signals and slots must match, otherwise errors occur;&lt;/li&gt;
&lt;li&gt;The number of parameters of the slot could be less than the number of the signal's, but not more than.&lt;/li&gt;
&lt;li&gt;The connection between signals and slots is dynamic, i.e. executed in runtime.&lt;/li&gt;
&lt;li&gt;Signals could connect to another signal, but slots could not connect to another slot.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the reason why, you could refer to my previous article and use a bit of your logic.&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisites
&lt;/h1&gt;

&lt;p&gt;For complete novices at threading and concurrency (sometimes multitasking/ multi-processing), you could just consider the smallest unit of threading - threads - as different processes running at the same time.&lt;/p&gt;

&lt;p&gt;In Qt, there are many thread-safe functions and methods. The term thread-safety means that the variables and members always maintain a valid state, i.e. they're not interfered.&lt;/p&gt;

&lt;p&gt;A situation where thread-safety does not exist:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There's an integer variable, say, &lt;code&gt;a&lt;/code&gt;, initialized as 20.&lt;/li&gt;
&lt;li&gt;Thread 1 changes it to 30.&lt;/li&gt;
&lt;li&gt;Thread 2 uses it to calculate something, and process the result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This looks all fine. But what if, say:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;li&gt;Thread 1 changes it to 30.&lt;/li&gt;
&lt;li&gt;Thread 3 accidentally changes it to 0.&lt;/li&gt;
&lt;li&gt;Thread 2 uses it to calculate something, which is expected as 30 but got 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If Thread 2 is processing an internal event or process, that is still fine; at most a segmentation fault or zero-division error will occur and the application will crash. However, if Thread 2 is handling, say, OS-wide event or system-related or critical processes, then your computer will screw up. Say, if you're using your computer for an important reason, your whole day will be bad, I guess.&lt;/p&gt;

&lt;p&gt;From here, you could see how important thread safety is.&lt;/p&gt;

&lt;h1&gt;
  
  
  Connection Types
&lt;/h1&gt;

&lt;p&gt;I've introduced the basic signal connection syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signalObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slotObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Actually, there's a hidden &lt;code&gt;Qt::ConnectionType connectionType&lt;/code&gt; behind. It defaults to &lt;code&gt;Qt::ConnectionType::AutoConnection&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's the enumerators:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Qt::ConnectionType::AutoConnection&lt;/code&gt;: The default one of the &lt;code&gt;QObject::connect&lt;/code&gt; (as just mentioned). If the signal and slot are on the same thread (you'll get to know what does it mean), it becomes &lt;code&gt;Qt::ConnectionType::DirectConnection&lt;/code&gt;. Else, it becomes &lt;code&gt;Qt::ConnectionType::QueuedConnection&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Qt::ConnectionType::DirectConnection&lt;/code&gt;: As the name suggests, when the signal emits, the slot will be called instantly. This is not thread-safe (as mentioned).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Qt::ConnectionType::QueuedConnection&lt;/code&gt;: The slot will not be called instantly after the signal emits. Instead, a private class (i.e. an internal class used by Qt itself only, you could not normally access it) called &lt;code&gt;QMetaCallEvent&lt;/code&gt; is sent to the receiver. It is an event (wait for and see the later articles). The slot is executed in the receiver's thread.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Qt::ConnectionType::BlockingQueuedConnection&lt;/code&gt;: Same as &lt;code&gt;Qt::ConnectionType::QueuedConnection&lt;/code&gt;, but the signalling thread blocks until the slot returns. You should not use this one in a single-threaded application, or else your application will become unresponsive &lt;del&gt;(But who on earth will modify these settings manually!?)&lt;/del&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;For queued connections, the thread is actually called by &lt;code&gt;QApplication::postEvent()&lt;/code&gt;. This explains that why it usually calls the slot &lt;em&gt;inside&lt;/em&gt; the thread that the object of the slot currently lives in.&lt;/p&gt;

&lt;p&gt;Also, for queued connections, I suggest you explicitly pointing out to the compiler that you're using queued connections, as it makes the code more readable and reduces error occurrences (well, probably).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are two flags added so that you could use the a bitwise OR operator (&lt;code&gt;|&lt;/code&gt;) to combine them with the above connection types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Qt::ConnectionType::UniqueConnection&lt;/code&gt;: Ensures the uniqueness of the connection, i.e. &lt;code&gt;QObject::connect&lt;/code&gt; will fail if the same signal has already been connected to the same slot of the same pair of objects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Qt::ConnectionType::SingleShotConnection&lt;/code&gt;: Ensures the singleness of the emission, i.e. &lt;code&gt;emit&lt;/code&gt;-ing the signal will not work if the signal has already been emitted once. That is, the connection will be automatically broken after the signal is emitted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do remember that you could actually use &lt;code&gt;QObject::sender&lt;/code&gt; to access the sender of the signal; yet this violates the design patterns of OOP (object-oriented programming). In addition, there are few points to know in using &lt;code&gt;sender()&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Its return type is &lt;code&gt;QObject&lt;/code&gt;; you need to use &lt;code&gt;qobject_cast&lt;/code&gt; to convert it to the desired type (wait and see the article introducing the dynamic system of Qt).&lt;/li&gt;
&lt;li&gt;An invalid pointer is returned when your function/ method scope is &lt;em&gt;not&lt;/em&gt; a signal (well, fair enough. How on earth could you ask your fish how many Spanish words he or she knows?)&lt;/li&gt;
&lt;li&gt;An invalid pointer is returned when your slot does not live in the same thread as the sender (well, still fair enough. How could you possibly send a message to some random aliens that live 8000 light years away? By telepathy?)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, it's &lt;em&gt;wetter&lt;/em&gt; than the theories and explanations of the previous article, but it's still very &lt;em&gt;dry&lt;/em&gt;. So, I'll craft yet another mini-example showcasing the mighty trans-thread communication system &lt;em&gt;of&lt;/em&gt; the signal-slot mechanism &lt;em&gt;of&lt;/em&gt; the meta-object system &lt;em&gt;of&lt;/em&gt; the module &lt;code&gt;&amp;lt;QtCore&amp;gt;&lt;/code&gt; &lt;em&gt;of&lt;/em&gt; Qt!&lt;/p&gt;

&lt;h1&gt;
  
  
  Little Example
&lt;/h1&gt;

&lt;p&gt;There's a little, simple and intuitive app, say - the corresponding project is titled as "Countdown10Seconds" - which counts down 10 seconds commencing from the start of the app (to be precise, it is the time when the thread receives the start-counting signal; and to be even more precise, the term "signal" I have just used has nothing to do with the "signal" as in signal-slot connections). There is a label showing you the thread information, remaining seconds, etc. After the timeout, there will be a newline appended to the label's text, which read "10 seconds have passed."&lt;/p&gt;

&lt;p&gt;We have to create 2 classes (i.e. 4 files using the language spoken by Qt Creator), &lt;code&gt;SecondObject&lt;/code&gt; and &lt;code&gt;CountThread&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Without further ado, as my usual practice, I will show you my code and explain as detailed as possible.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;secondobject.h&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef SECONDOBJECT_H
#define SECONDOBJECT_H
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QObject&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SecondObject&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Q_OBJECT&lt;/span&gt;

&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;explicit&lt;/span&gt; &lt;span class="n"&gt;SecondObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nl"&gt;signals:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;secondPassed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;slots&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;onTimeout&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;m_seconds&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cp"&gt;#endif // SECONDOBJECT_H
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;secondobject.cpp&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"secondobject.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QThread&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;

&lt;span class="n"&gt;SecondObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SecondObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;m_seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;SecondObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onTimeout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_seconds&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;identifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;QThread&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;currentThreadId&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;secondPassed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;m_seconds&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;QThread&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;currentThread&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;countthread.h&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef COUNTTHREAD_H
#define COUNTTHREAD_H
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QThread&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QPointer&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CountThread&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;QThread&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Q_OBJECT&lt;/span&gt;

&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;CountThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;CountThread&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;

&lt;span class="k"&gt;protected&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;QPointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m_parent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cp"&gt;#endif // COUNTTHREAD_H
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;countthread.cpp&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"countthread.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"secondobject.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"mainwindow.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QTimer&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;

&lt;span class="n"&gt;CountThread&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CountThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_parent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;CountThread&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QTimer&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;SecondObject&lt;/span&gt; &lt;span class="n"&gt;secondObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 10 seconds&lt;/span&gt;
    &lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;QTimer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;secondObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;SecondObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onTimeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1000 milliseconds, discussed already earlier&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;m_parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isNull&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;secondObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;SecondObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;secondPassed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;m_parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onSecondLeft&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mainwindow.h&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef MAINWINDOW_H
#define MAINWINDOW_H
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QMainWindow&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QLabel&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainWindow&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;QMainWindow&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Q_OBJECT&lt;/span&gt;

&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QWidget&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;slots&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;onSecondLeft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="n"&gt;QLabel&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m_label&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="cp"&gt;#endif // MAINWINDOW_H
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mainwindow.cpp&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"mainwindow.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"countthread.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QWidget&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;QMainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;m_label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;QLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;m_label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setAlignment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AlignmentFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AlignCenter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;m_label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setMargin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;setCentralWidget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_label&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;setWindowTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Misinahaiya - Multi-thread Signals"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;CountThread&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;CountThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;CountThread&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;finished&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;CountThread&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;deleteLater&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::~&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onSecondLeft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;QString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"There is/are %1 second(s) left, and the current (executing) thread identifier is %2.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
                             &lt;span class="s"&gt;"The second-counting thread's identifier is %3."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;QThread&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;currentThreadId&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;10 seconds have passed."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;m_label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;main.cpp&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"mainwindow.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QApplication&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QApplication&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;MainWindow&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;Here are the two tiny images showcasing the results of our tiny ~~ and useless~~ application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Not yet been timeout:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frqvv83szatv2so62o55b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frqvv83szatv2so62o55b.png" alt="Not yet been timeout" width="613" height="164"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Timeout already:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fba6ptbsx9yjmq3f9dt8k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fba6ptbsx9yjmq3f9dt8k.png" alt="Timeout already" width="613" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanations
&lt;/h2&gt;

&lt;p&gt;Analyzing the headers, we could see that the &lt;code&gt;SecondObject::secondPassed&lt;/code&gt; signal will be emitted every second.&lt;/p&gt;

&lt;p&gt;The counting thread inherits &lt;code&gt;QThread&lt;/code&gt; as you could see, and &lt;code&gt;QThread&lt;/code&gt; is the most basic threading class in Qt (somehow canonical too, you will be introduced later after you have mastered the use of Qt GUI and Qt Widgets).&lt;/p&gt;

&lt;p&gt;To make use of the &lt;code&gt;QThread::start&lt;/code&gt; method and send signals efficiently, I overrode the virtual method &lt;code&gt;QThread::run&lt;/code&gt;. As the candle wick (the initiator, sorry for the bewilderment if you do not understand my metaphor) of a &lt;code&gt;QThread&lt;/code&gt; is typically &lt;code&gt;QThread::start&lt;/code&gt;, and it calls its underlying &lt;code&gt;protected&lt;/code&gt; method &lt;code&gt;QThread::run&lt;/code&gt;, overriding it is a good way to prevent &lt;a href="https://en.wikipedia.org/wiki/Reinventing_the_wheel#In_software_development" rel="noopener noreferrer"&gt;reinventing the wheel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As the thread is considered &lt;em&gt;useless&lt;/em&gt; after it exited in this case (i.e. the program guarantees that the thread will not be reused), so it's safe to delete this on finishing calling the method &lt;code&gt;exit(0)&lt;/code&gt; (where "&lt;code&gt;0&lt;/code&gt;" symbolizes no errors occurred, same as the &lt;a href="https://tldp.org/LDP/abs/html/exitcodes.html" rel="noopener noreferrer"&gt;Unix return codes&lt;/a&gt;). However, to really &lt;em&gt;ensure&lt;/em&gt; no one is using the thread anymore, I have introduced a new method &lt;code&gt;QObject::deleteLater&lt;/code&gt; to you. It &lt;em&gt;schedules&lt;/em&gt; the removal of the unwanted object, i.e. the mighty Qt (meta-) object system has an intricate and meticulous way to ensure nothing is using the object anymore before the removal. If you're interested, please go and look at some &lt;a href="https://doc.qt.io/qt-6/qobject.html#deleteLater" rel="noopener noreferrer"&gt;docs that are better than mine&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;On introducing the &lt;code&gt;QObject::deleteLater&lt;/code&gt; slot, I think I also need to introduce a completely new class to you &lt;code&gt;QPointer&lt;/code&gt;. Have you ever used &lt;a href="https://en.cppreference.com/book/intro/smart_pointers" rel="noopener noreferrer"&gt;smart pointers&lt;/a&gt;? If yes, then it's pretty simple and similar in nature. If you haven't (oh my, are you a C programmer from the 90's?), then I'll explain it in few sentences in simple English: a smart pointer is a pointer that automatically detects the deletion of the linked object. As a pointer is a &lt;em&gt;reference&lt;/em&gt; that is &lt;em&gt;pointing&lt;/em&gt; to an object by definition, it's just a bunch of binary numbers. So, if you have a pointer pointing to &lt;code&gt;0&lt;/code&gt; (no object) or &lt;code&gt;nullptr&lt;/code&gt;, it's a null pointer; to a deleted object, it's a dangling pointer; to an invalid object, it's a raw pointer. You do not need to memorize these terms, as it is not the main point. You just need to know that there's a magical class in Qt, &lt;code&gt;QPointer&lt;/code&gt;, where it overrides the operators &lt;code&gt;.&lt;/code&gt; and &lt;code&gt;-&amp;gt;&lt;/code&gt; so that you could access meta-pointer information (like if it is deleted, null etc.) and the pointer itself.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So, the above example is a practical usage of not only &lt;code&gt;QThread&lt;/code&gt; and &lt;code&gt;QObject::connect&lt;/code&gt;, but also &lt;code&gt;QPointer&lt;/code&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One last thing is &lt;code&gt;QString&lt;/code&gt;. It's a Qt string class. We will discuss this more deeply in the following topics, covering the basic types of Qt like containers (lists, arrays, maps, etc.) and strings (&lt;code&gt;char&lt;/code&gt;s, strings, byte arrays, etc.)&lt;/p&gt;

&lt;p&gt;The entry function &lt;code&gt;main&lt;/code&gt; is generated by Qt Creator and it's very typical. So I'll not dive deep into it as I've already explained it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For convenience and conciseness, &lt;code&gt;Qt::ConnectionType::QueuedConnection&lt;/code&gt; is not used. However, for actual practice, I do suggest you adding it explicitly, but I wouldn't &lt;em&gt;urge&lt;/em&gt; you to do so as, really, it does not matter, for the &lt;code&gt;QObject::connect&lt;/code&gt; method will automatically detect so.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Right, so this chapter is considered as a little addendum to the previous ultra-long article. I know that explaining and putting bunch of examples regarding the feature-rich signal-slot system in one fell swoop would make you faint, so I'll split it into the single-threaded worked-examples (the previous one) and the multi-threaded worked-example (this one).&lt;/p&gt;

&lt;p&gt;Anyway, hopefully you got what signals really do. At least you could (almost) master the signal-slot system in Qt, and it's about 70% in terms of the weighing of the importance of the Qt meta-object system.&lt;/p&gt;

&lt;p&gt;In the next article or two, I'll be introducing the rest of the meta-object system features. Still that line, stayed tuned to this series and practice your Qt skill constantly!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>qt</category>
      <category>desktop</category>
    </item>
    <item>
      <title>[Qt] VI. Signals-slots and Meta-objects</title>
      <dc:creator>Misinahaiya</dc:creator>
      <pubDate>Mon, 24 Feb 2025 06:48:02 +0000</pubDate>
      <link>https://dev.to/misinahaiya/qt-vi-signals-slots-and-meta-objects-5hi8</link>
      <guid>https://dev.to/misinahaiya/qt-vi-signals-slots-and-meta-objects-5hi8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"&lt;em&gt;In fact, the socialization (connection) gives us the tools to fill our evolutionary roles. They are our building blocks.&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;Warren Farrell&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes, socialization is so important to us humans who are all characterized as gregarious animals million years ago. Something created by humans could &lt;em&gt;not&lt;/em&gt; be an exception as well (... well, there are still plenty of exceptions though having said that). In fact, the socialization system of Qt among classes under &lt;code&gt;QObject&lt;/code&gt;, canonically known as the signal-slot(s) connection, is worth explaining it using two or three five-thousand-word pieces of prose.&lt;/p&gt;

&lt;p&gt;Why? Because it's the heart of various Qt objects - it is the fact the most efficient, standardized, well-known and (probably) the only way to communicate between objects (including widgets and a whole bunch more that will be introduced later).&lt;/p&gt;

&lt;p&gt;Let us dive - hopefully as deep as possible - into the well-known signal-slot system, and the controller behind: the meta-object system.&lt;/p&gt;

&lt;p&gt;(In fact, I have already mentioned two or three times in my previous articles. They're too important that even a hello-world example for fresh novices requires so.)&lt;/p&gt;

&lt;h1&gt;
  
  
  The Heart's Heart of Heart
&lt;/h1&gt;

&lt;p&gt;If you peek at the official repositories of (open-source) Qt, you will soon discovered that the one with the most traffic, stars and popularity by looking at the number of forks (provided that you are using GitHub), and the most important one by inferring the name, is Qt Base. Of the ten or twenty sub-components of the Qt Base repository, by just inferring their names, you will figure out that Qt Core is the most important one. In Qt Core, the signal-slot system is the most important one.&lt;/p&gt;

&lt;p&gt;That is why it is called the heart's heart of heart.&lt;/p&gt;

&lt;p&gt;Qt could somehow be considered as a library &lt;em&gt;based on&lt;/em&gt; the signal-slot system, or more accurately, the meta-object system. The meta-object system, in plain English, is a mechanism used to build independent software components (you will soon get to know what I mean). These components need not to recognize (identify) each other at runtime, even in order to run and collaborate with one another seamlessly. After this article and probably two or three more, you will soon discover that the meta-object system &lt;em&gt;extends&lt;/em&gt; the C++ language per se and &lt;em&gt;beautifies&lt;/em&gt; it, given that you follow my steps and learn Qt patiently for a while.&lt;/p&gt;

&lt;p&gt;Why? Because the meta-object system provides 3 key features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Signal-slot system (it's way too important that it sometimes be the pronoun of the meta-object system per se though it's just a part of it; we'll be using the term "meta-object system" consistently from now on in this series. Sorry for the previous incongruousness),&lt;/li&gt;
&lt;li&gt;Dynamic runtime-data-type detection (do you believe it, It's C-plus-plus &lt;em&gt;plus&lt;/em&gt; Qt, not Python nor Ruby! How mighty is Qt), and&lt;/li&gt;
&lt;li&gt;Dynamic property system.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything in Qt is possible if and only if these 3 key features is possible and got implemented.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In this article, we'll talk about the first one first. In the next or two, I'll be cover the rest of them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The meta-object system is possible when&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The class inherits &lt;code&gt;QObject&lt;/code&gt; or any of its subclass, i.e. the class that would like to enjoy the meta-object system must be, either direct or indirect, descendant of &lt;code&gt;QObject&lt;/code&gt;. This is because the meta-object system is implemented by &lt;code&gt;QObject&lt;/code&gt; and &lt;code&gt;QMetaObject&lt;/code&gt; (you'll get to know it later in this article).&lt;/li&gt;
&lt;li&gt;As the meta-object system requires some &lt;em&gt;private&lt;/em&gt; class members, so every &lt;code&gt;QObject&lt;/code&gt; descendant needs to get a &lt;code&gt;Q_OBJECT&lt;/code&gt; macro declared in the &lt;code&gt;private:&lt;/code&gt; section of the class (owing the fact that C++ defaults members of undeclared accessibility as private members, so you could also put the macro as the first declaration of the class, which is consistent with the Qt programming style, as well as mine). Putting it in the &lt;code&gt;public:&lt;/code&gt; section is an undefined behavior (at least I have never tried it). If you do not do so, errors regarding undefined members will be raised if you try to define a signal, emit a signal (see later) in or with this class (internally and externally respectively); similarly, if you do so in a non-&lt;code&gt;QObject&lt;/code&gt;-descendant class, you'll still get a whole bunch of errors as the declaration calls some &lt;code&gt;QObject&lt;/code&gt; methods.&lt;/li&gt;
&lt;li&gt;You must have a meta-object compiler made by Qt. It's somehow like the preprocessor compiler mandated by the C++ Standard and provided by almost all C/C++ compilers. However, instead of dealing with the hash signs &lt;code&gt;#&lt;/code&gt;, it deals with the Q_OBJECT declarations, implementing the dynamic properties, etc. Unless you accidentally delete it (I won't believe there is such a careless person in the world), this condition is the easiest to fulfill by the time you finished the Qt installation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The above content looks so dry that I think we need a comprehensive and self-explanatory example to make it practical.&lt;/p&gt;

&lt;h1&gt;
  
  
  Flowers
&lt;/h1&gt;

&lt;p&gt;This time, we only build a console project. To build a console project (i.e. the project that you do not use a window or something, just use the core features of Qt), you will follow the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Launch Qt Creator (of course!)&lt;/li&gt;
&lt;li&gt;Click &lt;code&gt;File -&amp;gt; New Project&lt;/code&gt; as always&lt;/li&gt;
&lt;li&gt;Then, here's the different part: after the dialog is being launched, do manually click the "Qt Console Application" instead of the default one, "Qt Widget Application".&lt;/li&gt;
&lt;li&gt;Follow the steps in the "Hello, Qt!" article. Name your project a reasonable title, and for this time, I opt for "Flowers" instead of any other strange things.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're curious, you could glimpse at the newly created &lt;code&gt;CMakeLists.txt&lt;/code&gt; file, and it differs from the previous files at: all &lt;code&gt;Widget&lt;/code&gt; entries are replaced by the word &lt;code&gt;Core&lt;/code&gt;. That indicates that using widgets is invalid and may results in a preprocessing error or a linkage error (for C++ intermediate learners, just think of some errors irrelevant to your code). But since we're using the meta-object system today, using the cores is already enough.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using the &lt;code&gt;&amp;lt;QtCore&amp;gt;&lt;/code&gt; module is indeed &lt;em&gt;already&lt;/em&gt; enough, but only having the &lt;code&gt;main.cpp&lt;/code&gt; file is indeed &lt;em&gt;not&lt;/em&gt; enough. To set things up, we need to create four additional files, namely &lt;code&gt;flower.h&lt;/code&gt;, &lt;code&gt;flower.cpp&lt;/code&gt;, &lt;code&gt;picker.h&lt;/code&gt; and &lt;code&gt;picker.cpp&lt;/code&gt;. We could just create them by pressing the hotkey (&lt;code&gt;Ctrl+N&lt;/code&gt;) or navigating to the action &lt;code&gt;File -&amp;gt; New File&lt;/code&gt; (I've already taught it before in somewhere, haven't I), then choose the option "C++ Class".&lt;/p&gt;

&lt;p&gt;Enter the name of the class. After entering the name, you could soon realize that the "Header file:" and "Source file:" fields are already filled of their required relative directories automatically by Qt Creator. How helpful it is!&lt;/p&gt;

&lt;p&gt;As the two classes, namely &lt;code&gt;Flower&lt;/code&gt; and &lt;code&gt;Picker&lt;/code&gt; need to inherit &lt;code&gt;QObject&lt;/code&gt;, select the option "QObject" in the combo-box labeled as "Base class:". Qt Creator is so helpful that it automatically helps you in selecting the often-omitted options: &lt;code&gt;#include &amp;lt;QObject&amp;gt;&lt;/code&gt; and add the macro directive &lt;code&gt;Q_OBJECT&lt;/code&gt;. Without these two, our code cannot function. But it's the two cute (?) creatures that we often forget.&lt;/p&gt;

&lt;p&gt;Anyways, let's press "Next" and your final dialogs should look like these ones:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhld42b6xfuv21y4h7f1y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhld42b6xfuv21y4h7f1y.png" alt="Flower" width="800" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zpzben3ahgol46kob5x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zpzben3ahgol46kob5x.png" alt="Picker" width="800" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, let's do some coding (finally!)&lt;/p&gt;

&lt;p&gt;The code is pretty standardized, so I will just pick some important spots to explain. The other parts are often self-explanatory. (I am trying my best to reduce your reading stress in this article as this one should be the longest one throughout the whole series; but forgive me, because the meta-object system is literally the &lt;em&gt;behemoth&lt;/em&gt; in Qt and explaining it takes two or three longest articles like this one.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Full code
&lt;/h2&gt;

&lt;p&gt;Let me show you my code first. I'll then explain it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;flower.h&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef FLOWER_H
#define FLOWER_H
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QObject&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QTimer&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Flower&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Q_OBJECT&lt;/span&gt;

&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;explicit&lt;/span&gt; &lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;secToBloom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bloomToWither&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nl"&gt;signals:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;bloomSignal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;witherSignal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="n"&gt;slots&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;onFlowerBloom&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="n"&gt;QTimer&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m_timer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;m_secToBloom&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;m_bloomToWither&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cp"&gt;#endif // FLOWER_H
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;flower.cpp&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"flower.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;

&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;secToBloom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bloomToWither&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;m_timer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;QTimer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="n"&gt;m_secToBloom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secToBloom&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1'000&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;m_bloomToWither&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bloomToWither&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1'000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;m_timer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setSingleShot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_timer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;QTimer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onFlowerBloom&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;m_timer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_secToBloom&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onFlowerBloom&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;bloomSignal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_timer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;QTimer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onFlowerBloom&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_timer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;QTimer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;witherSignal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;m_timer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_bloomToWither&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;picker.h&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef PICKER_H
#define PICKER_H
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QObject&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Picker&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Q_OBJECT&lt;/span&gt;

&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;explicit&lt;/span&gt; &lt;span class="n"&gt;Picker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;slots&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;onFlowerBloom&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;onFlowerWither&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cp"&gt;#endif // PICKER_H
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;picker.cpp&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"picker.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QDebug&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QCoreApplication&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;

&lt;span class="n"&gt;Picker&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Picker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QString&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;m_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Picker&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onFlowerBloom&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"I,"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;", picked a flower when it bloomed!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Picker&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onFlowerWither&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"[Sobbing] I,"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;m_name&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;", witnessed the death of a beautiful flower. How sad!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;QCoreApplication&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above example, we could conclude that the basic syntax for connecting the "sender" (signal) to the "receiver" (slot) is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;objectSender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signalPointer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;objectReceiver&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slotPointer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're connecting &lt;em&gt;inside&lt;/em&gt; a &lt;code&gt;QObject&lt;/code&gt; subclass, you may omit the member/ scope indicator &lt;code&gt;QObject::&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From the above example, we also explored the three different keywords introduced by Qt: &lt;code&gt;signal&lt;/code&gt;, &lt;code&gt;slot&lt;/code&gt; and &lt;code&gt;emit&lt;/code&gt;. The &lt;code&gt;signal&lt;/code&gt; keyword introduces a list of signals in the class member declaration lists until the next access specifier meets, so does the &lt;code&gt;slot:&lt;/code&gt; keyword. The &lt;code&gt;emit&lt;/code&gt; keyword, followed by a signal, emits that signal.&lt;/p&gt;

&lt;p&gt;We could see that we could connect a same signal to multiple slots (as in the &lt;code&gt;Flower::bloomSignal&lt;/code&gt;) too.&lt;/p&gt;

&lt;p&gt;We also explored the usage of a pretty handy class &lt;code&gt;QTimer&lt;/code&gt;. We'll look into it later, but you just need to remember that &lt;code&gt;QTimer::start&lt;/code&gt; starts a timer by &lt;strong&gt;milliseconds&lt;/strong&gt; (that's why the private properties &lt;code&gt;Flower::m_secToBloom&lt;/code&gt; and &lt;code&gt;Flower::m_bloomToWither&lt;/code&gt; needs to be multiplied by 1000 programmatically in the fields-initialization list), while a timer emits the &lt;code&gt;QTimer::timeout&lt;/code&gt; signal after the time is out. The &lt;code&gt;QTimer::singleShot&lt;/code&gt; method specifies that the &lt;code&gt;timeout&lt;/code&gt; signal will be emitted once only, instead of an interval of the number specified in the parameter of &lt;code&gt;QTimer::start&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, as a good practice, unless the &lt;code&gt;QObject&lt;/code&gt; subclass will not be referred by any other functions (i.e. passed in as a parameter), &lt;a href="https://doc.qt.io/qt-6/qobject.html#no-copy-constructor-or-assignment-operator" rel="noopener noreferrer"&gt;you should really use the pointer forms of the &lt;code&gt;QObject&lt;/code&gt;'s instead of the variable forms&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Additionally, you may be using some libraries such as &lt;code&gt;boost&lt;/code&gt;, which have classes or identifiers called &lt;code&gt;signals&lt;/code&gt;, &lt;code&gt;slots&lt;/code&gt; or &lt;code&gt;emit&lt;/code&gt;, i.e. the Qt identifiers clash with them. In this case, fire your &lt;code&gt;CMakeLists.txt&lt;/code&gt; up and add the following definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cmake"&gt;&lt;code&gt;&lt;span class="nb"&gt;add_definitions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;-DQT_NO_KEYWORDS&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, you will discover that you are not able to use &lt;code&gt;signals&lt;/code&gt;, &lt;code&gt;slots&lt;/code&gt; and &lt;code&gt;emit&lt;/code&gt; keywords any more. Instead, you must use &lt;code&gt;Q_SIGNALS&lt;/code&gt;, &lt;code&gt;Q_SLOTS&lt;/code&gt; and &lt;code&gt;Q_EMIT&lt;/code&gt; respectively.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If these macro names still clash, well, then, the 3-rd party library designer, you're a son-of-a-b***h, go refactor your code...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why?
&lt;/h2&gt;

&lt;p&gt;As mentioned, these 6 names are all macros (7 if you count &lt;code&gt;Q_OBJECT&lt;/code&gt;; in fact, there are hundred or more macros defined by Qt).&lt;/p&gt;

&lt;p&gt;If you go on &lt;code&gt;qobjectdefs.h&lt;/code&gt; in the Qt Base repository, you'll find the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define signals public __attribute__((annotate("qt_signal")))
#define slots __attribute__((annotate("qt_slot")))
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Q_SIGNAL&lt;/code&gt; and &lt;code&gt;Q_SLOTS&lt;/code&gt; are actually the same.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A &lt;code&gt;signal&lt;/code&gt; is basically a waiting-to-be-handled &lt;code&gt;public&lt;/code&gt; object, and the annotation will probably be ignored by the compiler (because it has been tagged &lt;code&gt;__attribute__&lt;/code&gt;, see &lt;a href="https://en.cppreference.com/w/cpp/language/attributes" rel="noopener noreferrer"&gt;here&lt;/a&gt;), so does a &lt;code&gt;slot&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As &lt;code&gt;signals&lt;/code&gt; is already been defined as &lt;code&gt;public&lt;/code&gt; while &lt;code&gt;slots&lt;/code&gt; is not, that is the reason why the syntax &lt;code&gt;public signals:&lt;/code&gt; is illegal while &lt;code&gt;public slots:&lt;/code&gt; is legal. This also explains that in, say, &lt;code&gt;private:signals: a();&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt; could still be accessed outside of the class.&lt;/p&gt;

&lt;p&gt;Same as &lt;code&gt;emit&lt;/code&gt; (or &lt;code&gt;Q_EMIT&lt;/code&gt;), if you go to &lt;code&gt;qmetamacros.h&lt;/code&gt;, you will soon discovered that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define emit
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, &lt;code&gt;emit signal();&lt;/code&gt; is just &lt;code&gt;signal();&lt;/code&gt;. Actually, when you defined the signal, the Meta-object compiler (&lt;code&gt;moc&lt;/code&gt; or &lt;code&gt;moc.exe&lt;/code&gt; if you're on Windows) compiles the signal into a function. That's why you could also connect a signal to another signal (connect &lt;code&gt;QTimer::timeout&lt;/code&gt; to &lt;code&gt;Flower::witherSignal&lt;/code&gt; above, for example).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Actually, there are also two macros you haven't been introduced yet. They are &lt;code&gt;SIGNALS&lt;/code&gt; and &lt;code&gt;SLOTS&lt;/code&gt;, where you'll find them on decade-old code. They're basically converting the name to a string of a name that is recognized by the &lt;code&gt;moc&lt;/code&gt;. Precisely, in &lt;code&gt;moc&lt;/code&gt;, &lt;code&gt;"1"&lt;/code&gt; means a slot and &lt;code&gt;"2"&lt;/code&gt; means a signal. If you, say, do the code &lt;code&gt;SIGNAL(clicked())&lt;/code&gt; and &lt;code&gt;SLOT(onClicked())&lt;/code&gt;, they output &lt;code&gt;"2clicked()"&lt;/code&gt; and &lt;code&gt;"1onClicked()"&lt;/code&gt; respectively. This is why Qt Creator warns you if you have a space between the arguments as these macros require strict formatting and strict syntax.&lt;br&gt;
We seldom use these directives now, and we better use the function pointer to pass to the &lt;code&gt;connect&lt;/code&gt; function. They're outdated, obsolete and will not be used unless we need to deal with an antique, like code written fifteen years ago where Qt 5 was still in beta-testing. Until the challenges and big projects in this series later on, we'll not be using any of them.&lt;/p&gt;

&lt;p&gt;Do notice that mix-and-match is not allowed, i.e. a function pointer connecting to a &lt;code&gt;SLOT&lt;/code&gt; macro or vice versa.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Meta-object Compiler is a True Magician
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;Q_OBJECT&lt;/code&gt; macro defines some key functions of performing the meta-object mechanism:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define Q_OBJECT \
public: \
    QT_WARNING_PUSH \
    Q_OBJECT_NO_OVERRIDE_WARNING \
    static const QMetaObject staticMetaObject; \
    virtual const QMetaObject *metaObject() const; \
    virtual void *qt_metacast(const char *); \
    virtual int qt_metacall(QMetaObject::Call, int, void **); \
    QT_TR_FUNCTIONS \
private: \
    Q_OBJECT_NO_ATTRIBUTES_WARNING \
    Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
    QT_WARNING_POP \
    QT_DEFINE_TAG_STRUCT(QPrivateSignal); \
    QT_ANNOTATE_CLASS(qt_qobject, "")
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It basically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defines a static meta-object &lt;code&gt;QMetaObject staticMetaObject&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;Overrides the virtual method &lt;code&gt;QObject::metaObject&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;Overrides the virtual method &lt;code&gt;QObject::qt_metacast&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;Does some internationalization support (we'll be discussing that later),&lt;/li&gt;
&lt;li&gt;Overrides the virtual method &lt;code&gt;QObject::qt_metacall&lt;/code&gt;, and&lt;/li&gt;
&lt;li&gt;Defines the static function &lt;code&gt;qt_static_metacall&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For your level, you need not care what are and how do these methods work. You just need to know them they're important and indispensable so as to do some signaling works.&lt;/p&gt;

&lt;p&gt;In C++, every declared functions that are called at least once must have a definition somewhere, otherwise you'll have the linkage error by the linker, saying the &lt;code&gt;Undefined reference at&lt;/code&gt; somewhere (I'm pretty sure that you all have been tortured by it, haven't you). The &lt;code&gt;moc&lt;/code&gt; compiler, in simple English, is generating the definitions of the above functions (as well as the initialization of the objects).&lt;/p&gt;

&lt;p&gt;The files are usually named identical as the host source file with a prefix &lt;code&gt;moc_&lt;/code&gt; over it, for example the &lt;code&gt;Flower&lt;/code&gt; class in &lt;code&gt;flower.cpp&lt;/code&gt; have its meta-object definitions in &lt;code&gt;moc_flower.cpp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's take a closer look on the &lt;code&gt;moc_flower.cpp&lt;/code&gt; (for &lt;code&gt;moc_picker.cpp&lt;/code&gt;, the overall structure is about the same):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/****************************************************************************
** Meta object code from reading C++ file 'flower.h'
**
** Created by: The Qt Meta Object Compiler version 68 (Qt 6.8.1)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/&lt;/span&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"../../../../flower.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QtCore/qmetatype.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QtCore/qtmochelpers.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QtCore/qxptype_traits.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'flower.h' doesn't include &amp;lt;QObject&amp;gt;."
#elif Q_MOC_OUTPUT_REVISION != 68
#error "This file was generated using the moc from 6.8.1. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif
&lt;/span&gt;
&lt;span class="cp"&gt;#ifndef Q_CONSTINIT
#define Q_CONSTINIT
#endif
&lt;/span&gt;
&lt;span class="n"&gt;QT_WARNING_PUSH&lt;/span&gt;
&lt;span class="n"&gt;QT_WARNING_DISABLE_DEPRECATED&lt;/span&gt;
&lt;span class="n"&gt;QT_WARNING_DISABLE_GCC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"-Wuseless-cast"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;qt_meta_tag_ZN6FlowerE_t&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// unnamed namespace&lt;/span&gt;


&lt;span class="cp"&gt;#ifdef QT_MOC_HAS_STRINGDATA
&lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;qt_meta_stringdata_ZN6FlowerE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;QtMocHelpers&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;stringData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"Flower"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"bloomSignal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"witherSignal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"onFlowerBloom"&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cp"&gt;#else  // !QT_MOC_HAS_STRINGDATA
#error "qtmochelpers.h not found or too old."
#endif // !QT_MOC_HAS_STRINGDATA
&lt;/span&gt;
&lt;span class="n"&gt;Q_CONSTINIT&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;qt_meta_data_ZN6FlowerE&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

 &lt;span class="c1"&gt;// content:&lt;/span&gt;
      &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;// revision&lt;/span&gt;
       &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;// classname&lt;/span&gt;
       &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// classinfo&lt;/span&gt;
       &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// methods&lt;/span&gt;
       &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// properties&lt;/span&gt;
       &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// enums/sets&lt;/span&gt;
       &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// constructors&lt;/span&gt;
       &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;// flags&lt;/span&gt;
       &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;// signalCount&lt;/span&gt;

 &lt;span class="c1"&gt;// signals: name, argc, parameters, tag, flags, initial metatype offsets&lt;/span&gt;
       &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x06&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="cm"&gt;/* Public */&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="mi"&gt;33&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x06&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="cm"&gt;/* Public */&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

 &lt;span class="c1"&gt;// slots: name, argc, parameters, tag, flags, initial metatype offsets&lt;/span&gt;
       &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x09&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="cm"&gt;/* Protected */&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

 &lt;span class="c1"&gt;// signals: parameters&lt;/span&gt;
    &lt;span class="n"&gt;QMetaType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;QMetaType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

 &lt;span class="c1"&gt;// slots: parameters&lt;/span&gt;
    &lt;span class="n"&gt;QMetaType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

       &lt;span class="mi"&gt;0&lt;/span&gt;        &lt;span class="c1"&gt;// eod&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;Q_CONSTINIT&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt; &lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;staticMetaObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SuperData&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;staticMetaObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;qt_meta_stringdata_ZN6FlowerE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;offsetsAndSizes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;qt_meta_data_ZN6FlowerE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;qt_static_metacall&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;qt_incomplete_metaTypeArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;qt_meta_tag_ZN6FlowerE_t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// Q_OBJECT / Q_GADGET&lt;/span&gt;
        &lt;span class="n"&gt;QtPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TypeAndForceComplete&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;true_type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// method 'bloomSignal'&lt;/span&gt;
        &lt;span class="n"&gt;QtPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TypeAndForceComplete&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;false_type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// method 'witherSignal'&lt;/span&gt;
        &lt;span class="n"&gt;QtPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TypeAndForceComplete&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;false_type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// method 'onFlowerBloom'&lt;/span&gt;
        &lt;span class="n"&gt;QtPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TypeAndForceComplete&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;false_type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;nullptr&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;qt_static_metacall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;_o&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Call&lt;/span&gt; &lt;span class="n"&gt;_c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;_t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_o&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;InvokeMetaMethod&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;_t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bloomSignal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;_t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;witherSignal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;_t&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;onFlowerBloom&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;default:&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;IndexOfMethod&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;_q_method_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::*&lt;/span&gt;&lt;span class="p"&gt;)();&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_q_method_type&lt;/span&gt; &lt;span class="n"&gt;_q_method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;bloomSignal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;_q_method_type&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;_q_method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;_q_method_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::*&lt;/span&gt;&lt;span class="p"&gt;)();&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_q_method_type&lt;/span&gt; &lt;span class="n"&gt;_q_method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;witherSignal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;_q_method_type&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;_q_method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;metaObject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;d_ptr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;metaObject&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;d_ptr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dynamicMetaObject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;staticMetaObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;qt_metacast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;_clname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;_clname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_clname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qt_meta_stringdata_ZN6FlowerE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stringdata0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;qt_metacast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_clname&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;qt_metacall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Call&lt;/span&gt; &lt;span class="n"&gt;_c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;QObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;qt_metacall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;InvokeMetaMethod&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;qt_static_metacall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;RegisterMethodArgumentMetaType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;QMetaType&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;QMetaType&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;_id&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// SIGNAL 0&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;bloomSignal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;staticMetaObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// SIGNAL 1&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;witherSignal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;staticMetaObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;QT_WARNING_POP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After analyzing the source code, the &lt;code&gt;moc&lt;/code&gt; compiler actually:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Scans the headers, and analyzes the names and the corresponding information (like slots) according to the &lt;code&gt;signals:&lt;/code&gt; and &lt;code&gt;slots:&lt;/code&gt; specifiers (actually by looking at the attributes &lt;code&gt;"qt_signals"&lt;/code&gt; and &lt;code&gt;"qt_slots"&lt;/code&gt;, etc.) and stores them into a global variable &lt;code&gt;qt_meta_stringdata_&lt;/code&gt; succeeding the class name. The strange numbers are actually internal references used by C++ storing the data of overrides, overloads and templates (and that's why we need to have &lt;code&gt;extern "C"&lt;/code&gt; when communicating with C code, as the nomenclature is different in nature; anyway, back to the topic),&lt;/li&gt;
&lt;li&gt;Annotates the methods, signals, slots, properties, enumerators, etc. and stores them into the global array &lt;code&gt;qt_meta_data_&lt;/code&gt; succeeding by an internal class name,&lt;/li&gt;
&lt;li&gt;Implements the declarations in &lt;code&gt;Q_OBJECT&lt;/code&gt;. It calls some &lt;code&gt;QObject&lt;/code&gt; functions, and that's why I have mentioned that non-&lt;code&gt;QObject&lt;/code&gt;-descendants will have some errors in using this macro. It then initializes and puts all sort of behaviors inside the &lt;code&gt;staticMetaObject&lt;/code&gt; instance, and links all implemented methods to the &lt;code&gt;staticMetaObject&lt;/code&gt; instance.&lt;/li&gt;
&lt;li&gt;Makes templates and implementations for the signals &lt;code&gt;Flower::bloomSignal&lt;/code&gt; and &lt;code&gt;Flower::witherSignal()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The signals, as in step 4, are actually regular functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Flower&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;bloomSignal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;staticMetaObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What does the &lt;code&gt;QMetaObject::activate&lt;/code&gt; do?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Connecting
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;QMetaObject::activate&lt;/code&gt; is an internal method in &lt;code&gt;QMetaObject&lt;/code&gt;. In &lt;code&gt;QMetaObject&lt;/code&gt; in the file &lt;code&gt;qobjectdefs.h&lt;/code&gt;, it defines that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// internal index-based signal activation&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;signal_index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;local_signal_index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;signal_offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;local_signal_index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking deeper into the source of &lt;code&gt;QMetaObject&lt;/code&gt;, you will soon discover that the &lt;code&gt;QMetaObject&lt;/code&gt; is just a struct with many anonymous attributes, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Q_CORE_EXPORT&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// E L L I P S E S&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// private data&lt;/span&gt;
        &lt;span class="n"&gt;SuperData&lt;/span&gt; &lt;span class="n"&gt;superdata&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;stringdata&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;StaticMetacallFunction&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;QObject&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QMetaObject&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Call&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;StaticMetacallFunction&lt;/span&gt; &lt;span class="n"&gt;static_metacall&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;SuperData&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;relatedMetaObjects&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;QtPrivate&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;QMetaTypeInterface&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;metaTypes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;extradata&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//reserved for future use&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// E L L I P S E S&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What you need to care about is that in &lt;code&gt;moc_flower.cpp&lt;/code&gt;, the variables and instances with the prefix &lt;code&gt;qt_meta_data_&lt;/code&gt; members are actually transferred to the &lt;code&gt;QMetaObject::d&lt;/code&gt; members. For example, &lt;code&gt;qt_meta_stringdata_ZN6FlowerE&lt;/code&gt; is actually given to &lt;code&gt;QMetaObject::d::stringdata&lt;/code&gt;; &lt;code&gt;qt_meta_data_ZN6FlowerE&lt;/code&gt; is actually given to &lt;code&gt;QMetaObject::d::data&lt;/code&gt;; &lt;code&gt;qt_static_metacall&lt;/code&gt; is actually given to &lt;code&gt;QMetaObject::d::static_metacall&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;QMetaObject::activate&lt;/code&gt;, as well as the &lt;code&gt;qt_static_metacall&lt;/code&gt; functions, are all functions calling a signal/ slot/ meta-method by an index. For example, in &lt;code&gt;QMetaObject::activate&lt;/code&gt;, after analyzing the source code, there are actually several steps in invoking the signals:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Testing if the signals are connected, the method returns if not,&lt;/li&gt;
&lt;li&gt;Testing the declarative data,&lt;/li&gt;
&lt;li&gt;Testing any signal interference,&lt;/li&gt;
&lt;li&gt;Getting the connectors,&lt;/li&gt;
&lt;li&gt;Calling all the slots involved&lt;/li&gt;
&lt;li&gt;Calling the additional signal interference.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These might seem a bit abstract, and for your level, you just need to know that it calls &lt;code&gt;QMetaObject::metacall&lt;/code&gt;. The latter one tests the instance of the receiver, and if it is valid, it calls its &lt;code&gt;metaCall()&lt;/code&gt; method; otherwise, it calls the &lt;code&gt;qt_metacall()&lt;/code&gt; method defined in the instance of the receiver. As long as the receiver also inherits &lt;code&gt;QObject&lt;/code&gt;, &lt;code&gt;qt_metacall&lt;/code&gt; is guaranteed to exist.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I know the indices, in order to call the &lt;code&gt;activate()&lt;/code&gt;, &lt;code&gt;metaCall()&lt;/code&gt; or &lt;code&gt;qt_metacall()&lt;/code&gt; methods?
&lt;/h3&gt;

&lt;p&gt;Simple. First, the &lt;code&gt;moc&lt;/code&gt; will just simply scan the number of signals the heir has, and it hands over to the internal class &lt;code&gt;Connection&lt;/code&gt;, which represents a signal connection.&lt;/p&gt;

&lt;p&gt;There are two important members in &lt;code&gt;Connection&lt;/code&gt;, namely &lt;code&gt;Connection::method_relative&lt;/code&gt; and &lt;code&gt;Connection::method_offset&lt;/code&gt;. After calling the &lt;code&gt;QObject::connect&lt;/code&gt; connection method, it hands over the signaling data to the &lt;code&gt;QMetaObject&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;You do not need to know how they work; they're to be hidden and remain internal. But you just need to know that they in &lt;code&gt;QMetaObject&lt;/code&gt;, it matches the relative index of a method (and assigns some index to a newly met method). When invoking the signals, another method &lt;code&gt;methodMatch&lt;/code&gt; is encountered and it matches the corresponding method, like the overloading part, overriding part and any other parts.&lt;/p&gt;

&lt;p&gt;The required data include &lt;code&gt;data&lt;/code&gt;, &lt;code&gt;static_metacall&lt;/code&gt; in &lt;code&gt;QMetaObject::d&lt;/code&gt;, which are all defined in &lt;code&gt;moc_flower.cpp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This leads us to today's final topic:&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;QMetaObject&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;I know you're scratching your head till your brain could consume all the information we have discussed. Fortunately, today's key points could be reviewed unlimited times as long as &lt;code&gt;dev.to&lt;/code&gt; is free. So, please allow me to introduce a hidden struct we've mentioned numerous times yet not discussed: &lt;code&gt;QMetaObject&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Actually, it's simpler than the &lt;em&gt;magics&lt;/em&gt; of &lt;code&gt;moc&lt;/code&gt;. It's &lt;em&gt;way simpler&lt;/em&gt;. To understand &lt;code&gt;QMetaObject&lt;/code&gt;, think of &lt;code&gt;staticMetaObject&lt;/code&gt;. It stores all the meta information (i.e. information related to the class per se, in plain English, like the class name, signals names and indices, slots names and indices, parent classes, properties, etc.), and it is these pieces of required information make signal-slot connection possible.&lt;/p&gt;

&lt;p&gt;Here's some key methods in &lt;code&gt;QMetaObject&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;className&lt;/code&gt; returns the name of a class,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;superClass&lt;/code&gt; returns the &lt;code&gt;QMetaObject&lt;/code&gt; of parent class of the host class.&lt;/li&gt;
&lt;li&gt;We have other name-explanatory methods like &lt;code&gt;method&lt;/code&gt;, &lt;code&gt;methodCount&lt;/code&gt;, &lt;code&gt;enumerator&lt;/code&gt;, &lt;code&gt;enumeratorCount&lt;/code&gt;, &lt;code&gt;constructor&lt;/code&gt;, &lt;code&gt;constructorCount&lt;/code&gt; etc. They all return some meta classes like &lt;code&gt;QMetaMethod&lt;/code&gt; or &lt;code&gt;QMetaEnum&lt;/code&gt;, etc., which have similar functions as a &lt;code&gt;QMetaObject&lt;/code&gt;. Note that accessing the methods, constructors, etc. requires the index, which is described above.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Python lovers, here's a close analogy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;inspect&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;# It's just literally the same
&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;AGEN_CLOSED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;AGEN_CREATED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;AGEN_RUNNING&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;AGEN_SUSPENDED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ArgInfo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Arguments&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Attribute&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;BlockFinder&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;BoundArguments&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;BufferFlags&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CORO_CLOSED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CORO_CREATED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CORO_RUNNING&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CORO_SUSPENDED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_ASYNC_GENERATOR&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_COROUTINE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_GENERATOR&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_ITERABLE_COROUTINE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_NESTED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_NEWLOCALS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_NOFREE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_OPTIMIZED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_VARARGS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CO_VARKEYWORDS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ClassFoundException&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ClosureVars&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;EndOfBlock&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;FrameInfo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;FullArgSpec&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;GEN_CLOSED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;GEN_CREATED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;GEN_RUNNING&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;GEN_SUSPENDED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;OrderedDict&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Parameter&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Signature&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;TPFLAGS_IS_ABSTRACT&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Traceback&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_ClassFinder&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_FrameInfo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_KEYWORD_ONLY&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_NonUserDefinedCallables&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_POSITIONAL_ONLY&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_POSITIONAL_OR_KEYWORD&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_ParameterKind&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_Traceback&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_VAR_KEYWORD&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_VAR_POSITIONAL&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__all__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__author__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__builtins__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__cached__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__doc__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__file__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__loader__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__name__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__package__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__spec__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_check_class&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_check_instance&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_descriptor_get&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_empty&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_filesbymodname&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_findclass&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_finddoc&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_get_code_position&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_get_code_position_from_tb&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_get_dunder_dict_of_class&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_getmembers&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_has_code_flag&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_has_coroutine_mark&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_is_coroutine_mark&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_main&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_missing_arguments&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_sentinel&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_shadowed_dict&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_shadowed_dict_from_weakref_mro_tuple&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_bound_method&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_from_builtin&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_from_callable&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_from_function&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_fromstr&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_get_partial&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_get_user_defined_method&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_is_builtin&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_is_functionlike&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_signature_strip_non_python_syntax&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_static_getmro&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_too_many&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;_void&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;abc&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ast&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;attrgetter&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;builtins&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;classify_class_attrs&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cleandoc&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;collections&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;currentframe&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;dis&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;enum&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;findsource&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;formatannotation&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;formatannotationrelativeto&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;formatargvalues&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;functools&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;get_annotations&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getabsfile&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getargs&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getargvalues&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getasyncgenlocals&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getasyncgenstate&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getattr_static&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getblock&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getcallargs&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getclasstree&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getclosurevars&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getcomments&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getcoroutinelocals&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getcoroutinestate&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getdoc&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getfile&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getframeinfo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getfullargspec&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getgeneratorlocals&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getgeneratorstate&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getinnerframes&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getlineno&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getmembers&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getmembers_static&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getmodule&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getmodulename&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getmro&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getouterframes&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getsource&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getsourcefile&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;getsourcelines&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;importlib&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;indentsize&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isabstract&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isasyncgen&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isasyncgenfunction&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isawaitable&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isbuiltin&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isclass&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;iscode&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;iscoroutine&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;iscoroutinefunction&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isdatadescriptor&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isframe&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isfunction&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isgenerator&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isgeneratorfunction&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isgetsetdescriptor&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;iskeyword&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ismemberdescriptor&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ismethod&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ismethoddescriptor&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ismethodwrapper&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ismodule&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;isroutine&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;istraceback&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;itertools&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;linecache&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;make_weakref&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;markcoroutinefunction&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;modulesbyfile&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;namedtuple&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;os&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;re&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;signature&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;stack&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sys&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;token&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;tokenize&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;trace&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;types&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;unwrap&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;walktree&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... Stayed tuned for the next article. We'll still be discussing meta-objects, signals-and-slots, ...&lt;/p&gt;

&lt;p&gt;(I know you're not quite anticipating compared to the previous article regarding how you expect the signal-slot system looks like, particularly after seeing these complex and perplexed mechanisms... But, my friend, please believe me that this is the &lt;em&gt;most&lt;/em&gt; important feature in Qt in my opinion, and I've devoted time to elucidating it.)&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>qt</category>
      <category>desktop</category>
    </item>
    <item>
      <title>[Qt] V. Widgets: QPushButton and Buttons</title>
      <dc:creator>Misinahaiya</dc:creator>
      <pubDate>Sun, 16 Feb 2025 07:05:25 +0000</pubDate>
      <link>https://dev.to/misinahaiya/qt-v-widgets-qpushbutton-and-buttons-4lbn</link>
      <guid>https://dev.to/misinahaiya/qt-v-widgets-qpushbutton-and-buttons-4lbn</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"&lt;em&gt;Push me, push me, push me hard&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;&lt;del&gt;Some random Japanese anime&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;Someone who strikes for excellence in a field that he loves&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So far, you've gotten something called labels (as in the form of &lt;code&gt;QLabel&lt;/code&gt;), and you eagerly want them to obey to your instruction and interact with the user of your tiny, little GUI:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl5t3bg05ywyvnrx6aj8d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl5t3bg05ywyvnrx6aj8d.png" alt="Labeled useless button" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You clicked the mouse (or tapped the track-pad, whatever) like a lunatic; "Quit! - The window, now," you demanded. After a fifteen-second combat, you gave up and said, "Qt's so useless! They can't even make the app gone. Let alone those fancy features. I shouldn't 've trusted a !(random guy on &lt;code&gt;dev.to&lt;/code&gt;)[&lt;a href="https://dev.to/misinahaiya/series/30375"&gt;https://dev.to/misinahaiya/series/30375&lt;/a&gt;) who'd adulated Qt. Be quick, unfollow him! Uninstall Qt now!" And you unfollowed me and uninstalled Qt.&lt;/p&gt;

&lt;p&gt;Okay, let's change the timeline. Back before you want to implement a feature with an action of "Quit", you've read my tutorial (specifically, this one). You know you did not learn how to build a &lt;em&gt;responsive&lt;/em&gt; widget to &lt;em&gt;response&lt;/em&gt; to the user's desires of quitting the application. You just haven't learned them. So, you would not have complained, or uninstalled Qt. (Hopefully you won't have unfollowed me too, by the way) You - sorry for the long prelude - now finally know that you're looking for a widget called a button.&lt;/p&gt;

&lt;h1&gt;
  
  
  Different Types of Buttons that You'd Better Know
&lt;/h1&gt;

&lt;p&gt;All types of buttons, except for &lt;code&gt;QCommandLinkButton&lt;/code&gt; which is an odd one to flatter Windows as well as Microsoft (will be explained later), inherited the subclass of &lt;code&gt;QWidget&lt;/code&gt;: the &lt;code&gt;QAbstractButton&lt;/code&gt; class. This class encapsulates the common functions of a button, such as displaying the text, drawing the button borders and the icons (if any), setting whether if they could be toggled (and much more, which will all be explained later).&lt;/p&gt;

&lt;p&gt;There are totally 4 classes inheriting the &lt;code&gt;QAbstractButton&lt;/code&gt; class, so there are totally 5 types of buttons in Qt. Note that the number of types here indicates the number of types of built-in button in Qt. That is, you could draw your own button and handle the event called &lt;em&gt;signals&lt;/em&gt;; I'll guide you when you've learned enough. For now, just stick to the five &lt;em&gt;basic&lt;/em&gt; buttons (well, you could understand as so), and it should be rather enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checkbox
&lt;/h2&gt;

&lt;p&gt;A checkbox, represented by the class &lt;code&gt;QCheckBox&lt;/code&gt;, is just a type of button with a box allowing you to "&lt;em&gt;check&lt;/em&gt;" the mark, which is beside the text of the button:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hr1vby9ibgkq76r8g0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hr1vby9ibgkq76r8g0i.png" alt="Custom QCheckBox" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Actual usage could be found everywhere, for example, in LibreOffice, literally anywhere in every single tab regarding the settings of characters, paragraphs and pages:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn3k5cdhchppyu3yjt64e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn3k5cdhchppyu3yjt64e.png" alt="LibreOffice's Checkbox" width="290" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Radio Button
&lt;/h2&gt;

&lt;p&gt;A radio button, represented by the class &lt;code&gt;QRadioButton&lt;/code&gt;, is a type of button that is often mutually exclusive under the same parent widget, i.e. only one radio button of a group of radio buttons could be checked at once.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8h6t6u0iz900gzb58zn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8h6t6u0iz900gzb58zn.png" alt="Radio Button" width="322" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Actual usage could be found also everywhere, particularly in Microsoft Paint:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnv4i2besa879mcwjzehl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnv4i2besa879mcwjzehl.png" alt="Paint's Radio Button" width="622" height="775"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Push Button
&lt;/h2&gt;

&lt;p&gt;This class, as represented by the class &lt;code&gt;QPushButton&lt;/code&gt;, is - undeniably - the most well-received class among the four; and it is also a part of today's title. The push button is simple, it is just literally a &lt;em&gt;button&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flswbdakgxstq4scfaqp1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flswbdakgxstq4scfaqp1.png" alt="QPushButton" width="294" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For actual examples... Well, it's way more prevalent than the radio buttons and check buttons, provided how common are the latter ones. Take the simplest one: &lt;a href="https://learn.microsoft.com/en-gb/office/vba/language/reference/user-interface-help/msgbox-function" rel="noopener noreferrer"&gt;a single-buttoned message box written in single-line VBScript&lt;/a&gt; (Windows only) has a push button (although it isn't that beautiful):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vb"&gt;&lt;code&gt;&lt;span class="n"&gt;MsgBox&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hi, there's a push button below"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;produces&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6gg9u0glf3lhz6nz94wn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6gg9u0glf3lhz6nz94wn.png" alt="Visual Basic Script push button" width="260" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tool Button
&lt;/h2&gt;

&lt;p&gt;We'll talk about it later. Basically, this one - named &lt;code&gt;QToolButton&lt;/code&gt; - is just like a flattened push button, almost always with an icon instead of some text, and usually with a "Down" arrow:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9x3mlrj1o7rtjkoiq7jw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9x3mlrj1o7rtjkoiq7jw.png" alt="QToolButton" width="150" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This class should be the least common class among the four heirs of the &lt;code&gt;QAbstractButton&lt;/code&gt; class, yet we'll still be diving into the &lt;code&gt;QToolButton&lt;/code&gt; class later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Command Link Button
&lt;/h2&gt;

&lt;p&gt;For this one though... As it does not inherit the &lt;code&gt;QAbstractButton&lt;/code&gt; class, it is really not that applicable and relevant to today's topic. Many methods will be unavailable.&lt;/p&gt;

&lt;p&gt;For introduction, this class is just a mimic of the installers for various applications under the Windows Vista or Windows 7 systems:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxhqlmpuf60irgolkw84.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxhqlmpuf60irgolkw84.png" alt="Vista" width="540" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks for &lt;a href="https://stackoverflow.com/questions/10469968/how-do-i-create-command-link-buttons-with-multiple-lines-of-text-in-vb-net" rel="noopener noreferrer"&gt;StackOverflow&lt;/a&gt; for the image.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since both Windows Vista and Windows 7 are dead now, we'll just skip &lt;a href="https://doc.qt.io/qt-6/qcommandlinkbutton.html" rel="noopener noreferrer"&gt;it&lt;/a&gt;. (For people interested though, do dive into this site.)&lt;/p&gt;

&lt;h1&gt;
  
  
  Customize the buttons
&lt;/h1&gt;

&lt;p&gt;Now let's discover some useful practices you might opt to use when manipulating the buttons, the views or even the whole user interface design. These class are universally (publicly) available among the heirs of the &lt;code&gt;QAbstractButton&lt;/code&gt; class. This series makes the &lt;code&gt;QPushButton&lt;/code&gt; as the example as it's too common. Note that there will be significant changes in appearance and internal differences of methods of the same name (e.g. &lt;code&gt;setChecked&lt;/code&gt;) between the classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automation
&lt;/h2&gt;

&lt;p&gt;Property &lt;code&gt;autoExclusive&lt;/code&gt; is one of the features of the radio buttons, as described above. When a group of buttons (that means they have the same parent widget) is checked, then the others must be de-checked. Normally, we don't need this except for a radio button, whose property of &lt;code&gt;autoExclusive&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; by default already.&lt;/p&gt;

&lt;p&gt;Property &lt;code&gt;autoRepeat&lt;/code&gt; is interesting: normally, if you press a button, the button will recognize the click once only. However, with &lt;code&gt;autoRepeat&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt;, the button recognizes the click signal repeatedly after you've clicked (provided you didn't let go) for &lt;code&gt;autoRepeatDelay&lt;/code&gt; millisecond, and the interval is &lt;code&gt;autoRepeatInterval&lt;/code&gt; millisecond.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checkability
&lt;/h2&gt;

&lt;p&gt;If we call something "checked", then it is marked by an indicator that is darker (brighter if you're in dark mode) to show that that thing is active, in progress, or any other synonyms. For example, if I'm using ruler in LibreOffice Writer, there will be a brighter box around the action &lt;code&gt;View -&amp;gt; Rulers -&amp;gt; Ruler&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9jx41wr9uv53h17wobz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9jx41wr9uv53h17wobz.png" alt="Checked Action" width="199" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, if I opt not to show the formatting marks (i.e. invisible characters, will be covered later in the chapter of &lt;code&gt;QTextOption&lt;/code&gt; in relation to &lt;code&gt;QTextEdit&lt;/code&gt;), there will be no check marks on the action "Formatting Marks (Ctrl+F10)":&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frna97yq0jrxp1yzaz7cc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frna97yq0jrxp1yzaz7cc.png" alt="Unchecked Action" width="269" height="682"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To make the button checkable, use the property &lt;code&gt;checkable&lt;/code&gt;. To see if the button is checked or not or to programmatically set the check state, use the property &lt;code&gt;checked&lt;/code&gt;. Normally, radio buttons and check boxes are checkable by default (of course!).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3e2750ube26ho9obtuop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3e2750ube26ho9obtuop.png" alt="Checked versus unchecked" width="252" height="192"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While we say a radio button or a check box is checked if the indicator (i.e. check mark for check box, circle for radio button) is present instead of the shadings.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Never call &lt;code&gt;setChecked(true)&lt;/code&gt; if &lt;code&gt;checkable() == true&lt;/code&gt;. Result or behavior is untested or even undefined.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Text
&lt;/h2&gt;

&lt;p&gt;To display some text or retrieve so, use the property &lt;code&gt;text&lt;/code&gt;. Simple and intuitive, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Simulation
&lt;/h2&gt;

&lt;p&gt;To simulate the user's actions, there's some method to do so programmatically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;QAbstractButton::click&lt;/code&gt;: when called, the button will instantly recognize itself being clicked. Has no effects when it is disabled.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;QAbstractButton::animateClick&lt;/code&gt;: similar to &lt;code&gt;QAbstractButton::click&lt;/code&gt;, but more fancy. It will do some animations to &lt;em&gt;show&lt;/em&gt; that the button is clicked. I prefer &lt;code&gt;QAbstractButton::click&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;QAbstractButton::toggle&lt;/code&gt;: just change the check state of the button, but it will not make the button recognize that it has been &lt;em&gt;clicked&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;

&lt;p&gt;A small example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ... Main Windows&lt;/span&gt;
&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QWidget&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;QMainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QPushButton&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;btn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;QPushButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;btn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setAutoExclusive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;btn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setAutoRepeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;btn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setAutoRepeatDelay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;btn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setAutoRepeatInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;btn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Check me!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;setCentralWidget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Make the handler of the button,&lt;/span&gt;
    &lt;span class="c1"&gt;// that the function will be called when the button&lt;/span&gt;
    &lt;span class="c1"&gt;// recognizes that it is checked.&lt;/span&gt;
    &lt;span class="c1"&gt;//&lt;/span&gt;
    &lt;span class="c1"&gt;// You'll be introduced in the next chapter.&lt;/span&gt;
    &lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;QPushButton&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;clicked&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;functionToBeCalled&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;functionToBeCalled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isChecked&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;qDebug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Clicked!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// For your level, just treat this as std::cout.&lt;/span&gt;
    &lt;span class="c1"&gt;// Remember to #include &amp;lt;QDebug&amp;gt;.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4z6s7esfu89ttolkpuz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4z6s7esfu89ttolkpuz.gif" alt="Clicked" width="376" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have fun with your buttons along with your labels! Next chapter or two (or presumably, more), I'll talk about the most important and well-received Qt feature - signals and slots. Then, we'll be stepping into the realm of layout management (i.e., in plain English, how to cram the labels and buttons and all sort of widgets into a single widget). Stayed tuned!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>qt</category>
      <category>desktop</category>
    </item>
    <item>
      <title>[Qt] IV. Widgets: QLabel</title>
      <dc:creator>Misinahaiya</dc:creator>
      <pubDate>Wed, 05 Feb 2025 12:50:39 +0000</pubDate>
      <link>https://dev.to/misinahaiya/qt-iv-widgets-qlabel-19o4</link>
      <guid>https://dev.to/misinahaiya/qt-iv-widgets-qlabel-19o4</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"&lt;em&gt;Everything is based on information. Information is the God indeed.&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;Anonymous&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You have successfully built your first-ever program using Qt. That's amazing, isn't it?&lt;/p&gt;

&lt;p&gt;But after you'd come out of excitement, you soon found out that the user interface is &lt;em&gt;way too&lt;/em&gt; ugly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqthf8v0fdab9w26t5kti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqthf8v0fdab9w26t5kti.png" alt="Ugly QLabel" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's not very good. And that's not very Qt. Qt is not a tool for building ugly applications irrespective of whether the app is usable. In simple English, that's not our goal.&lt;/p&gt;

&lt;p&gt;How to make it better? Hopefully, it looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnf16ibv2bm4wd5vpbx56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnf16ibv2bm4wd5vpbx56.png" alt="Better QLabel" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The label isn't adhered to the margins anymore; instead, it's at the center &lt;em&gt;as a *center&lt;/em&gt; widget*. The font weight is bold and &lt;em&gt;mono-spaced&lt;/em&gt; to emphasize.&lt;/p&gt;

&lt;p&gt;Today, I'll be showing how to &lt;em&gt;beautify&lt;/em&gt; the labels.&lt;/p&gt;

&lt;h1&gt;
  
  
  Widget Basics
&lt;/h1&gt;

&lt;p&gt;This article might be longer than other widget-introducing articles, as I assume you do not know what on earth does the jargon "widget" actually mean, and instead you're just sitting in front of your screen, puzzled, reading my repeatedly use of "widgets," "&lt;code&gt;QWidget&lt;/code&gt;, "subclass," etc. in the previous article.&lt;/p&gt;

&lt;p&gt;"Subclass" should not be a term that is hard to explain, and I assume you've already known it (sorry for not saying the prerequisite of knowing C++ in the previous articles). Any class &lt;a href="https://en.cppreference.com/w/cpp/language/derived_class" rel="noopener noreferrer"&gt;inherited&lt;/a&gt; a &lt;em&gt;base&lt;/em&gt; class is the &lt;em&gt;subclass&lt;/em&gt; of the &lt;em&gt;base&lt;/em&gt; class.&lt;/p&gt;

&lt;p&gt;A "widget," in terms of Qt, is the primary element (the building blocks) for creating GUI. It's the atom of a user interface. Widgets could display data (output), receive user input (input) or as containers of other widgets (process). If you understand how computers work, here are some analogies in the parentheses.&lt;/p&gt;

&lt;p&gt;A widget in Qt could also be considered as any object having the base type of &lt;code&gt;QWidget&lt;/code&gt; verbatim. The following are all widgets (including the box):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftb1el6yvxnaqopy22gz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftb1el6yvxnaqopy22gz3.png" alt="Mini-gallery" width="453" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As mentioned in the previous article, &lt;code&gt;QWidget&lt;/code&gt; that has a parent is called a "widget", whereas parent symbolizes ownership of a widget, as with &lt;code&gt;QObject&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;More introductions of various Qt widgets (up two a hundred ones) are on their ways to this series. Stay tuned!&lt;/p&gt;

&lt;h1&gt;
  
  
  So How Could I Beautiful My Labels!!?
&lt;/h1&gt;

&lt;p&gt;Why are you always so in haste? Don't worry here it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting text
&lt;/h2&gt;

&lt;p&gt;Before we move on, let's refresh your memory of setting the text of a &lt;code&gt;QLabel&lt;/code&gt;. You could do so by using the method &lt;code&gt;QLabel::setText&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Spaces
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Alignment
&lt;/h3&gt;

&lt;p&gt;First, we want it to be aligned. Use the method &lt;code&gt;QLabel::setAlignment&lt;/code&gt; to do so. To acquire the current alignment, use &lt;code&gt;QLabel::alignment&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Qt is so powerful that it supports the alignment such as:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Qt::AlignmentFlag::AlignLeft&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f1kns9plvqg9hgpn4kp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f1kns9plvqg9hgpn4kp.png" alt="Align Left" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Qt::AlignmentFlag::AlignRight&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2wpra3ucr0rsbj5k1yz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2wpra3ucr0rsbj5k1yz.png" alt="Align Right" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Qt::AlignmentFlag::AlignHCenter&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbjbjhgap4auegu5yc1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbjbjhgap4auegu5yc1j.png" alt="Align Horizontal Center" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Qt::AlignmentFlag::AlignJustify&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw74jhtg9kxyii08pdwp7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw74jhtg9kxyii08pdwp7.png" alt="Align Justify" width="302" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You could also adjust the vertical alignment by using the &lt;code&gt;|&lt;/code&gt; operator. For example,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Qt::AlignmentFlag::AlignRight | Qt::AlignmentFlag::AlignTop&lt;/code&gt; aligns at the top-right corner:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkg0ysqsqgz5vn8n9equw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkg0ysqsqgz5vn8n9equw.png" alt="Align Top and Right" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, &lt;code&gt;Qt::AlignmentFlag::AlignLeft | Qt::AlignmentFlag::AlignBottom&lt;/code&gt; aligns at the bottom-left corner:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fma2hqd9von7x23m54es0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fma2hqd9von7x23m54es0.png" alt="Align Bottom and Left" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whereas &lt;code&gt;Qt::AlignmentFlag::AlignVCenter&lt;/code&gt; aligns as the name suggests:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzankj6xn9s9sqy4fdc57.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzankj6xn9s9sqy4fdc57.png" alt="V-Center" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's a special enumerator: &lt;code&gt;Qt::AlignmentFlag::AlignBaseline&lt;/code&gt;. It ensures that the alignment is in accordance with the text baseline in many labels lay outed horizontally instead of the text:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpjop9lnd3ocfq194dw03.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpjop9lnd3ocfq194dw03.png" alt="Align baseline" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that you could use &lt;code&gt;Qt::AlignmentFlag::AlignCenter&lt;/code&gt; as &lt;code&gt;Qt::AlignmentFlag::AlignVCenter | Qt::AlignmentFlag::AlignHCenter&lt;/code&gt;. They carry the same meaning.&lt;/p&gt;

&lt;p&gt;What if I don't want to do alignments?&lt;/p&gt;

&lt;h3&gt;
  
  
  Indentation
&lt;/h3&gt;

&lt;p&gt;Then, you have indentation. Use &lt;code&gt;QLabel::setIndent&lt;/code&gt; to set, and &lt;code&gt;QLabel::indent&lt;/code&gt; to acquire the current value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: as you have observed, there are usually two methods: the acquisition method of a specific property of its own name, and the setting method of it using the format of &lt;code&gt;set&lt;/code&gt; plus the capitalized name of the property. For example, for &lt;code&gt;alignment&lt;/code&gt;, we have &lt;code&gt;setAlignment&lt;/code&gt; and &lt;code&gt;alignment&lt;/code&gt;. For &lt;code&gt;indent&lt;/code&gt;, there are &lt;code&gt;setIndent&lt;/code&gt; and &lt;code&gt;indent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When there's exception, I will tell you. From now on, I call them "properties" and you know the getters and setters, as every character counts in this series.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If &lt;code&gt;alignment&lt;/code&gt; is &lt;code&gt;Qt::AlignmentFlag::AlignLeft&lt;/code&gt; (the default), then the indentation will be on the left edge, and if &lt;code&gt;alignment&lt;/code&gt; is &lt;code&gt;Qt::AlignmentFlag::AlignTop&lt;/code&gt;, the indentation will be on the top edge and so on and so forth.&lt;/p&gt;

&lt;p&gt;Setting alignment for centers do not have any effect.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivwowb9gzac1evxmvher.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivwowb9gzac1evxmvher.png" alt="Indentation" width="308" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If both &lt;code&gt;Qt::AlignmentFlag::AlignTop&lt;/code&gt; and &lt;code&gt;Qt::AlignmentFlag::AlignLeft&lt;/code&gt; or etc. are set, then both vertical and horizontal alignments will be indented:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43kf509thb4c26ycdgfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43kf509thb4c26ycdgfm.png" alt="Indentation advanced" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Margins
&lt;/h3&gt;

&lt;p&gt;You could also set the margins using the property &lt;code&gt;margin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Margins add &lt;em&gt;more&lt;/em&gt; space &lt;em&gt;around&lt;/em&gt; the label, irrespective of whether the label is indented or aligned or not. The default margin is 0 pixel (it is also counted in pixels).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Precisely, the margin is the distance between the innermost pixel of the label('s border) and the outermost pixel of contents (of the label).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8e7tmi4eteo07v76qg3r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8e7tmi4eteo07v76qg3r.png" alt="Margins" width="316" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap
&lt;/h3&gt;

&lt;p&gt;Wait... So, you've just tried out the "justify" example with indentations and margins and all sort of fancy things, but you got this whole long thorn instead of a smaller, (cuter?), fitter version:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fangrijc3yp386xv0wda8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fangrijc3yp386xv0wda8.png" alt="Long thorn" width="750" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You're then looking for the property &lt;code&gt;wordWrap&lt;/code&gt;. By using &lt;code&gt;QLabel::setWordWrap(true)&lt;/code&gt;, you could make the text be wrapped and it should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh5lvxq6rkyqylrt1qii6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh5lvxq6rkyqylrt1qii6.png" alt="Word-wrapped QLabel" width="302" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Normally, it wrapped according to the size of the window, or the parent widget.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using HTML
&lt;/h2&gt;

&lt;p&gt;QLabel &lt;a href="https://doc.qt.io/qt-6/richtext-html-subset.html" rel="noopener noreferrer"&gt;supports a limited subset of HTML&lt;/a&gt;, as well as other editor-like widgets we will cover later. You could just pass in valid and simple HTML code in order to do rich-text formatting.&lt;/p&gt;

&lt;p&gt;You could beautiful your text in terms of that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// I assume you've created your project&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QLabel&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;QLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;setCentralWidget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setAlignment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AlignmentFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AlignCenter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setMargin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// label-&amp;gt;setIndent(10);&lt;/span&gt;
    &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;p style='font-size: 25px;'&amp;gt;&amp;lt;b&amp;gt;Beautified&amp;lt;/b&amp;gt;&amp;lt;/p&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setWordWrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could disable it by using the property &lt;code&gt;textFormat&lt;/code&gt;. There are four &lt;code&gt;Qt::TextFormat&lt;/code&gt; enumerators defined:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Qt::TextFormat::PlainText&lt;/code&gt; makes the label displaying plain text:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3t4ajgipy2cl4qj0wd6d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3t4ajgipy2cl4qj0wd6d.png" alt="Plain text" width="255" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Qt::TextFormat::RichText&lt;/code&gt; makes the valid HTML code get parsed and interpreted, as shown as the above example. Also, if you pass in the argument &lt;code&gt;Qt::TextFormat::AutoText&lt;/code&gt;, it determines whether if the text is rich text or not by matching the HTML tags. (See function &lt;code&gt;mightBeRichTextImpl&lt;/code&gt; in Qt sources). If it is rich text, then &lt;code&gt;Qt::TextFormat::AutoText&lt;/code&gt; becomes &lt;code&gt;Qt::TextFormat::RichText&lt;/code&gt;. Otherwise, it becomes &lt;code&gt;Qt::TextFormat::PlainText&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You could also add markdown text via &lt;code&gt;Qt::TextFormat::Markdown&lt;/code&gt; from version 5.14 on (the latest one is 6.8).&lt;/p&gt;

&lt;h1&gt;
  
  
  Editable Text
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Those are not funny and interesting enough!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Allow me to introduce the final boss: a &lt;code&gt;QLabel&lt;/code&gt; could be a simple text editor.&lt;/p&gt;

&lt;p&gt;Using the property &lt;code&gt;textInteractionFlags&lt;/code&gt;, you could set that:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Qt::TextInteractionFlag::NoTextInteraction&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;'https://google.com'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Google&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6fdauv1mh6ytdnpuequa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6fdauv1mh6ytdnpuequa.png" alt="Google" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The link seems to be not able to process the hover (mouse-on) events and seems to be disabled, and you could not access so.&lt;/p&gt;

&lt;p&gt;To make it accessible, use &lt;code&gt;Qt::TextInteractionFlag::LinksAccessibleByMouse&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn0z7pfyx1cxzsv68u4ua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn0z7pfyx1cxzsv68u4ua.png" alt="Google accessible by mouse" width="252" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Though it still looks like disabled, you could hover it and see the hyper-link cursor!&lt;/p&gt;

&lt;p&gt;To make the keyboard could also access the link via the &lt;code&gt;tab&lt;/code&gt; key (called &lt;em&gt;focus&lt;/em&gt;, which will be introduced to you later), take the &lt;code&gt;Qt::TextInteractionFlag::LinksAccessibleByKeyboard&lt;/code&gt; in bitwise-or operation (&lt;code&gt;|&lt;/code&gt;) with &lt;code&gt;LinkAccessibleByMouse&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setTextInteractionFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;LinksAccessibleByKeyboard&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;LinksAccessibleByMouse&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75nbv8mmi93446waz724.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75nbv8mmi93446waz724.png" alt="Selectable" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you wish your link is readily available and open in the default browser immediately after activating it (pressing it or clicking &lt;code&gt;Enter&lt;/code&gt;, use the property &lt;code&gt;openExternalLinks&lt;/code&gt; and set it to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's the same for selection: &lt;code&gt;TextSelectableByMouse&lt;/code&gt; by dragging and &lt;code&gt;TextSelectableByKeyboard&lt;/code&gt; by using the &lt;code&gt;Shift&lt;/code&gt; key(s) and the arrow keys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;'font-weight: 700;'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Lorem ipsum dolor sit amet.&lt;span class="nt"&gt;&amp;lt;br&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;'https://google.com'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Google&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setTextInteractionFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextSelectableByMouse&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextSelectableByKeyboard&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;LinksAccessibleByKeyboard&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;LinksAccessibleByMouse&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fybt1dueee1ra5lgljte4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fybt1dueee1ra5lgljte4.png" alt="Text Selectable" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To investigate whether there is text selected, use the method &lt;code&gt;hasSelectedText&lt;/code&gt;. To see the selected text, use the method &lt;code&gt;selectedText&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, the final boss of the final bosses: &lt;code&gt;Qt::TextInteractionFlag::TextEditable&lt;/code&gt;. By using this on &lt;code&gt;QLabel&lt;/code&gt;, your label could be a mini-text editor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setTextInteractionFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextEditable&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextSelectableByMouse&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextSelectableByKeyboard&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;LinksAccessibleByKeyboard&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;Qt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TextInteractionFlag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;LinksAccessibleByMouse&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fttd5e6890yyxk7p0supc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fttd5e6890yyxk7p0supc.png" alt="Editable" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, you know how to beautify your tiny text label!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>qt</category>
      <category>desktop</category>
    </item>
    <item>
      <title>[Qt] III. Hello, World!</title>
      <dc:creator>Misinahaiya</dc:creator>
      <pubDate>Tue, 04 Feb 2025 12:25:14 +0000</pubDate>
      <link>https://dev.to/misinahaiya/qt-iii-hello-world-34o2</link>
      <guid>https://dev.to/misinahaiya/qt-iii-hello-world-34o2</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"&lt;em&gt;Hello, World!&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;First B Language Program, &lt;em&gt;The B Language Tutorial&lt;/em&gt;, Bell Labs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So you have been forced to sit in front of your computer, looking at the screen for one hour or two, to install the behemoth Qt. Then, you really wanted to try out the best IDE in the universe - Qt Creator, but you were intimidated by the (slightly?) overly posh user interface:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsy7zlql62nxe1fvb6d08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsy7zlql62nxe1fvb6d08.png" alt="Welcome Screen of Qt Creator" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You did not know what to do.&lt;/p&gt;

&lt;p&gt;That's normal, as it is the first time that you got to know Qt. Let me introduce how to create your first Qt application.&lt;/p&gt;

&lt;p&gt;"Hello, World!" is probably the most iconic and the most well-known programming language example for absolute beginners. So, for Qt, even it is just a comprehensive tool, I will still be introducing you "Hello, World!" as the first example ever for our series.&lt;/p&gt;

&lt;h1&gt;
  
  
  Projects (Oh My!)
&lt;/h1&gt;

&lt;p&gt;Every app, created or edited on Qt Creator, is called a "project". Don't be misguided by the name, a project need not to be very complex or large.&lt;/p&gt;

&lt;p&gt;To create a project, you should select the "File" menu on the top-left corner. Select "Project" to create a new project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvdp6g44fjlhr1rry5xqq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvdp6g44fjlhr1rry5xqq.png" alt="Create Project Action" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, a dialog called "wizard" will appear. It guides you to create your first project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8lyw4p42cfxtdwghkqeq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8lyw4p42cfxtdwghkqeq.png" alt="Create Project Wizard's Select Project Type" width="800" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, just choose "Application (Qt)", which implies using the C++ programming language. We are not going to use Python or develop web applications in Qt throughout this series, are we? So, leave those fields intact. Click "Choose".&lt;/p&gt;

&lt;p&gt;After that, you would see the dialog below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs0kdt36q6rao49vk58lj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs0kdt36q6rao49vk58lj.png" alt="Create a new project" width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Name your project a reasonable and clear name. I'll use "Hello World" for this article. Do note that special characters (including spaces ' ' yet not hyphens '-' nor underlines '_') are illegal characters in project names, i.e. you cannot use them in naming your project.&lt;/p&gt;

&lt;p&gt;Normally, you do not need to create any folder for your project, as Qt Creator will create it for you. As for me, I prefer put my projects into a directory called "Projects" under the location of the Documents folder. But trust me, changing this will not affect anything. It's just a matter of preference.&lt;/p&gt;

&lt;p&gt;If you decide to use this directory continuously and throughout this series (and presumably also continuously for your own app development), remember to check the check box "Use as default project location." It will make Qt Creator recognize your preference.&lt;/p&gt;

&lt;p&gt;Click "Next," if there are no problems. Then you'll see a simple yet standard combo-box:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkm8lxm2b3lj9uwiteh59.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkm8lxm2b3lj9uwiteh59.png" alt="Define Build System" width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm generally against you using qbs, as it's &lt;a href="https://www.qt.io/blog/2018/10/29/deprecation-of-qbs" rel="noopener noreferrer"&gt;outdated and not maintained for more than 5 years&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You could choose QMake, the own Qt build system for C++. But in this tutorial, I'm going to use CMake, as it is supported by the Qt 6 (the latest version) more than that of Qt 5. In other words, QMake will still be, inevitably, deprecated in the future.&lt;/p&gt;

&lt;p&gt;If you really love QMake, there are &lt;a href="https://doc.qt.io/qt-6/qmake-manual.html" rel="noopener noreferrer"&gt;a lot of useful and official guides online&lt;/a&gt; that you could make a reference of. If you do pure Qt, there are no difference between QMake and CMake except the syntax. They both generate Makefiles. But due to the official support and the latter one is more compatible to external tools (e.g. OpenCV uses CMake), I do CMake.&lt;/p&gt;

&lt;p&gt;Anyway, let's do away with the page and flick the dialog to the new page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfsz237lk4xxtxlbce42.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfsz237lk4xxtxlbce42.png" alt="Create MainWindow file" width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, de-check the "Generate From" check box.&lt;/p&gt;

&lt;p&gt;What does that action do? Well, it launches the so-called convenient tool of "Qt Designer", which is already bundled in and bound with Qt Creator. You could just actually craft a good-looking, fancy user interface without any coding involved.&lt;/p&gt;

&lt;p&gt;Sounds nice, right? We'll abstain from using this throughout the whole series. Why?&lt;/p&gt;

&lt;p&gt;Think about it: athletes are trained with heavy and weary things called "burden" on their backs, and when they do really be in a race, they'll be like running above the sky. Same thing for your Qt odyssey: if you do want to have absolutely no dark corners on &lt;em&gt;how&lt;/em&gt; you could craft the user interface and on &lt;em&gt;how&lt;/em&gt; to be an architect at user interface, don't use it.&lt;/p&gt;

&lt;p&gt;Instead, with my detailed explanations, you'll be very good at building everything from scratch under the Qt framework. After that, you could then try out the Qt Designer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F86g17gu6vy2q4syv782w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F86g17gu6vy2q4syv782w.png" alt="Parlez-vous Francais" width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Je suis en train de essayer le Qt Creator!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok, we'll not be doing French or other things here, at least for now. Internationalization and translations will be introduced and be there later. Skip them now to be simple. (Well, Qt has a astonishingly meticulous internationalization system. I &lt;em&gt;can't&lt;/em&gt; omit the elephant in the room, but to explain it thoroughly you must have a solid foundation on the topics we'll be covering later.)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpow8141wv41r5qlhq32q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpow8141wv41r5qlhq32q.png" alt="Select kits" width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, kits! Well, for kits, my advice is, select the default one. If you just installed one version of Qt (e.g. Qt 6.8.2), on Windows, "MinGW" will then be shown and be the default. On macOS, it should be "clang" or "LLVM". On GNU/Linux and (mostly) the others, it should be "GCC".&lt;/p&gt;

&lt;p&gt;If not, I suggest you de-checking the default ones and checking these. The tutorial will be focusing on these compilers respectively; unless you have a solid foundation on &lt;em&gt;those&lt;/em&gt; kits, or any other reason that you have to use those kits.&lt;/p&gt;

&lt;p&gt;(Note that on Windows, when we reach the point of creating web browsers, "MSVC" must be used instead of "MinGW". I'll explain it why later. Now, just choose "MinGW". In most of the time, it will do)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgnnmx8h67cd2t4oosij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgnnmx8h67cd2t4oosij.png" alt="No-Git day" width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well... Here, we aren't going to do Git. If you wish and you have &lt;code&gt;git&lt;/code&gt;, you could craft your own repository as well. But that's not for me, due to simplicity.&lt;/p&gt;

&lt;p&gt;Click "Finish". Yippee, we're done!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuov57p5qtmgnkoz857hr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuov57p5qtmgnkoz857hr.png" alt="Hello, World!" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that you could always change almost all the above configurations in the "Project" column on the leftmost bar of the Qt Creator.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Building the Real "Hello, World!"
&lt;/h1&gt;

&lt;p&gt;Let's look at the three source files created:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;main.cpp&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"mainwindow.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QApplication&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QApplication&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;MainWindow&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;mainwindow.h&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef MAINWINDOW_H
#define MAINWINDOW_H
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QMainWindow&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainWindow&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;QMainWindow&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Q_OBJECT&lt;/span&gt;

&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QWidget&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="cp"&gt;#endif // MAINWINDOW_H
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;mainwindow.cpp&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"mainwindow.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QWidget&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;QMainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::~&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;code&gt;MainWindow&lt;/code&gt; is a subclass of &lt;code&gt;QMainWindow&lt;/code&gt; (as suggested by its name), and the latter one is a subclass of &lt;code&gt;QWidget&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the files
&lt;/h2&gt;

&lt;p&gt;In the main file, the &lt;code&gt;QApplication&lt;/code&gt; object is imported and used &lt;em&gt;before&lt;/em&gt; the creation of the &lt;code&gt;MainWindow&lt;/code&gt; type. That's correct, as &lt;strong&gt;there must always be an, and only one, &lt;code&gt;QApplication&lt;/code&gt; instance before the creation of any paint-devices&lt;/strong&gt;. In simple words, &lt;strong&gt;any &lt;code&gt;QWidget&lt;/code&gt; must be created after the creation of a &lt;code&gt;QApplication&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is because that &lt;code&gt;QApplication&lt;/code&gt; is considered as a &lt;em&gt;coordinator&lt;/em&gt; of the different widgets and objects: how should they interact with the user and the system? How should they be arranged? How should handle their lives (e.g. construction and destruction)? A &lt;code&gt;QApplication&lt;/code&gt; object is always responsible for these.&lt;/p&gt;

&lt;p&gt;By just looking at its methods: &lt;code&gt;setDoubleClickInterval&lt;/code&gt;, &lt;code&gt;setCursorFlashTime&lt;/code&gt;, &lt;code&gt;setStartDragDistance&lt;/code&gt;, &lt;code&gt;setStartDragTime&lt;/code&gt; or &lt;code&gt;topLevelWidgets&lt;/code&gt; could prove so.&lt;/p&gt;

&lt;p&gt;Note that the application object needs to have the arguments as the parameters.&lt;/p&gt;

&lt;p&gt;After that, at the &lt;code&gt;MainWindow&lt;/code&gt; constructor, you could see a parameter called &lt;code&gt;parent&lt;/code&gt;. By practice, if a &lt;code&gt;QWidget&lt;/code&gt; does not have a parent, it is the top-level widget (i.e., a window).&lt;/p&gt;

&lt;p&gt;If it does have, then we say the &lt;em&gt;ownership&lt;/em&gt; of the widget is transferred to its parent widget. It means that the widget becomes the &lt;em&gt;child&lt;/em&gt; of its &lt;em&gt;parent&lt;/em&gt;, and when the parent got destructed, all of its children also got destructed, even if you had not implemented the destructor method to demand explicitly the program and Qt to destruct the children, for it is applicable for every &lt;code&gt;QObject&lt;/code&gt; and its subclasses.&lt;/p&gt;

&lt;p&gt;Calling the &lt;code&gt;show&lt;/code&gt; method shows the main window (also for any other &lt;code&gt;QWidget&lt;/code&gt;). If it is already embedded in another &lt;code&gt;QWidget&lt;/code&gt; (and generally it becomes a child), calling this has no use.&lt;/p&gt;

&lt;p&gt;Finally, the app should return the value of &lt;code&gt;exec&lt;/code&gt;. The method &lt;code&gt;exec&lt;/code&gt; indicates it's time for the application to receive user input, so-called "entering the &lt;em&gt;main-loop&lt;/em&gt;". It generally returns 0, indicating that no error occurred.&lt;/p&gt;

&lt;p&gt;Now, key the shortcut &lt;code&gt;Ctrl&lt;/code&gt; and &lt;code&gt;R&lt;/code&gt;, or click the menu as follows: &lt;code&gt;Build -&amp;gt; Run&lt;/code&gt;. You could see an empty window if your compiler is okay:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3okch70n1n8bv1c813wq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3okch70n1n8bv1c813wq.png" alt="Empty Hello World" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where's my Hello World?
&lt;/h2&gt;

&lt;p&gt;Don't be in haste. If you saw an empty window, that indicates you have a good compiler and you're going to do so.&lt;/p&gt;

&lt;p&gt;Change the &lt;code&gt;mainwindow.cpp&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"mainwindow.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;QLabel&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QWidget&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;QMainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;QLabel&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;QLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;setCentralWidget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="o"&gt;::~&lt;/span&gt;&lt;span class="n"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Tips:&lt;/em&gt; Every &lt;code&gt;QObject&lt;/code&gt; needs to be in pointer form if they need to interact with other objects. In the above example, &lt;code&gt;label&lt;/code&gt; is set as the central widget by another &lt;code&gt;QObject&lt;/code&gt;, &lt;code&gt;MainWindow&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We included the &lt;code&gt;QLabel&lt;/code&gt; widget. It's another subclass of &lt;code&gt;QWidget&lt;/code&gt; that used to display read-only text. If you have any experience in designing GUI, you might have come across similar widgets. For example, in TCL/Tk, you may have &lt;code&gt;Label&lt;/code&gt;. In Wx Frameworks, you have &lt;code&gt;wxStaticText&lt;/code&gt;. They all mean the approximately same thing.&lt;/p&gt;

&lt;p&gt;To set the text, you could just pass the text as strings to the method &lt;code&gt;setText&lt;/code&gt;. After that, to make it the central widget (i.e., widget that occupies most of the space of the main window), use the method defined in &lt;code&gt;QMainWindow&lt;/code&gt;: &lt;code&gt;setCentralWidget&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, build again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fko2pmrac329fpoupzmtt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fko2pmrac329fpoupzmtt.png" alt="First successful example" width="252" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Succeed!&lt;/p&gt;

&lt;p&gt;Hello, World!&lt;br&gt;
Hello, Qt!&lt;br&gt;
Hello, Everyone!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>qt</category>
      <category>desktop</category>
    </item>
    <item>
      <title>[Qt] II. How To Install Qt</title>
      <dc:creator>Misinahaiya</dc:creator>
      <pubDate>Wed, 29 Jan 2025 14:16:46 +0000</pubDate>
      <link>https://dev.to/misinahaiya/qt-ii-how-to-install-qt-1948</link>
      <guid>https://dev.to/misinahaiya/qt-ii-how-to-install-qt-1948</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"&lt;em&gt;Well begun is half done.&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;Aristotle&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An adviser or an instructor? Or for naysayers, an agitator? Ha ha, that's what you would call me for I have (hopefully and probably) persuaded you to learn Qt with me in this series.&lt;/p&gt;

&lt;p&gt;Without being a Victorian writer with much abstruse metaphors and without further ado, let us (really) start this series now.&lt;/p&gt;

&lt;h1&gt;
  
  
  What will we be doing today?
&lt;/h1&gt;

&lt;p&gt;Let me prompt you a question first: what would you do before &lt;em&gt;using&lt;/em&gt; a piece of software, given that "&lt;em&gt;software&lt;/em&gt;" could also be a tool or a library used in different programming languages.&lt;/p&gt;

&lt;p&gt;Don't answer me that you're going to download the package from the Internet or going to learn how to use the piece of software. Those are not quite accurate. Now, as a rule, before we use a piece of software (except the ones which are out-of-the-box zip files or install-on-disks programs), we must install it using an installer, as you would do for 90% of (especially Windows) programs.&lt;/p&gt;

&lt;p&gt;Qt can't be an exception. It must find its way to install itself.&lt;/p&gt;

&lt;h1&gt;
  
  
  It All Started at that Snowy Night, on that Website...
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Qt-dot-I-O
&lt;/h2&gt;

&lt;p&gt;Open your little browser and type in this address: &lt;a href="https://www.qt.io/" rel="noopener noreferrer"&gt;qt.io&lt;/a&gt;. This should be launching the official website of Qt. Any docs or examples that you would like to take a look later may also be found here. Here is the screenshot from about February, 2025. Your version may vary:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmc6hnph1vdy1vq7esysm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmc6hnph1vdy1vq7esysm.png" alt="Qt Homepage" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the button "Accept All Cookies," as what you'll do when visiting every website. Normally, accepting all cookies does no harms to your computer as it only stores your footprint or preferences for a short period of time but not any personal data, so that the web server could remember your choice and be improved. If you really mind these configurations, do configure them in "Cookie Settings."&lt;/p&gt;

&lt;p&gt;Anyway, after this, click on the "Download. Try." button, or the button somewhat similar. You'll see (again, your version may vary):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqul27o6o36pegwhnc1y7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqul27o6o36pegwhnc1y7.png" alt="Qt Download or Try Page" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the "Community User" link. As mentioned in the previous article, Qt is dual-licensed. If you do use Qt in your business or use it as a tool to generate profit, you need to pay the Qt Group. Otherwise, as long as you're open-sourced (or otherwise complying with the LGPL License), you're free to download to use, and good to go to install.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fot7w3qxy74jn68l6kc3a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fot7w3qxy74jn68l6kc3a.png" alt="Qt Open Source Page" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, scroll till you see a banner called "Looking for Qt Binaries?". Your version may vary, but there should always be a banner somewhat similar to this, and you will click on the "Qt Online Installer".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd8y3ymzzjbguxr3mr0to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd8y3ymzzjbguxr3mr0to.png" alt="Qt Download Binaries" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choose the operating system you're using. Be careful of the architecture, i.e. whether you're using a 32-bit computer or a 64-bit one, or you're using an Intel chip or an ARM chip (i.e. Apple chips series) or not. If you do not, Qt would be very naughty.&lt;/p&gt;

&lt;p&gt;I'm using a 64-bit Windows with an Intel chip. But the procedures should all be the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbozk9mypxr6169an7bjw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbozk9mypxr6169an7bjw.png" alt="Qt Selecting Operating System" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there are no Internet problems, you should download your online installer in a short time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feh7v0y38dwohuvfo66n7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feh7v0y38dwohuvfo66n7.png" alt="Qt Installer Downloaded" width="800" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the installer
&lt;/h2&gt;

&lt;p&gt;Execute the installer. You'll be prompted to fill in your Qt-account and password. Believe me, it's very safe to &lt;a href="https://login.qt.io/register" rel="noopener noreferrer"&gt;register one&lt;/a&gt;. You won't be giving any crucial personal information to the Qt Group, but instead, just a few steps would do and you could access to the &lt;a href="https://forum.qt.io" rel="noopener noreferrer"&gt;Qt Forum&lt;/a&gt; (the programmers' forum similar to Stack Overflow) and the &lt;a href="https://www.qt.io/academy" rel="noopener noreferrer"&gt;Qt Academy&lt;/a&gt; (the place where you could learn Qt better if you found these articles unhelpful), besides the rightful access to the installer. It's quick and completely free without any credit cards. Believe me.&lt;/p&gt;

&lt;p&gt;Then, click "Next." You will agree to the License Agreements, which is all based on the famous open-source license, the LGPL License, as mentioned previously.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlctpsv4jlthtxn81fej.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlctpsv4jlthtxn81fej.png" alt="License Agreements" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Preferences
&lt;/h2&gt;

&lt;p&gt;Click "Next" twice. Select if you would like to contribute to the development of the Qt's exclusive IDE, the Qt Creator. It's always Okay not to opt to improve, and no features provided would be affected. But as an open-source writer and developer, I always choose "Yes," and believe me, there's not really much information that the Qt Group could steal. Click "Next" again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpa61qygu9dwmsksw3i3j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpa61qygu9dwmsksw3i3j.png" alt="Qt Creator's Help" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Selecting paths and modes
&lt;/h2&gt;

&lt;p&gt;Now, it's time to select the installation path. Normally, I suggest you choosing &lt;code&gt;C:\Qt&lt;/code&gt; on Windows, or &lt;code&gt;~/Qt&lt;/code&gt; on UNIX-based systems (macOS, GNU/Linux, BSDs, etc.), for as a generally rule of software development, choosing the given and default installation path should always be the easiest to manage or to update.&lt;/p&gt;

&lt;p&gt;Also, choose "Custom Installation." I'll guide you how to choose the components next.&lt;/p&gt;

&lt;p&gt;If you have already a favorite code editor or IDE for your &lt;code&gt;.cpp&lt;/code&gt; files or &lt;code&gt;.h&lt;/code&gt; files, etc. like CLion or Kate, then I suggest you de-checking the option "Associate common file types with Qt Creator." It'll override your settings and make your favorite editors not be the default ones to edit C++ source files. On the other hand, if you are just a newbie at C++, I strongly suggest you checking this option. Qt Creator is one of the most powerful and strongest free-and-open-source IDE on this planet for the C++ programming language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fck2qbve43hv8cibu888p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fck2qbve43hv8cibu888p.png" alt="Qt Installer Select Path and Mode" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing components
&lt;/h2&gt;

&lt;p&gt;Now it's the big part: how to choose the components? Let me dissect for you:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzu4kin1iodqsq4cf4q7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzu4kin1iodqsq4cf4q7.png" alt="Selecting Components" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For Qt Design Studio, it's a powerful tool to design for QML file (the programming language crafted by Qt), even more powerful than its exclusive C++ IDE, Qt Creator. QML is basically the spokesperson of the mobile support of Qt, as well as Nokia (Nokia has once owned Qt from 2008 to 2012). But since I'm not that into Qt-based mobile development than native APIs or web-based APIs, I will not introduce it as much as I will do for Qt Creator later this series. Also, without QML, Qt Design Studio is not really that used for C++-based Qt Widgets, where the Qt Group started with.&lt;/p&gt;

&lt;p&gt;Long paragraph short: choose it if you're really a copain of Qt-based mobile development. I am not.&lt;/p&gt;

&lt;p&gt;For extensions, they are the extended components for the advanced Qt users to develop their application with demand of more advanced features. They include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Qt Insight Tracker provides the programs tracking application usages of their application or device. I have never used it and thus it will not appear in this series.&lt;/li&gt;
&lt;li&gt;Qt PDF provides the programs rendering PDF documents, including showing, bookmarking, parsing and interpreting them. I will be using this component later this series, as to make our own PDF viewer.&lt;/li&gt;
&lt;li&gt;Qt Web Engine provides the programs rendering web page and displaying them, based on the Chromium cores. In other words, you could just build a full-featured browser using this component. (How powerful Qt is.) We'll, definitely, be using it later this series for making our own browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's the detailed explanations on the cores of the Qt components. You are advised to select &lt;em&gt;all&lt;/em&gt; of them. To select &lt;em&gt;all&lt;/em&gt; of them, just check the version number:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm01dw7nkh8pqss4odv1z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm01dw7nkh8pqss4odv1z.png" alt="Selecting Core Components" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm just trying as I might to sum up the key Qt components sentence-pass and in simple words.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Active Qt: a C++/Qt binding of the ActiveX framework on Windows.&lt;/li&gt;
&lt;li&gt;Qt 3D: the best 3D rendering tool by native Qt, with features like Unity or Unreal Engine.&lt;/li&gt;
&lt;li&gt;Qt 5 Compatibility Module: some useful features like &lt;code&gt;QTextCodec&lt;/code&gt; are marked deprecated and removed in Qt 6 by the airhead Qt developers; and they're requested to add them back. (But this module will still be replaced in Qt 7, so sad).&lt;/li&gt;
&lt;li&gt;Qt Charts: a module for plotting 2D charts.&lt;/li&gt;
&lt;li&gt;Qt Connectivity: a module for basic Dial-up, (W)LAN, USB and VPN support.&lt;/li&gt;
&lt;li&gt;Qt Data Visualization: a module for plotting 3D charts. (I don't know why they did not name it "Qt 3D Charts".)&lt;/li&gt;
&lt;li&gt;Qt Graphs: a module aims for integrating and replacing Qt Charts and Qt Data Visualization.&lt;/li&gt;
&lt;li&gt;Qt HTTP Server: a module for serving files by the HTTP protocol, as the name suggests.&lt;/li&gt;
&lt;li&gt;Qt Image Formats: a module for providing extensions to interpret and parse different image file formats, namely&lt;code&gt;.gif&lt;/code&gt;, &lt;code&gt;.icns&lt;/code&gt;, &lt;code&gt;.ico&lt;/code&gt;, &lt;code&gt;.jpeg&lt;/code&gt;, &lt;code&gt;.pdf&lt;/code&gt;, &lt;code&gt;.svg&lt;/code&gt;, &lt;code&gt;.tga&lt;/code&gt;, &lt;code&gt;.tiff&lt;/code&gt;, &lt;code&gt;.wbmp&lt;/code&gt; and &lt;code&gt;.webp&lt;/code&gt; on Windows 11.&lt;/li&gt;
&lt;li&gt;Qt Language Server: a module for providing support to the &lt;a href="https://microsoft.github.io/language-server-protocol/" rel="noopener noreferrer"&gt;Language Server Protocol by Microsoft&lt;/a&gt;; it's mainly for the C++ and QML programming languages.&lt;/li&gt;
&lt;li&gt;Qt Location: a module for accessing and presenting map data, making touch gestures, querying for a specific geographical location and route and adding and searching places and layers on top; in other words, a module like Google Maps.&lt;/li&gt;
&lt;li&gt;Qt Lottie Animation: a module for using the Lottie animation framework in QML.&lt;/li&gt;
&lt;li&gt;Qt Multimedia: a module for recording and presenting the multimedia data platform-independently, including videos, audios and cameras.&lt;/li&gt;
&lt;li&gt;Qt Network Authorization: a module for providing network authorization, namely Open-Authorization versions 1 and 2.&lt;/li&gt;
&lt;li&gt;Qt Positioning: a module for testing and retrieving position information like the current location and speed.&lt;/li&gt;
&lt;li&gt;Qt Protobuf and Qt GRPC: a module for parsing specific data formats like Protobuf (Protocal Buffer).&lt;/li&gt;
&lt;li&gt;Qt Quick *: a module for assisting QML libraries.&lt;/li&gt;
&lt;li&gt;Qt Remote Objects: a module for manipulating the remote devices like GPS and printer access.&lt;/li&gt;
&lt;li&gt;Qt Sensors: a module for sensing the device environment, e.g. speed, acceleration, humidity, axes, etc.&lt;/li&gt;
&lt;li&gt;Qt Serial Bus: a module for accessing various serial bus protocols, like CAN, ModBus, and etc.&lt;/li&gt;
&lt;li&gt;Qt Serial Port: a module for accessing various serial port functions like configuration, I/O operation, etc.&lt;/li&gt;
&lt;li&gt;Qt Shader Tools: a Qt port for the Shader Tools.&lt;/li&gt;
&lt;li&gt;Qt Speech: a module for converting text to speech. The voice is similar to the Google Translate ones.&lt;/li&gt;
&lt;li&gt;Qt State Machines: a module for creating &lt;a href="https://en.wikipedia.org/wiki/Finite-state_machine" rel="noopener noreferrer"&gt;state machines&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Qt Virtual Keyboard: a module for creating virtual keyboards. (That are, the ones you will see on phones.)&lt;/li&gt;
&lt;li&gt;Qt Web Channel: a module for enabling peer-to-peer communication between a server (could be made with QML or C++) and a client (could be made with front-end languages like HTML or JavaScript, or QML).&lt;/li&gt;
&lt;li&gt;Qt Web Sockets: a module for providing web-based protocol designed to enable a two-way interactive communication session between a client app and a remote host.&lt;/li&gt;
&lt;li&gt;Qt Web View: a module for making browsers, same as the Qt Web Engine above, but it is for the QML fans.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, you will select the following build tools of their latest versions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MinGW,&lt;/li&gt;
&lt;li&gt;CMake,&lt;/li&gt;
&lt;li&gt;Ninja and&lt;/li&gt;
&lt;li&gt;OpenSSL Toolkit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They're useful in building in different scenarios.&lt;/p&gt;

&lt;p&gt;Lastly, the first-in-cosmos IDE, Qt Creator, should also be installed of its newest version. If you have space, I do suggest you also installing the debugging symbols and plugin developments.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trivia
&lt;/h2&gt;

&lt;p&gt;Finally, click "Next" to agree with (again) the GPL/ LGPL license. Click next&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxyvclnpdc846cy7j0axh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxyvclnpdc846cy7j0axh.png" alt="Qt Installer License Agreement" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Windows exclusive): On windows, there's an additional step: you're then about to choose the start menu folder. If you wish to access Qt Creator (and, presumably, the other essential tools) from the start menu, add Qt to the menu, then click "Next." Don't hesitate:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhp6p04v6b5giokdo0u34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhp6p04v6b5giokdo0u34.png" alt="Start-menu Qt" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, you're guided to a page called "Ready to Install." You will know how much space will the installation cost. Normally, these configurations cost around 40 GB, which is still quite feasible on modern PCs. However, if you feel they're still too big, try de-select some components like the additional libraries or external tools by clicking the "Back" button multiple times.&lt;/p&gt;

&lt;p&gt;Click "Install".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7niwymh2sggb6emcq2w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7niwymh2sggb6emcq2w.png" alt="Ready to Install" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Finishing
&lt;/h2&gt;

&lt;p&gt;As I have already had Qt installed, I could not really show you the whole installation process (with apologies!). The installation takes about one hour or two, depending on your Internet connection and the query/ request of the Qt code mirror that day.&lt;/p&gt;

&lt;p&gt;If you finished your installation, you can relaxedly check out the newest README file, explore Qt Creator, and most importantly, prepare for your odyssey of Qt.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>qt</category>
      <category>desktop</category>
    </item>
    <item>
      <title>[Qt] I. Why you should learn Qt</title>
      <dc:creator>Misinahaiya</dc:creator>
      <pubDate>Tue, 28 Jan 2025 10:21:11 +0000</pubDate>
      <link>https://dev.to/misinahaiya/qt-i-why-you-should-learn-qt-4jh4</link>
      <guid>https://dev.to/misinahaiya/qt-i-why-you-should-learn-qt-4jh4</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"&lt;em&gt;The future is written with Qt.&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;Qt Group&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is a general rule of software design, irrespective of where you are; regardless of the type of software you are crafting: I generally do not consider any frameworks or libraries will survive under the overly keen competitions among different tools in assisting the overall application design, nor any additional libraries that are not endorsed by the tech giants or the standards.&lt;/p&gt;

&lt;p&gt;Yet, Qt is obviously the exception - the extraordinary exception that will not perish in time. Ever.&lt;/p&gt;

&lt;p&gt;Released back in 1995, Qt continuously showed its unparalleled talents in producing either small-scale toy applications, as well as the heavy and strong titans. From time to time, software of various types and functions may disappear from the shelves, and be forgotten by the end users who soon have found better alternatives. When, in the next time, you surf the Internet or especially the Application Store, do remember peek that whether the things you installed on your computer a decade ago is still here. I could guarantee that over 75% will be gone without leaving a word nor reason.&lt;/p&gt;

&lt;p&gt;Undoubtedly, the ever-increasing demand from the market and the overly keen competition contribute significant reasons for software, tools and all sort of things being gone, which - the latter ones - were once all the rage. But, what's more, there are some reasons also contribute as significantly as the external factors:&lt;/p&gt;

&lt;h1&gt;
  
  
  What Are the Advantages of Qt?
&lt;/h1&gt;

&lt;p&gt;So, what reasons brought the perished apps or packages down? Or, in other words, what made Qt flourish? Let us investigate.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, it's cross-platform
&lt;/h2&gt;

&lt;p&gt;Yep, you're right. Stepping into the mobile era could we hardly recognize that how hard it is for cross-platform compatibility. In the bygone, dark days of computers of the 1980's to 1990's, where a single operation of "one-plus-one" took years and a &lt;em&gt;laptop&lt;/em&gt; looked like a &lt;em&gt;printer&lt;/em&gt;, every single piece of code was basically invalid on another machine, even it could be compiled successfully on one.&lt;/p&gt;

&lt;p&gt;As of then, computer scientists struggled. Why? It is just simply because that they could not find a &lt;em&gt;stable&lt;/em&gt; solution of &lt;em&gt;stable&lt;/em&gt; building and &lt;em&gt;stably&lt;/em&gt; deploy the &lt;em&gt;stable&lt;/em&gt; code. They were seeking their utopia of "&lt;em&gt;Write Once, Build Everywhere&lt;/em&gt;".&lt;/p&gt;

&lt;p&gt;"Will these come true?" Unfortunately no, at least in the 80's, as in the mid-1980's making an cross-platform solution was &lt;em&gt;far-fetched&lt;/em&gt; as there were not any standardized descriptions of how computers execute code via their chips and CPU's. For example, assume that an Intel CPU port 0xE which served for Microsoft DOS or Windows might be responsible for jumping, but an ARM chip will just be on strike.&lt;/p&gt;

&lt;p&gt;But things &lt;em&gt;have&lt;/em&gt; to go on. A cross-platform solution must be presented. Then, many firms had decided to make some protocols and all sort of fancy things to make cross-platform possible for the major programming languages like C/C++, Java or Python. As intended, the pursuit of cross-platform software successfully filtered a considerable portion of old and unstable pieces of software.&lt;/p&gt;

&lt;p&gt;So, why are the protocols and the cross-platform compatibility related to Qt? Well, as you might have already guessed, Qt is an extremely powerful library in terms of cross-platform compatibility. Basically, without any modification could your code be compiled on every platform as long as Qt itself supports so, given that you have not used any external or 3rd-party libraries.&lt;/p&gt;

&lt;p&gt;This already filtered out the some potential alternatives like Windows UI or Cocoa. Might you ask, what about Python's Tkinter or C++'s WxWidgets? Aren't they also cross-platform solutions?&lt;/p&gt;

&lt;h2&gt;
  
  
  Second, it's feature-rich
&lt;/h2&gt;

&lt;p&gt;To measure how &lt;em&gt;mature&lt;/em&gt; (or &lt;em&gt;good&lt;/em&gt;, whatever) a library is, the metrics we should be using is not quite the cost (let alone being open-source or the license used under), nor the community engagement. These are not that important as the metric: features. How many classes or namespaces of different genres the library has? How many languages does it support (or, how is the internationalization going)? How, or if it is, possible to implement both simple apps like a Notepad or complex apps like GIMP?&lt;/p&gt;

&lt;p&gt;It is, undoubtedly, filtered out another good portion of the potential alternatives of developing applications.&lt;/p&gt;

&lt;p&gt;I have used almost all the major application development frameworks, including Python's Tkinter (and some of its derivatives), C++'s Qt and WxWidgets, as well as Java's Swing. All of them are not that &lt;em&gt;holistic&lt;/em&gt; enough. How? Why?&lt;/p&gt;

&lt;p&gt;A picture is worth a thousand words. Look at the Qt classes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgaenz6lvcfm81gyoirw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgaenz6lvcfm81gyoirw.png" alt="Things with Qt" width="800" height="655"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are not the screens of the crackers used in films. These are the synopsis of all the Qt classes. Not only GUI design, but Qt is able and mighty to deal with networking (Qt Network), code editing (Scintilla), communicating with D-Bus (Qt D-Bus) on Embedded or ActiveX servers on Windows (Qt ActiveX), unit testing (Qt Test), doing Bluetooth (Qt Bluetooth), charting (Qt Chart and Qt Data Visualization) and XML (Qt XML), authorizing using Open-authorization (Qt OAuth), doing with SQL (Qt SQL) or serial ports (Qt Serial Port), and making web applications or browsers (Qt Web Channel, Qt Web Socket, Qt Web Engine and Qt Web Engine Widgets). Want to play music or video? Even better, record one? Use Qt Multimedia with Qt Multimedia Widget! Want to make your own 3D Google Map or Google Earth with JavaScript? Use the Qt Quick (3D) of the Qt QML (Qt's own programming language) with Qt Location! The list goes on and cannot reach an end.&lt;/p&gt;

&lt;p&gt;How to prove so? Look at the big players in software design:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adobe's Photoshop Element uses Qt.&lt;/li&gt;
&lt;li&gt;Google's Google Earth uses Qt.&lt;/li&gt;
&lt;li&gt;TeamViewer uses Qt.&lt;/li&gt;
&lt;li&gt;Telegram uses Qt.&lt;/li&gt;
&lt;li&gt;VideoLAN's VLC Player uses Qt.&lt;/li&gt;
&lt;li&gt;Oracle's Virtual Box uses Qt.&lt;/li&gt;
&lt;li&gt;Dreamworks Pictures uses Qt.&lt;/li&gt;
&lt;li&gt;Disney's animations use Qt.&lt;/li&gt;
&lt;li&gt;Chinese office, the WPS Office, uses Qt.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Again, the list goes on and cannot reach an end.&lt;/p&gt;

&lt;p&gt;"Okay, I admit that Qt is powerful. But this does not prove the other cross-platform frameworks and persuades me to learn Qt." Yep, how about WxWidgets or Swing, which was once favored, or Tkinter? Well, they are, in the contrary, not that feature rich. An example: you cannot paint widgets &lt;em&gt;freely&lt;/em&gt; in WxWidgets or Swing as it does not provide any good interfaces for the program to customize the widgets and regions (ah yes, customization should be an important feature). In Tkinter it's crazier and more nonsense: it does not even provide any ways for the program to paint their widgets.&lt;/p&gt;

&lt;p&gt;A more subtle example: you have all encountered the "Justify" option in Microsoft Word or LibreOffice Writer, right? So you could expect that every GUI library has an option on its rich-text editor: justify. Disappointingly, neither WxWidgets nor Swing nor Tkinter implement so. Only Qt, which is almost almighty in every aspect of application development could do so. Furthermore, you could launch an HTML file in rich editing (yep, besides normal web browsing) with &lt;a href="https://doc.qt.io/qt-6/richtext-html-subset.html" rel="noopener noreferrer"&gt;a bit limited support&lt;/a&gt;. Frankly, I haven't encounter any frameworks with this level of meticulous design.&lt;/p&gt;

&lt;h1&gt;
  
  
  You Couldn't Say Qt Is Flawless
&lt;/h1&gt;

&lt;p&gt;I do admit that Qt is not flawless, as any other thing in the word does.&lt;/p&gt;

&lt;h2&gt;
  
  
  It's devilishly hard!
&lt;/h2&gt;

&lt;p&gt;Qt does, indeed, have a steep learning curve. You should get to know the basics before you get on to craft any projects - this rules is applied everywhere, for every tools it is the general rule. However, in Qt, the "&lt;em&gt;basics&lt;/em&gt;" should be defined as the "&lt;em&gt;advanced pattern&lt;/em&gt;" in other tools. Setting up a project might include necessary knowledge on CMake or QMake (either one used for the build system), &lt;code&gt;QObject&lt;/code&gt; and &lt;code&gt;QApplication&lt;/code&gt;, as well as the model/ view structure and the basic uses of each widget.&lt;/p&gt;

&lt;p&gt;But fear not! For in this series I am going to help all of you. From as simple as the basic moves like creating a &lt;code&gt;QApplication&lt;/code&gt; instance, to the most advanced moves like crafting a LibreOffice Writer-like application as the hands-on projects, I am going to explain every detail that matters as I might. The key is, to have a committed heart in learning Qt and to try to do everything as you might, and enjoy your learning process. That was also my key believes in learning Qt.&lt;/p&gt;

&lt;h2&gt;
  
  
  I don't have a buck!
&lt;/h2&gt;

&lt;p&gt;Regarding to the cost, Qt (and almost all of its derivatives) has a dual-license system. To explain, if you &lt;em&gt;sell&lt;/em&gt; your application (or make &lt;em&gt;any&lt;/em&gt; profit from your application) developed by Qt, you have to pay Qt an amount of money calculated from the profit you have gained. Someone groaned that the system is expensive and unfair, as they have only seen the expensive Enterprise solutions. Indeed, they are expensive. They just mainly provide supports for the non-developers in the company for those which have already written on the documentation. So, we, as developers, do not really need these kinds of services like instructive or installations.&lt;/p&gt;

&lt;p&gt;They did not also consider that if you did not earn any profit from your applications, you do not need to pay Qt as it uses the &lt;a href="https://www.gnu.org/licenses/lgpl-3.0.html" rel="noopener noreferrer"&gt;LGPL license&lt;/a&gt;, which enables you to use the software freely under certain conditions. Qt is fair, if you do not let others pay a buck, you do not need to pay a buck.&lt;/p&gt;

&lt;h1&gt;
  
  
  In A Nutshell...
&lt;/h1&gt;

&lt;p&gt;All these point to that broad and open road, where both firms like Google and Oracle, and individuals meet and are more crowded than the Times Square in New York. The road is called, "Go Learn Qt".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenlts5erlcas1fz50tlh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenlts5erlcas1fz50tlh.png" alt="Editor Sample" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This editor is built entirely with Qt and C++. Do you want to have one? Stay tuned to this series and wait till the end, where I will teach you how to build an editor like this, an editor like Microsoft Word and many more.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>qt</category>
      <category>desktop</category>
    </item>
  </channel>
</rss>
