title: How to register QObject class in CMake with qt_add_qml_module?
body: <p>I have a <code>QObject</code> derived class <code>Expense</code> that I use in QML like this.</p>
  <pre><code>// main.qml
  Expense {
    id: expenseManager
    onExpenseCreated: {
        // Do something
    }
  }
  </code></pre>
  <p>The expense class has no UI components, it has some basic Signal and Slots for API communications.</p>
  <pre><code>// expense.h
  
  #ifndef EXPENSE_H
  #define EXPENSE_H
  #include &lt;QObject&gt;
  #include &lt;QString&gt;
  
  #include &quot;service.h&quot;
  
  class Expense : public QObject
  {
      Q_OBJECT
  
  private:
      Service service;
      void networkError();
      bool buttonLock = false;
  
  public:
      explicit Expense(QObject *parent = nullptr);
  
  
  public slots:
      void createInvoice(QString item, float amount);
  
  signals:
      void expenseCreated();
  
  };
  
  #endif // EXPENSE_H
  </code></pre>
  <p>I have used <code>qmlRegisterType()</code> for registering <code>Expense</code> type in QML. Below is how my <code>main()</code> looks like.</p>
  <pre><code>int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
      ......
  
  
      qmlRegisterType&lt;Expense&gt;(&quot;com.kadbyte.expense&quot;, 1, 0, &quot;Expense&quot;);
  
      ........
      return app.exec();
  }
  </code></pre>
  <p>Everything working perfectly as it used to. But recently I have upgraded my project to QT6 with CMake as the build tool instead of QMake. In the docs I saw that we can use <code>qt_add_qml_module</code> command in CMakeList.txt to register C++ Classes instead of <code>qmlRegisterType()</code>, by adding <code>QML_ELEMENT</code> macro to the QObject class.</p>
  <p>But I can't understand how to do this, the documentation doesn't make sense as it uses qmake example (<a href="https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html#registering-an-instantiable-object-type" rel="nofollow noreferrer">Link to docs</a>) instead of CMake. Below is my CMakeLists.txt file</p>
  <pre><code>cmake_minimum_required(VERSION 3.16)
  
  project(Udyan VERSION 0.1 LANGUAGES CXX)
  
  set(CMAKE_AUTOMOC ON)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
  
  find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)
  
  qt_add_executable(appUdyan
      main.cpp
      expense.h expense.cpp
  )
  
  qt_add_qml_module(appUdyan
      URI Udyan
      VERSION 1.0
      QML_FILES qml/main.qml
  )
  
  set_target_properties(appUdyan PROPERTIES
      MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
      MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
      MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
      MACOSX_BUNDLE TRUE
      WIN32_EXECUTABLE TRUE
  )
  
  target_compile_definitions(appUdyan
      PRIVATE $&lt;$&lt;OR:$&lt;CONFIG:Debug&gt;,$&lt;CONFIG:RelWithDebInfo&gt;&gt;:QT_QML_DEBUG&gt;)
  
  target_link_libraries(appUdyan
      PRIVATE Qt6::Quick)
  </code></pre>
  <p>So how to use <code>qt_add_qml_module</code> to registering QObject class to use in QML?</p>
  <p>Note: All the example I have given above is just an MRE and not my complete code.</p>