简体   繁体   English

QT:qobject_cast出现问题

[英]QT: trouble with qobject_cast

I have derived QGraphicsItem and QGraphicsScene classes. 我派生了QGraphicsItem和QGraphicsScene类。 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. 我希望这些项能够调用scene()并获得一个derviedGraphicsItem *而不是QGraphicsItem *,所以我重新实现了QGraphicsScene :: itemAt以返回一个派生指针。

DerivedItem* DerivedScene::itemAt( const QPointF &position, const QTransform &dt ) const
{
    return qobject_cast< DerivedItem * >(
            QGraphicsScene::itemAt(position, dt) );
}

I get the following error (Qt 4.6, GCC 4.4.3 on Ubuntut 10.4) 我收到以下错误(Qt 4.6,UCCntut 10.4上的GCC 4.4.3)

scene.cpp: In member function ‘DerivedItem* DerivedScene::itemAt(qreal, qreal, const QTransform&) const’:
scene.cpp:28: error: no matching function for call to ‘qobject_cast(QGraphicsItem*)’

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. 然后我注意到QGraphicsItem没有继承QObject,所以我使我的派生QGraphicsItem类具有来自QObject和QGraphicsItem的多重继承,并且在添加Q_OBJECT宏并重建项目后,我得到了相同的错误。

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. 我知道尝试将父类强制转换为孩子应该是糟糕的设计,但在这种情况下它看起来像我想要的,因为我的派生项类具有新功能,其对象需要一种方法来调用该新功能围绕自己的项目,并使用itemAt()询问项目场景对象似乎是最好的方法 - 但我需要itemAt()来返回正确类型的指针。 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. 我可以通过使用dynamic_cast将派生项目转换为QGraphicsScene :: itemAt()返回的QGraphicsItem *来解决这个问题,但我真的不明白为什么这样做而不是qobject_cast,或者使用dynamic_cast与qobject_cast的好处或缺点。

EDIT: forgot to mention that I also reimplemented QGraphicsItem::scene() in my derived class to return a DerivedScene *, as 编辑:忘了提到我也在我的派生类中重新实现了QGraphicsItem :: scene()以返回DerivedScene *,如

DerivedScene* DerivedItem::scene() const
{
    return qobject_cast< DerivedScene * >( QGraphicsItem::scene() );
}

but this doesn't appear to be causing a compilation error... 但这似乎没有导致编译错误......

There is no point in inheriting from QObject just for casting. 继承QObject只是为了进行转换是没有意义的。 The advantage of qobject_cast over dynamic cast is summed up pretty much in the qobject_cast documentation : qobject_cast文档中 ,qobject_cast优于动态强制转换的优势总结如下

The qobject_cast() function behaves similarly to the standard C++ dynamic_cast(), with the advantages that it doesn't require RTTI support and it works across dynamic library boundaries. qobject_cast()函数的行为类似于标准C ++ dynamic_cast(),其优点是不需要RTTI支持,并且它可以跨动态库边界工作。

It's nice to have and useful if you have QObjects, but not worth to inherit from QObject if it is all you want from QObject. 如果你有QObjects,那么拥有和有用是很好的,但如果你想从QObject中获取它,则不值得从QObject继承。

Also, for QGraphicsIems there is qgraphicsitem_cast , which should do exactly what you want :) 另外,对于QGraphicsIems,有qgraphicsitem_cast ,它应该完全符合你的要求:)

You have to pass a QObject pointer to qobject_cast() and QGraphicsScene::itemAt returns a QGraphicsItem pointer. 您必须将QObject指针传递给qobject_cast(),QGraphicsScene :: itemAt返回一个QGraphicsItem指针。 Since, as you mentioned, QGraphicsItem does not derive from QObject, the complier gave you that error. 因为,如你所提到的,QGraphicsItem不是从QObject派生的,编译器给你的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM