title: How to register custom type with QML if it is passed as const from C++
body: <p>I have a C++ class called <code>MyClass</code>. This class is registered as an uncreatable type so it can be referred by name in QML.</p>
  
  <p><code>MyClass.h</code>:</p>
  
  <pre><code>#pragma once
  
  #include &lt;QObject&gt;
  
  class MyClass : public QObject {
  Q_OBJECT
  
  public:
      static void initQML();
  
      MyClass();
      MyClass(const MyClass &amp;myClass);
      ~MyClass();
  
      Q_INVOKABLE void printText();
  };
  </code></pre>
  
  <p><code>MyClass.cpp</code>:</p>
  
  <pre><code>#include "MyClass.h"
  
  #include &lt;QtQml&gt;
  #include &lt;QDebug&gt;
  #include &lt;QMetaType&gt;
  
  /*static*/ void MyClass::initQML() {
      qmlRegisterUncreatableType&lt;const MyClass&gt;("Test", 1, 0, "MyClass", "Don't create it");
  
      qRegisterMetaType&lt;const MyClass*&gt;("const MyClass*");
  }
  
  MyClass::MyClass() {
      qDebug() &lt;&lt; "Constructor called.";
  }
  
  MyClass::MyClass(const MyClass &amp;myClass) {
      qDebug() &lt;&lt; "Copy constructor called.";
  }
  
  MyClass::~MyClass() {
      qDebug() &lt;&lt; "Destructor called.";
  }
  
  void MyClass::printText() {
      qDebug() &lt;&lt; "This a sample text.";
  }
  </code></pre>
  
  <p>I have another class which is called <code>MyClassFactory</code>. This class creates <code>MyClass</code> objects. It is registered as a singleton type to QML.</p>
  
  <p><code>MyClassFactory.h</code>:</p>
  
  <pre><code>#pragma once
  
  #include "MyClass.h"
  
  #include &lt;QObject&gt;
  
  class MyClassFactory : public QObject {
  Q_OBJECT
  
  public:
      static void initQML();
  
      MyClassFactory();
  
  public slots:
      static const MyClass *makeDefaultMyClass();
  };
  </code></pre>
  
  <p><code>MyClassFactory.cpp</code>:</p>
  
  <pre><code>#include "MyClassFactory.h"
  
  #include &lt;QtQml&gt;
  #include &lt;QQmlEngine&gt;
  #include &lt;QJSEngine&gt;
  
  QObject *simpleQmlRegisterSingletonTypeCallback(QQmlEngine *, QJSEngine *) {
      return new MyClassFactory();
  }
  
  /*static*/ void MyClassFactory::initQML() {
      qmlRegisterSingletonType&lt;MyClassFactory&gt;("Test", 1, 0, "MyClassFactory",
                                               simpleQmlRegisterSingletonTypeCallback);
  }
  
  MyClassFactory::MyClassFactory() {}
  
  /*static*/ const MyClass* MyClassFactory::makeDefaultMyClass() {
      return new MyClass();
  }
  </code></pre>
  
  <p>In my <code>main.qml</code> I create a property of <code>MyClass</code> type and bind a value to it via <code>MyClassFactory</code>. Here is my <code>main.qml</code>:</p>
  
  <pre><code>import QtQuick 2.9
  import QtQuick.Controls 2.2
  
  import Test 1.0
  
  ApplicationWindow {
      visible: true
  
      width: 480
      height: 272
  
      property MyClass myClass: MyClassFactory.makeDefaultMyClass()
  
      Button {
          anchors.centerIn: parent
  
          text: "Create MyClass"
  
          onClicked: myClass.printText()
      }
  }
  </code></pre>
  
  <p>When I build and run my code, I get the following error in the console:</p>
  
  <p><code>qrc:/main.qml:12: Error: Unknown method return type: const MyClass*</code></p>
  
  <p>If I register <code>const MyClass*</code> as a meta type, then this error disappears, but I get another one:</p>
  
  <p><code>qrc:/main.qml:12:31: Unable to assign const MyClass* to MyClass*</code></p>
  
  <p>First of all why do I have to register <code>const MyClass*</code> as a meta type, if I have already registered <code>const MyClass</code> as a QML (uncreatable) type?</p>
  
  <p>Main question is how do I pass a const pointer to QML?</p>
  
  <p>For a full running example, here is my <code>main.cpp</code>:</p>
  
  <pre><code>#include &lt;QGuiApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  
  #include "MyClass.h"
  #include "MyClassFactory.h"
  
  int main(int argc, char *argv[])
  {
      QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  
      QGuiApplication app(argc, argv);
  
      // Init
  
      MyClass::initQML();
      MyClassFactory::initQML();
  
      // Main loop
  
      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  
      return app.exec();
  }
  </code></pre>
  
  <p>I use Qt 5.9.5.</p>