title: How to expose property of QObject pointer to QML
body: <p>I am giving very brief (and partial) description of my class to show my problem. Basically I have setup two properties.</p>
  
  <pre><code>class Fruit : public QObject
  {
  Q_OBJECT
    ....
  public:
      Q_PROPERTY( int price READ getPrice NOTIFY priceChanged)
  
      Q_PROPERTY(Fruit * fruit READ fruit WRITE setFruit NOTIFY fruitChanged)
  }
  </code></pre>
  
  <p>In my QML if I access the <code>price</code> property it works well and good. But if I access the <code>fruit</code> property which obviously returns <code>Fruit</code> and then try to use its <code>price</code> property, that doesn't work. Is this not supposed to work this way?</p>
  
  <pre><code>Text {
      id: myText
      anchors.centerIn: parent
      text: basket.price // shows correctly
      //text: basket.fruit.price // doesn't show
  }
  </code></pre>
  
  <p>The 2nd one returns <code>Fruit</code> which also is a property and it has <code>price</code> property but it doesn't seem to access that property? Should this work?</p>
  
  <p><strong>Updated</strong> </p>
  
  <p>I am including my source code. I created a new demo with <code>HardwareComponent</code>, it just makes more sense this way. I tried to make it work based on answer I received but no luck.</p>
  
  <p><code>HardwareComponent</code> class is the base class for <code>Computer</code> and <code>CPU</code>.</p>
  
  <h3>main.cpp</h3>
  
  <pre><code>#include &lt;QGuiApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QQmlContext&gt;
  
  class HardwareComponent : public QObject
  {
     Q_OBJECT
  public:
     HardwareComponent() : m_price(0) {}
     virtual int price() = 0;
     virtual Q_INVOKABLE void add(HardwareComponent * item) { m_CPU = item; }
     HardwareComponent * getCPU() const { return m_CPU; }
  
     Q_SLOT virtual void setPrice(int arg)
     {
        if (m_price == arg) return;
        m_price = arg;
        emit priceChanged(arg);
     }
     Q_SIGNAL void priceChanged(int arg);
  
  protected:
     Q_PROPERTY(HardwareComponent * cpu READ getCPU);
     Q_PROPERTY(int price READ price WRITE setPrice NOTIFY priceChanged)
  
     HardwareComponent * m_CPU;
     int m_price;
  };
  
  class Computer : public HardwareComponent
  {
     Q_OBJECT
  public:
     Computer() { m_price = 500; }
  
     int price() { return m_price; }
     void setprice(int arg) { m_price = arg; }
  };
  
  class CPU : public HardwareComponent
  {
     Q_OBJECT
  public:
     CPU() { m_price = 100; }
  
     int price() { return m_price; }
     void setprice(int arg) { m_price = arg; }
  };
  
  int main(int argc, char *argv[])
  {
     HardwareComponent * computer = new Computer;
     CPU * cpu = new CPU;
     computer-&gt;add( cpu );
  
     QGuiApplication app(argc, argv);
     QQmlApplicationEngine engine;
  
     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
     engine.rootContext()-&gt;setContextProperty("computer", computer); 
  
     return app.exec();
  }
  
  #include "main.moc"
  </code></pre>
  
  <h3>main.qml</h3>
  
  <pre><code>import QtQuick 2.3
  import QtQuick.Window 2.2
  
  Window {
      visible: true
      width: 360
      height: 360
  
      Text {
          anchors.centerIn: parent
          text: computer.price // works
          //text: computer.cpu.price // doesn't work, doesn't show any value
      }
  }
  </code></pre>
  
  <p>This is the complete source code for all my project files.</p>
  
  <p>When I run it I get this output:</p>
  
  <blockquote>
    <p>Starting C:\Users\User\Documents\My Qt
    Projects\build-hardware-Desktop_Qt_5_4_0_MSVC2010_OpenGL_32bit-Debug\debug\hardware.exe...
    QML debugging is enabled. Only use this in a safe environment.
    qrc:/MainForm.ui.qml:20: ReferenceError: computer is not defined</p>
  </blockquote>
  
  <p>Even though it gives a warning at line 20 (computer.price) line, it still works and shows the price of computer (=500). If change it <code>computer.cpu.price</code>, the same warning is reported but the price no longer shows - it doesn't seem to work.</p>
  
  <p>The problem is since price is a virtual property, it works! but if I use this property on another hardware component inside the computer component, it doesn't work! The code/answer posted by Mido gives me hope there could be a solution to this, it looks quite close! I hope I can make this work.</p>