title: My Blackberry 10 Cascades (C++, Qt & QML) sample app is crashing on startup with the following error
body: <p>I am trying to make a sample Blackberry 10 Cascades app in C++, Qt, &amp; QML in the QNX Momentics IDE, and run it in the Blackberry 10 Dev Alpha Simulator. When I start running it, I get the following error messages:</p>
  
  <p>ApplicationPrivate::declarativeEngine: ERROR called from non-UI thread QThread(0x8067e48) 
  ApplicationPrivate::declarativeEngine: Method called from non-UI thread</p>
  
  <p>I have tried googling those error messages - but I only find a few links that actually apply to my situation - and the solutions for those problems don't seem to apply to me.</p>
  
  <p>I know it's got something to do with me trying to call different classes from different qml documents.  (Which is something I was having trouble with before - which is why I made this sample app in the first place - to see if I could figure out how to call different classes from different qml documents).</p>
  
  <p>Here is the code of my sample app. There are 7 files (<code>main.qml</code>, <code>main.cpp</code>, <code>app.cpp</code>, <code>app.hpp</code>, <code>Page2.cpp</code>, <code>Page2.h</code>, <code>Page2.qml</code>).  I would much appreciate if anyone could help me solve this - because I am not really sure what I am doing wrong.</p>
  
  <p>Thank you.</p>
  
  <p>(I have taken out <code>main.cpp</code> &amp; reduced the code from the other 6 files to what I think are the key parts to this problem).</p>
  
  <p>I know the problem has something to do with my <code>Page2</code> class and the <code>Page2.qml</code> file.</p>
  
  <p>(I think the problem is that there is something I have to do to be able to call qt code from more than one qml files - and I just don't know what that is).</p>
  
  <p><code>main.qml</code>:</p>
  
  <hr>
  
  <pre><code>import bb.cascades 1.0
  
  //-- create one page with a label and text
  Page {
      content: Container {
          Button {
              text: "Page 2"
              onClicked: app.goToPage2
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>from <code>app.cpp</code></p>
  
  <hr>
  
  <pre><code>App::App()
  {
      qmlRegisterType&lt;App&gt;("GDLiteBB", 1, 0, "app");
      qmlRegisterType&lt;Page2&gt;("GDLiteBB", 1, 0, "Page2");
  
      QmlDocument *qml = QmlDocument::create("main.qml");
      qml-&gt;setContextProperty("app", this);
  
      AbstractPane *root = qml-&gt;createRootNode&lt;AbstractPane&gt;();
      Application::setScene(root);
  }
  
  void App::goToPage2() {
      Page2::getInstance();
  }
  </code></pre>
  
  <hr>
  
  <p>from <code>app.hpp</code></p>
  
  <hr>
  
  <pre><code>class App : public QObject
  {
      Q_OBJECT
  public:
      App();
  
      Q_INVOKABLE void goToPage2();
  };
  </code></pre>
  
  <hr>
  
  <p><code>Page2.cpp</code></p>
  
  <hr>
  
  <pre><code>QmlDocument * Page2::qml_ = QmlDocument::create("Page2.qml");
  AbstractPane * Page2::root_ = qml_-&gt;createRootNode&lt;AbstractPane&gt;();
  Label * Page2::label_ = root_-&gt;findChild&lt;Label *&gt;("label");
  
  Page2::Page2() : QObject() {
      ++count_;
      page2_ = this;
      qml_-&gt;setContextProperty("Page2", this);
      Application::setScene(root_);
  }
  
  Page2 *Page2::getInstance() {
      return page2_ ? page2_ : new Page2();
  }
  
  void Page2::setLabel() {
      label_-&gt;setText("This is page 2");
  }
  </code></pre>
  
  <hr>
  
  <p><code>Page2.h</code></p>
  
  <hr>
  
  <pre><code>class Page2 : public QObject {
      Q_OBJECT
  
      static Page2 *page2_;
      static unsigned int count_;
  
      static bb::cascades::QmlDocument *qml_;
      static bb::cascades::AbstractPane *root_;
      static bb::cascades::Label *label_;
  
      Page2();
  public:
      static Page2 *getInstance();
      virtual ~Page2();
  
      Q_INVOKABLE void setLabel();
  };
  </code></pre>
  
  <hr>
  
  <p><code>Page2.qml</code></p>
  
  <hr>
  
  <pre><code>import bb.cascades 1.0
  import GDLiteBB 1.0
  
  Page {
      content: Container {
          Button {
              text: "Set Label"
              onClicked: Page2.setLabel()
          }
          Label {
              objectName: "Label"
          }
      }
  }
  </code></pre>