The presented code does the following:
<1> Create object of QObject derived class and expose it to QML
<2> The class has a pointer to another QObject derived class and the pointer is accessible to QML via Q_PROPERTY.
<3> MouseArea in QML detects user-click and simply asks the c++ code for the pointer to be deleted.
<4> Color of rectangle turns black once this is detected.

The problem is that while certain approaches detect the deletion of the pointer by c++ other approaches don't.

Look at the inline comments:
Combination of (1) and (1b) works
Combination of (1) and (1d) does not work
(2) alone by itself works but (3) alone by itself does not.

When things work you should see the color of the Rectangle turn to black from yellow.

Can someone please explain this behaviour?

CPP Code:
Qt Code:
  1. class Name : public QObject
  2. {
  3. Q_OBJECT
  4. Q_PROPERTY(bool boolVal READ boolVal CONSTANT FINAL)
  5. public:
  6. bool boolVal() const {return true;}
  7. };
  8.  
  9. class Root : public QObject
  10. {
  11. Q_OBJECT
  12. Q_PROPERTY(QObject* name READ name CONSTANT FINAL)
  13. public:
  14. QObject* name() const {return m_pName;}
  15. Q_INVOKABLE void deleteName() {delete m_pName; m_pName = 0;}
  16. private:
  17. Name *m_pName {new Name};
  18. };
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22. QGuiApplication app(argc, argv);
  23.  
  24. Root objRoot;
  25. QtQuick2ApplicationViewer viewer0;
  26. viewer0.rootContext()->setContextProperty("objRoot", &objRoot);
  27. viewer0.setMainQmlFile(QStringLiteral("qml/myApp/main.qml"));
  28. viewer0.showExpanded();
  29.  
  30. return app.exec();
  31. }
To copy to clipboard, switch view to plain text mode 
QML Code:
Qt Code:
  1. import QtQuick 2.0
  2.  
  3. Rectangle {
  4. width: 360
  5. height: 360
  6.  
  7. color: "red"
  8.  
  9. Rectangle {
  10. id: objRect
  11. anchors {left: parent.left; top: parent.top}
  12. height: 70; width: 70;
  13.  
  14. property bool checked
  15. property QtObject temp: objRoot.name
  16.  
  17. color: checked ? "yellow" : "black" // (1)
  18.  
  19. //color: objRect.temp && objRect.temp.boolVal ? "yellow" : "black" //--->WORKS (2)
  20. //color: objRoot.name && objRoot.name.boolVal ? "yellow" : "black" //--->DOES NOT WORK !!! (3)
  21.  
  22. Binding on checked {
  23. //value: objRect.temp && objRect.temp.boolVal //--->DOES NOT WORK !!! (1a)
  24. //value: objRect.temp !== null && objRect.temp.boolVal //--->WORKS (1b)
  25. value: objRect.temp ? objRect.temp.boolVal : false //--->WORKS (1c)
  26.  
  27. //Using directly without collecting in local QtQobject temp:
  28. //----------------------------------------------------------
  29. //value: objRoot.name && objRoot.name.boolVal //--->DOES NOT WORK !!! (1d)
  30. //value: objRoot.name !== null && objRoot.name.boolVal //--->DOES NOT WORK !!! (1e)
  31. //value: objRoot.name ? objRoot.name.boolVal : false //--->DOES NOT WORK !!! (1f)
  32. }
  33.  
  34. Text {
  35. text: "Destroy"
  36. anchors.centerIn: parent
  37. }
  38.  
  39. MouseArea {
  40. anchors.fill: parent
  41. onClicked: objRoot.deleteName()
  42. }
  43. }
  44. }
To copy to clipboard, switch view to plain text mode 

{Qt/QML 5.2.0, Win 7, MinGW 4.8, QtQuick 2.0}