title: QT: trouble with qobject_cast
body: <p>I have derived QGraphicsItem and QGraphicsScene classes.  I want the items to be able to call scene() and get a derviedGraphicsItem * instead of a QGraphicsItem *, so I reimplemented QGraphicsScene::itemAt to return a derived pointer.</p>
  
  <pre><code>DerivedItem* DerivedScene::itemAt( const QPointF &amp;position, const QTransform &amp;dt ) const
  {
      return qobject_cast&lt; DerivedItem * &gt;(
              QGraphicsScene::itemAt(position, dt) );
  }
  </code></pre>
  
  <p>I get the following error (Qt 4.6, GCC 4.4.3 on Ubuntut 10.4)</p>
  
  <pre><code>scene.cpp: In member function ‘DerivedItem* DerivedScene::itemAt(qreal, qreal, const QTransform&amp;) const’:
  scene.cpp:28: error: no matching function for call to ‘qobject_cast(QGraphicsItem*)’
  </code></pre>
  
  <p>I then noticed QGraphicsItem doesn't inherit QObject, so I made my derived QGraphicsItem class have multiple inheritance from QObject and QGraphicsItem, and after adding the Q_OBJECT macro and rebuilding the project I get the same error.</p>
  
  <p>Am I going about this the wrong way?  I know it's supposed to be bad design to try to cast a parent class as a child, but in this case it seems like what I want, since my derived item class has new functionality and its objects need a way to call that new functionality on items around themselves, and asking the items scene object with itemAt() seems like the best way - but I need itemAt() to return a pointer of the right type.  I can get around this by having the derived items cast the QGraphicsItem * returned by QGraphicsScene::itemAt() using dynamic_cast, but I don't really understand why that works and not qobject_cast, or the benefits or disadvantages to using dynamic_cast vs. qobject_cast.</p>
  
  <p>EDIT:
  forgot to mention that I also reimplemented QGraphicsItem::scene() in my derived class to return a DerivedScene *, as</p>
  
  <pre><code>DerivedScene* DerivedItem::scene() const
  {
      return qobject_cast&lt; DerivedScene * &gt;( QGraphicsItem::scene() );
  }
  </code></pre>
  
  <p>but this doesn't appear to be causing a compilation error...</p>