Class SignalItem implements the interface QGraphicsItem but does not list it in Q_INTERFACES. qobject_cast to QGraphicsItem will not work!
时间: 2024-05-27 20:12:02 浏览: 13
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.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)