title: Sending a signal to a QML item from C++ (Qt5)
body: <p>I have a QML file containing this:</p>
  
  <pre><code>Text {
      id: testData
      onTaskClicked:{
          testData.text = task.name
      }
  }
  </code></pre>
  
  <p>The catch is this taskClicked signal. It is emitted by another widget (C++) and needs to be relayed to QML.</p>
  
  <p>This is similar to <a href="https://stackoverflow.com/questions/8834147/c-signal-to-qml-slot-in-qt">this SO question</a>, except that the solution posted there doesn't work (why is written below).</p>
  
  <p>The C++ code:</p>
  
  <pre><code>ctxt-&gt;setContextProperty(QLatin1Literal("holiday"), m_model);
  ctxt-&gt;setContextProperty(QLatin1Literal("bgcolor"), color);
  
  view-&gt;setResizeMode(QQuickView::SizeRootObjectToView);
  
  auto mainPath = QStandardPaths::locate(QStandardPaths::DataLocation,
                                             QLatin1Literal("taskview.qml"));
  
  view-&gt;setSource(QUrl::fromLocalFile(mainPath));
  
  ctxt-&gt;setContextProperty(QLatin1Literal("viewer"), m_view);
  </code></pre>
  
  <p><code>m_view</code> is a <code>QListView</code> subclass that emits the <code>taskClicked(HolidayTask* task)</code> signal (from the .h file):</p>
  
  <pre><code>Q_SIGNALS:
      void taskClicked(HolidayTask* task);
  </code></pre>
  
  <p><code>color</code> and <code>m_model</code> are registered in QML and are used elsewhere. The object from the signal is already registered in QML. <code>view</code> is my <code>QQuickView</code>.</p>
  
  <p>First I tried the solution presented in the question above:</p>
  
  <pre><code>auto root = view-&gt;rootObject();
  auto myElement = root-&gt;findChild&lt;QObject*&gt;(QLatin1Literal("testData");
  
  connect(m_view, SIGNAL(taskClicked(HolidayTask* task), myElement,
          SLOT(taskClicked(HolidayTask* task);
  </code></pre>
  
  <p>However, <code>myElement</code> is always null (and I get a runtime warning about a non existent slot). </p>
  
  <p>If I try to set the view (the QListView) pointer as a context property of the QML view, it still doesn't work. </p>
  
  <p>In all cases, I get also:</p>
  
  <pre><code>QML Connections: Cannot assign to non-existent property "onTaskClicked"
  </code></pre>
  
  <p>What could I possibly be doing wrong here?</p>
  
  <p>EDIT to clarify some details: <code>HolidayTask</code> is a custom QObject subclass, and the signal <code>taskClicked</code> is defined in C++ (in a <code>QListView</code> subclass)</p>
  
  <p>EDIT2: We're getting close, but no cigar:</p>
  
  <pre><code>auto root = quickView-&gt;rootObject();
  auto myElement = root-&gt;findChild&lt;QObject*&gt;(QLatin1Literal("testData"));
  
  connect(m_view, SIGNAL(taskClicked(HolidayTask*)),
          myElement, SIGNAL(taskClicked(HolidayTask* task)));
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Text {
      id: testData
      objectName: "testData"
      signal taskClicked(HolidayTask task)
      onTaskClicked: {
          testData.text = task.name
          console.log("CLICk!")
      }
  }
  </code></pre>
  
  <p>yields</p>
  
  <pre><code>QObject::connect: No such signal QQuickText_QML_0::taskClicked(HolidayTask* task) in /home/lb/Coding/cpp/holiday-planner/src/mainwindow.cpp:178
  QObject::connect:  (receiver name: 'testData')
  </code></pre>
  
  <p>More details: HolidayTask, my custom QObject subclass, is registered in the code as</p>
  
  <pre><code>qmlRegisterType&lt;HolidayTask&gt;("HolidayPlanner", 1, 0, "HolidayTask");
  </code></pre>
  
  <p>Minimal QML with the data:</p>
  
  <pre><code> import QtQuick 2.0
   import QtQml 2.2
  
  import HolidayPlanner 1.0
  
  Rectangle {
      id: container
      objectName: "container"
      color: bgcolor
  
  
     Text {
          id: testData
          objectName: "testData"
          signal taskClicked(HolidayTask task)
          onTaskClicked: {
              testData.text = task.name
              console.log("CLICK")
          }
     }
  
  }
  </code></pre>
  
  <p>EDIT3: The final, working code is (see answers on why)</p>
  
  <pre><code> connect(m_view, SIGNAL(taskClicked(HolidayPlanner::HolidayTask*)),
          myElement, SIGNAL(taskClicked(HolidayPlanner::HolidayTask*)));
  </code></pre>
  
  <p>This worked <strong>only</strong> through the use of objects with full namespaces. Otherwise the signature will not match in QML.</p>