title: QTQuick design: how to have a global class available from both C++ and QML?
body: <p>I'm converting a project made with Borland C++Builder.
  There I have a class that manages messages for and from an electronic device. This class (call it MsgManager) encapsulate both a serial port and a tcpsocket, provides unified method and events no matter what kind of connection is used as long as some properties, and handle the whole protocol checks.</p>
  
  <p>I want this class to be "global": so each of all my other classes have a pointer to the MsgManager, but also QML pages needs to use his methods and properties. The only thing that QML pages will not use are the various "events" (like a datareceived events or some error like timouts etc)</p>
  
  <p>1) Is this a bad design for a QT applications? Seems that my old habits are sometime wrong in this new enviroment...</p>
  
  <p>2)if no (or at least not THAT bad) how can i obtain this?
  I tried using
  <code>qmlRegisterType&lt;cMessageManager&gt;("Phase.MessageManager", 1, 0, "MessageManager");</code>
  in main.cpp and then</p>
  
  <p><code>MessageManager {
          id: msgman;
      }</code></p>
  
  <p>in the mainForm.qml but i'm not able to "point" to the class from C++... and i'm not able to view the class from QML when i create it in c++</p>
  
  <p>Thanks.</p>
  
  <p><strong><em>-EDIT-
  Adding code and asking for clarification:</em></strong></p>
  
  <p>@leemes:
  Thanks for the detailed (but noob-friendly :) ) answer.</p>
  
  <p>I still have troubles....
  this was my previous working code (even before trying to adding my new class):</p>
  
  <pre><code>#include &lt;QtGui/QGuiApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QtQml&gt;
  #include &lt;QtQuick/QQuickView&gt;   // Necessario per QQuickWindow
  
  #include "ui_updater.h"
  
  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
      qmlRegisterType&lt;UI_Updater&gt;("Phase.UI_Updater", 1, 0, "UI_Updater");
      QQmlApplicationEngine engine(QUrl("qrc:/qml/MainForm.qml"));
      QObject *topLevel = engine.rootObjects().value(0);
      QQuickWindow *window = qobject_cast&lt;QQuickWindow *&gt;(topLevel);
      if ( !window ) {
          qWarning("Error: Your root item has to be a Window.");
          return -1;
      }
      window-&gt;show();
      return app.exec();
  }
  </code></pre>
  
  <p>I tried this changes:</p>
  
  <pre><code>#include &lt;QtGui/QGuiApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QtQml&gt;
  #include &lt;QtQuick/QQuickView&gt;   // Necessario per QQuickWindow
  
  #include "ui_updater.h"
  
  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
  
      // --- CHANGES TO TRY setContextProperty SOLUTION -------------------------
      //qmlRegisterType&lt;UI_Updater&gt;("Phase.UI_Updater", 1, 0, "UI_Updater");
      //QQmlApplicationEngine engine(QUrl("qrc:/qml/MainForm.qml"));
  
      UI_Updater*         ui_up;
      ui_up = new UI_Updater();
  
      QQmlApplicationEngine engine;
      engine.rootContext()-&gt;setContextProperty("UI_Updater",ui_up);
      engine.setBaseUrl(QUrl("qrc:/qml/MainForm.qml"));
      //-------------------------------------------------------------------------
  
      QObject *topLevel = engine.rootObjects().value(0);
      QQuickWindow *window = qobject_cast&lt;QQuickWindow *&gt;(topLevel);
      if ( !window ) {
          qWarning("Error: Your root item has to be a Window.");
          return -1;
      }
      window-&gt;show();
      return app.exec();
  }
  </code></pre>
  
  <p>But the debugger breaks somewhere in the disassembler window with a "segmentation fault" error.
  I see that i don't have the QQuickView (or QtQuickApplicationViewer) object you mentioned, and i can't recall why i ended up with the code i have now (but it was the only way i found to make it work :( )</p>
  
  <p>So, could you help me a tiny bit again?</p>