I'm playing around with connections between C++ and QML.

I've found a case in which it seems like a signal connection doesn't work at first, then works on calls from other locations in the code.

A bit of code to clarify:
Qt Code:
  1. // Header
  2. #include <QObject>
  3. #include <QDateTime>
  4.  
  5. class MyQmlInterface : public QObject
  6. {
  7. Q_OBJECT
  8.  
  9. public:
  10.  
  11. Q_PROPERTY(QString singleValue READ singleValue WRITE setSingleValue NOTIFY singleValueChanged)
  12.  
  13. const QString &singleValue() const;
  14. void setSingleValue(const QString &newSingleValue);
  15. void emitCppSignal(const QString &passedStr);
  16.  
  17. Q_INVOKABLE QDateTime getCurrentDateTime() const {
  18. return QDateTime::currentDateTime();
  19. }
  20.  
  21. signals:
  22. void singleValueChanged();
  23. // THIS is the signal I want to connect to
  24. void signalFromCpp(const QString &passedStr);
  25.  
  26. public slots:
  27. void somethingHappened();
  28.  
  29. private:
  30. QString mSingleValue;
  31. };
  32.  
  33. // Cpp
  34.  
  35. const QString &MyQmlInterface::singleValue() const
  36. {
  37. return mSingleValue;
  38. }
  39.  
  40. void MyQmlInterface::setSingleValue(const QString &newSingleValue)
  41. {
  42. if (newSingleValue == mSingleValue)
  43. return;
  44. mSingleValue = newSingleValue;
  45. emit singleValueChanged();
  46. }
  47.  
  48. void MyQmlInterface::somethingHappened()
  49. {
  50. setSingleValue("O RLY");
  51. }
  52.  
  53. // Method to emit the signal
  54. void MyQmlInterface::emitCppSignal(const QString &passedStr)
  55. {
  56. emit signalFromCpp(passedStr);
  57. }
  58.  
  59. // QML
  60.  
  61. import QtQuick 1.0
  62.  
  63. Rectangle {
  64. width: 360
  65. height: 360
  66.  
  67. Text {
  68. id: myText
  69. anchors.centerIn: parent
  70. text: myInterface.singleValue
  71. }
  72.  
  73. Connections {
  74. target: myInterface
  75. onSignalFromCpp: {
  76. myText.text += passedStr
  77. myText.rotation += 90
  78. }
  79. }
  80. }
To copy to clipboard, switch view to plain text mode 

Now here is where I initialize my Qml display and expect to see the result of my signal being emitted:
Qt Code:
  1. // The types are what you expect :)
  2. mView = new QDeclarativeView(this);
  3. mEngine = mView->engine();
  4. mContext = mEngine->rootContext();
  5.  
  6. mQmlInterface = new MyQmlInterface;
  7.  
  8. QString filePath = "D:\\QmlTest\\QmlTest.qml";
  9. mView->setSource(filePath);
  10.  
  11. mContext->setContextProperty("myInterface", mQmlInterface);
  12. mQmlInterface->setSingleValue("Set after context...");
  13.  
  14. ui.verticalLayout->addWidget(mView);
  15. mView->show();
  16.  
  17. //! If needed - this is how to clean up in case Qml wants to quit
  18. //connect(mEngine, SIGNAL(quit()), this, SLOT(declarativeEngineQuit()));
  19.  
  20. // This first signal call doesn't work
  21. mQmlInterface->emitCppSignal("this getting to you?");
To copy to clipboard, switch view to plain text mode 

The above code runs when I click on a button to "start" the qml test. In a different place in my code (index change of a table widget) I call the same method.
Qt Code:
  1. mQmlInterface->emitCppSignal("this getting to you?");
To copy to clipboard, switch view to plain text mode 

Current behavior is that only the index change calls are getting through the QML. For my implementation I can live with that but why is it happening? Is this a bug?