Qt-为什么我不能将自定义QGraphicsItem强制转换为QObject?

时间:2019-01-27 23:00:42

标签: c++ qt

我一直在努力Qt插槽和信号。此连接生成此错误:

  

moc_nodeitem.cpp:68:错误:从类型'QObject *'到类型'NodeItem *'的static_cast无效            NodeItem * _t = static_cast(_o);                                                     ^

void GraphScene::addArcDestination(QGraphicsSceneMouseEvent *event){
    NodeItem *item = dynamic_cast<NodeItem *>(itemAt(event->scenePos(), QTransform()));
    if (item){
        currentArc->setDestination(item);
        connect(item, &NodeItem::destroyed, currentArc, &ArcItem::deleteThis);
        connect(currentArc->getSource(), &NodeItem::destroyed, currentArc, &ArcItem::deleteThis);
        update();
    }else {
        removeItem(currentArc);
        update();
    }
}

因此,我在文件中添加了一个静态强制转换connect(static_cast<QObject*>(item), &NodeItem::destroyed, currentArc, &ArcItem::deleteThis);,它基本上说我的自定义类不继承自QObject。但是,这是NodeItem实现,它是通过QGraphicsItem间接继承自QObject的。

这是NodeItem实现:

class NodeItem : public QGraphicsItem
{
    Q_OBJECT
public:
    NodeItem(QPointF);

    void paint(QPainter *painter, const QStyleOptionGraphicsItem* option , QWidget *widget) override;
    QRectF boundingRect() const override;
    QPainterPath shape() const override;

private:
    std::unique_ptr<Node> node;

signals:
    void destroyed();
};

如果您需要更多信息或代码,请告诉我。

0 个答案:

没有答案