2
votes

my code:

//forkliftitem.h
class ForkliftItem : public QGraphicsObject
{
    Q_OBJECT
    //some necessary code...
}

//forkliftitem.cpp
ForkliftItem::ForkliftItem()
{
    //some other code...
    connect(this, &ForkliftItem::faceChanged, this, &ForkliftItem::setRotation);
}

When I compile my code will generate the error as shown in title.Certainly,

cannot convert argument 3 from 'const QGraphicsItem *' to 'const QObject *'

because QGraphicsItem * is not inherited from QObject.But my this pointer's type is const ForkliftItem *, and ForkliftItem is inherited from QGraphicsObject.

And compile information have the following tips:

see reference to function template instantiation 'QMetaObject::Connection QObject::connect<void(__thiscall ForkliftItem::* )(int),void(__thiscall QGraphicsItem::* )(qreal)>(const ForkliftItem *,Func1,const QGraphicsItem ,Func2,Qt::ConnectionType)' being compiled with [ Func1=void (__thiscall ForkliftItem:: )(int)

can be seen : argument 3 of connect() is handled as const QGraphicsItem *, that why generate the compile error.

I could repair the error by following code:

connect(this, &ForkliftItem::faceChanged, this, 
    static_cast<void (ForkliftItem::*) (qreal)>(&ForkliftItem::setRotation));

But I feel very confused by that const ForkliftItem * became const QGraphicsItem * when call connect(this, fun1, this, fun2), and argument 1 is used correctly and argument 3 not.Anybody knows, please tell me, thanks.

2
Besides, how to escape "<>" to show it other than HTML symbols here.Crawl.W
There is this guy here who says he has solved adding include QGraphisObject .. take a look forum.qt.io/topic/12491/…Marco
Please update your question to include a minimal reproducible example. As it stands there is a lot of relevant information missing.G.M.
What's the signature of ForkliftItem::faceChanged?G.M.

2 Answers

1
votes

I could repair the error by following code:

connect(this, &ForkliftItem::faceChanged, this, 
    static_cast<void (ForkliftItem::*) (qreal)>(&ForkliftItem::setRotation));

That's because setRotation's signature is QGraphicsItem::setRotation, no matter what derived class you specify when taking the address, and connect enforces that the method you invoke is in a class derived from QObject.

The explicit cast is safe and a decent workaround that doesn't invoke undefined behavior. Another workaround would be to re-implement the method:

class ForkliftItem : public QGraphicsObject {
   Q_OBJECT
public:
   Q_SIGNAL void faceChanged(qreal);
   void setRotation(qreal angle) { QGraphicsObject::setRotation(angle); }
   ForkliftItem() {
      connect(this, &ForkliftItem::faceChanged, this, &ForkliftItem::setRotation);
   }
};
2
votes

Your problem is that setRotation is a member inherited from QGraphicItem, a class that doesn't inherit from QObject. QGraphicsObject class inherits from 2 classes: QObject and QGraphicItem.

Either connect the signal to a lambda or create your own slot and call that method in there.

connect(this, &ForkliftItem::faceChanged, [this](qreal angle) { this->setRotation(angle); });