Class SignalItem implements the interface QGraphicsItem but does not list it in Q_INTERFACES. qobject_cast to QGraphicsItem will not work!
时间: 2024-05-27 10:12:02 浏览: 68
Qt QGraphicsItem测试
To resolve this issue, you need to add the Q_INTERFACES macro in the class declaration of SignalItem. The Q_INTERFACES macro tells Qt which interfaces are implemented by the class and allows qobject_cast to work properly.
Here's an example:
class SignalItem : public QObject, public QGraphicsItem
{
Q_OBJECT
Q_INTERFACES(QGraphicsItem)
public:
SignalItem(QObject *parent = nullptr);
// QGraphicsItem interface
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
};
In this example, SignalItem implements the QGraphicsItem interface and specifies it in the Q_INTERFACES macro. This will allow qobject_cast to work properly and cast SignalItem to QGraphicsItem.
阅读全文