title: C++ Subclassing, Inheritance and Casting
body: <p>This specific conundrum is hard for me to explain so I'll try as best I can.</p>
  
  <p>In the Qt framework, there are 2 classes: QTreeWidget and QTreeWidgetitem.</p>
  
  <p>I have subclassed a QTreeWidgetitem:</p>
  
  <pre><code>
  class MyQTreeWidgetitem : public QObject, public QTreeWidgetitem
  {
  
  ...
  
  public:
    int myCustomVariable;
  }
  </code></pre>
  
  <p>I add this to a QTreeWidget doing something like:</p>
  
  <pre><code>
  MyQTreeWidgetitem *item = new MyQTreeWidgetitem;
  item->myCustomVariable = 10;
  
  tree->addTopLevelItem(item);
  </code></pre>
  
  <p>Now, if ever I need to call tree->topLevelItem(x), all I get back is the QTreeWidgetitem*.</p>
  
  <p>However, what I'm after is the MyQTreeWidgetitem* so I:</p>
  
  <pre><code>
  MyQTreeWidgetItem *theitem = (MyQTreeWidgetItem*)tree->topLevelItem(n);
  </code></pre>
  
  <p>The problem with this is that theitem->myCustomVariable no longer equals 10.</p>
  
  <p>Is there a way to keep MyQTreeWidgetitem intact?</p>