title: Access C++ function from QML
body: <p>I'm trying to make a little program with Qt. I have a <code>main.cpp</code> with the following code:</p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include "qmlapplicationviewer.h"
  
  Q_DECL_EXPORT int main(int argc, char *argv[])
  {
      QScopedPointer&lt;QApplication&gt; app(createApplication(argc, argv));
  
      QmlApplicationViewer viewer;
      viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
      viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
      viewer.showExpanded();
  
      return app-&gt;exec();
  }
  
  int reken_tijden_uit(){
      return true;
  }
  </code></pre>
  
  <p>and I have a <code>.qml</code> file:</p>
  
  <pre><code>import QtQuick 1.1
  
  Rectangle {
  
  width: 360
  height: 360
  Text {
      text: qsTr("Hello World")
      anchors.centerIn: parent
  }
  MouseArea {
      anchors.fill: parent
      onClicked: {
          Qt.quit();
      }
  }
  }
  </code></pre>
  
  <p>Now, when I click on the <code>MouseArea</code>, the program quits. What I want is that it calls the function <code>reken_tijden_uit</code> in the <code>main.cpp</code> file.</p>
  
  <p>I've googled a lot, and searched on this site to. I've found a couple of answers, but I didn't get one working.</p>
  
  <p>So what code do I put where so I can call the function <code>reken_tijden_uit</code> in C++?</p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p>The header file looks like this:</p>
  
  <pre><code>#ifndef EIGEN_FUNCTION_HEADER_H
  #define EIGEN_FUNCTION_HEADER_H
  
  class MyObject : public QObject{
     Q_OBJECT
  public:
      explicit MyObject (QObject* parent = 0) : QObject(parent) {}
      Q_INVOKABLE int reken_tijden_uit(){
      return 1;
      }
  };
  
  #endif // EIGEN_FUNCTION_HEADER_H
  </code></pre>
  
  <p><code>main.cpp</code>:</p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include "qmlapplicationviewer.h"
  #include "eigen_function_header.h"
  
  QScopedPointer&lt;QApplication&gt; app(createApplication(argc, argv));
  
  qmlRegisterType&lt;MyObject&gt;("com.myself", 1, 0, "MyObject");
  
  Q_DECL_EXPORT int main(int argc, char *argv[])
  {
      QScopedPointer&lt;QApplication&gt; app(createApplication(argc, argv));
  
      QmlApplicationViewer viewer;
      viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
      viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
      viewer.showExpanded();
  
      return app-&gt;exec();
  }
  </code></pre>
  
  <p>and the QML file:</p>
  
  <pre><code>import QtQuick 1.1
  import com.myself 1.0
  
  Rectangle {
      width: 360
      height: 360
      Text {
          text: qsTr("Hello World")
          anchors.centerIn: parent
      }
      MyObject {
          id: myobject
      }
      MouseArea {
          anchors.fill: parent
          onClicked: {
              myobject.reken_tijden_uit()
          }
      }
  }
  </code></pre>
  
  <p>And the errors are as follow:</p>
  
  <pre><code>D:\*\main.cpp:6: error: 'argc' was not declared in this scope
  D:\*\main.cpp:6: error: 'argv' was not declared in this scope
  D:\*\main.cpp:8: error: expected constructor, destructor, or type conversion before '&lt;' token
  </code></pre>
  
  <p>So what did I do wrong?</p>