title: QML reports ReferenceError: XYZ is not defined on C++ object added to context
body: <p>I've started learning <code>QML</code> recently (after trying it out a long time ago) and I'm stuck at the way <code>Qt</code> <code>C++</code> code interacts with <code>QML</code> and vice versa.</p>
  
  <p>I have a <code>Counter</code> which has the following header:</p>
  
  <pre><code>#include &lt;QObject&gt;
  #include &lt;QTimer&gt;
  
  class Counter : public QObject
  {
      Q_OBJECT
      Q_PROPERTY(int count
             READ getCount
             WRITE setCount
             NOTIFY signalCountChanged)
  public:
      Counter(QObject *parent = Q_NULLPTR);
      int getCount();
      void setCount(int count);
  signals:
      void signalCountChanged(int);
  public slots:
      void slotStart();
  private slots:
      void slotTimeout();
  private:
      int count;
      QTimer *timer;
  };
  </code></pre>
  
  <p>My <code>main.cpp</code> is as follows:</p>
  
  <pre><code>#include &lt;QtGui/QGuiApplication&gt;
  #include &lt;QtQml/QQmlContext&gt;
  #include &lt;QtGui/QGuiApplication&gt;
  #include &lt;QtQuick/QQuickItem&gt;
  #include &lt;QtQuick/QQuickView&gt;
  #include "counter.h"
  
  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
  
      qmlRegisterType&lt;Counter&gt;("org.qmlplayground.counter", 0, 1, "Counter");
  
      QQuickView view;
      view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
  
      QObject *viewO = dynamic_cast&lt;QObject*&gt;(view.rootObject());
      Counter c;
      // Register Counter instance as "counter" property of top level context so that it can be accessed from within the QML code (for example: set the value count)
      view.rootContext()-&gt;setContextProperty("counter", &amp;c);
  
      QObject::connect(viewO, SIGNAL(signalStartCounter()),
              &amp;c, SLOT(slotStart()));
      QObject::connect(viewO, SIGNAL(signalQuit()), &amp;app, SLOT(quit()));
      view.show();
  
      return app.exec();
  }
  </code></pre>
  
  <p>and finally part of my <code>QML</code> (the <code>main.qml</code> which is loaded in the <code>QQuickView</code> while the rest being an UI form):</p>
  
  <pre><code>import QtQuick 2.7
  import QtQuick.Window 2.2
  
  // Importing some JavaScript files
  import "qrc:/loggingFunctions.js" as LOG
  import "qrc:/parseFunctions.js" as PARSE
  
  // Importing a Qt C++ custom module
  import org.qmlplayground.counter 0.1
  
  MainForm {
      property int countState: counter.count // ERROR HERE
      signal signalStartCounter()
      signal signalQuit()
  
      anchors.fill: parent
  
      textInputMouseArea.onClicked: {
          LOG.logger("Clicked! Selecting all text in text input field", "N")
          textInput.selectAll()
      }
  
      textInput.onAccepted: {
          if(textInput.text === "quit") signalQuit()//Qt.quit();
          if(textInput.text === "help") textInput.text = LOG.logger("Displaying help", "H");
  
          var res = PARSE.parseInput(textInput.text);
          if(res &amp;&amp; (typeof res === 'object') &amp;&amp; res.constructor === Array) {
              switch(res[0]) {
              case "fact":
                  labelOutput.text = res[1];
                  break;
              case "count":
                  counter.count = res[1];
                  signalStartCounter();
                  break;
              }
          }
      }
  
      onCountStateChanged:
          console.log("Hello")
  
      textInput.onTextChanged:
          console.log("Text changed");
  }
  </code></pre>
  
  <p>As you can see I've already tested two signals sent from my <code>QML</code> to my <code>C++</code> code one being connected to my <code>QGuiApplication</code>'s slot <code>quit()</code> and the other being connected to my <code>counter</code>'s slot <code>slotStart()</code>. It works fine. It seems that the line</p>
  
  <pre><code>counter.count = res[1];
  </code></pre>
  
  <p>doesn't cause any issues (perhaps because it's <code>JS</code> and not <code>QML</code>?). Now I want to read the <code>count</code> value of my <code>Counter</code> instance and update my UI accordingly. If I'm not mistaken every <code>QML</code> property automatically gets a couple of things one of these being the <code>onChanged</code> method (event handler or whatever it's called).</p>
  
  <p>When I run my code however I get </p>
  
  <pre><code>`qrc:/main.qml:21: ReferenceError: counter is not defined
  </code></pre>
  
  <p>I thought that doing <code>view-&gt;rootContext()-&gt;setContextProperty("counter", &amp;c);</code> would be enough but it seems that I'm missing something. So the more general question would be how do I <strong>properly</strong> make a <code>C++</code> object visible in <code>QML</code> context.</p>