title: How to invoke a function defined inside of a component?
body: <h2>Setup</h2>
  
  <p>My system is KUbuntu 17.10, Qt 5.9.</p>
  
  <p>I have a stackview, which actually holds many more components, but I've cut them out for the sake of brevity. Basically, when "Return the Item" button gets pressed, <code>submitReturnButton</code> signal is caught on the C++ side. Catching side doesn't have any state, it just tries to send text to set on label.</p>
  
  <hr>
  
  <h2>Code</h2>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QGuiApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QObject&gt;
  #include &lt;QQuickWindow&gt;
  
  #include "returnserver.h"
  
  int main(int argc, char *argv[])
  {
      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      QGuiApplication app(argc, argv);
  
      QQmlApplicationEngine engine;
      engine.load(QUrl(QLatin1String("qrc:/main.qml")));
      if (engine.rootObjects().isEmpty())
          return -1;
  
      //boilerplate ends here
  
      QObject *topLevel = engine.rootObjects().value(0);
      QQuickWindow *window = qobject_cast&lt;QQuickWindow *&gt;(topLevel);
      QObject* returnStatusLabel = engine.findChild&lt;QObject*&gt;("returnStatusLabel");
  
      ReturnServer returner(returnStatusLabel);
  
      QObject::connect(window, SIGNAL(submitReturnForm(QString)), &amp;returner, SLOT(returnPressed(QString)));
      return app.exec();
  }
  </code></pre>
  
  <p><strong>returnserver.h</strong></p>
  
  <pre><code>#ifndef RETURNSERVER_H
  #define RETURNSERVER_H
  
  #include &lt;QObject&gt;
  #include &lt;QVariant&gt;
  #include &lt;QString&gt;
  #include &lt;QQuickWindow&gt;
  
  class ReturnServer : public QObject
  {
      Q_OBJECT
  
      QObject* target;
  public:
      ReturnServer(QObject* target, QObject* parent = nullptr);
  
  public slots:
      void returnPressed(const QString&amp; str);
  };
  
  #endif // RETURNSERVER_H
  </code></pre>
  
  <p><strong>returnserver.cpp</strong></p>
  
  <pre><code>#include "returnserver.h"
  #include &lt;QString&gt;
  #include &lt;QVariant&gt;
  
  ReturnServer::ReturnServer(QObject* target, QObject *parent) :
      target(target),
      QObject(parent)
  {
  
  }
  
  void ReturnServer::returnPressed(const QString&amp; str)
  {
      //qDebug() &lt;&lt; "the signal got to slot correctly";
      QVariant status = "Returned";
  
  //    emit statusReady(QVariant::fromValue(status));
      QMetaObject::invokeMethod(target, "setReturnStatus", Q_ARG(QVariant, status));
  }
  </code></pre>
  
  <p><strong>QML</strong></p>
  
  <p><strong>main.qml</strong> not really relevant, just to show that the app has stackview and broken into components</p>
  
  <pre><code>import QtQuick 2.7
  import QtQuick.Controls 2.0
  import QtQuick.Layouts 1.3
  
  ApplicationWindow {
      visible: true
      width: 640
      height: 480
      title: qsTr("Borrowment terminal")
  
      signal submitReturnForm(string itemID)
  
      property int lineEditLength: 120
      background: Rectangle
      {
          id: mainBackground
          color: "gray"
      }
  
      StackView
      {
          id: stack
          initialItem: returnPageComponent
      }
  
      Component
      {
          id: returnPageComponent
          ReturnPage{}
      }
  }
  </code></pre>
  
  <p><strong>ReturnPage.qml:</strong></p>
  
  <pre><code>import QtQuick 2.0
  import QtQuick.Layouts 1.3
  import QtQuick.Controls 2.2
  
  Item {
      id: returnPageView
  
      ColumnLayout {
          id: returnPageMenu
          x: 213
          y: 99
          width: 200
          height: 282
          Layout.margins: 20
  
          //redundant, just to make it compile
          Rectangle {
              width: lineEditLength
              height: 40
              color: "white"
              Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
              TextInput {
                  id: itemIDEdit
                  width: parent.width
                  height: parent.height
                  font.pixelSize: 20
                  maximumLength: 8
              }
  
  
          }
  
          //this label I want to set
          Label
          {
              id: returnStatusLabel
              objectName: "returnStatusLabel"
              function setReturnStatus(status) {
                  console.log("set return status to " + status)
                  ReturnPage.returnStatus = status
              }
              text: "empty"
              width: 100
              height: 20
              Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
              font.pointSize: 14
  
          }
  
          Button {
              id: returnItemButton
              width: 75
              height: 30
              text: "Return the Item"
              Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
              onClicked:  submitReturnForm(itemIDEdit.text)
          }    
      }
  }
  </code></pre>
  
  <hr>
  
  <h2>Screenshot</h2>
  
  <p><a href="https://i.stack.imgur.com/Qkecw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qkecw.png" alt="enter image description here"></a></p>
  
  <h2>Question</h2>
  
  <p>How can I invoke/set the <code>returnStatusLabel</code> to some text sent from C++ end, given that it is in a different component? </p>
  
  <h2>What I tried</h2>
  
  <ol>
  <li><p>Make the function in <code>main.qml</code> and invoke it from C++ with root object as target. </p>
  
  <p>This did invoke the function, but the text was not set. I suspect that this is something about not using the right object, so something about lifetime.</p></li>
  <li><p>Make the function in the label itself, find the object in C++ and invoke the function with the object as argument</p>
  
  <p>In this case the function did not get invoked at all, although debugger did not complain about connection not being set correctly.</p></li>
  </ol>