title: Where to place Q_DECLARE_METATYPE statement when using header and source files?
body: <p>I'm using my custom <code>struct</code> within <code>QWizard</code> pages. Let's say a <code>struct</code> (or <code>class</code>) like so:</p>
  
  <pre><code>// foo.h
  
  namespace nm{
  
  struct Foo
  {
      Foo();
      void bar();
  };
  
  }
  </code></pre>
  
  <p>I want to be able to <code>registerField</code> with a <code>Q_PROPERTY</code> of type <code>nm::Foo</code>, for example:</p>
  
  <pre><code>// within some QWizardPage class
  Q_PROPERTY(nm::Foo foo READ getFoo WRITE setFoo)
  </code></pre>
  
  <p>I know that in order for me to use it, I have to declare it as Qt metatype: </p>
  
  <pre><code>Q_DECLARE_METATYPE(nm::Foo)
  </code></pre>
  
  <p>But I cannot figure out where to correctly place the above statement. I tried to place it at the end of <code>Foo.cpp</code>, but get a compilation error:</p>
  
  <blockquote>
    <p>path/QtCore/qglobal.h:738: error: static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system. #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)</p>
  </blockquote>
  
  <p>If I place it at the end of header file while also having a source file, I get: </p>
  
  <blockquote>
    <p>/usr/lib/gcc/x86_64-linux-gnu/5/include/stddef.h:149: error: expected constructor, destructor, or type conversion before ‘typedef’
     typedef PTRDIFF_TYPE ptrdiff_t;</p>
  </blockquote>
  
  <p>If I move all the definitions to the <code>Foo.h</code> and use it as a header-only file, then if I place the statement at the end of the file (outside of the namespace), it works. However, my class definitions are kind of long, and I want my class definitions to be placed at the source file.</p>
  
  <p>Is there any way I can use <code>Q_DECLARE_METATYPE</code> while having header and source files for my class and not a header-only file?</p>
  
  <p><strong>EDIT (Solution):</strong> I had to <em>fully define</em> my class, i.e., provide public default ctor, copy ctor and dtor. The final code looks like:</p>
  
  <pre><code>// somewhere in Foo.h
  #include &lt;QMetaType&gt;
  
  namespace nm{
  struct Foo
  {
      Foo();
      Foo(const Foo&amp; other);
      ~Foo();
  
      void bar();
  };
  } 
  
  Q_DECLARE_METATYPE(nm::Foo)
  </code></pre>