Qt 连接转换失败

Qt Connection conversion fail

本文关键字:失败 转换 连接 Qt      更新时间:2023-10-16

我尝试连接一个QGraphicsObject子类和一个QGraphicsView子类(在QGraphicsObject构造函数中使用以下行)

connect(this, SIGNAL(paintImage(QPainter *, const QImage &)), this->scene()->views().front(), SLOT(paintImage(QPainter *, const QImage &)));

但是我收到以下错误:

D:ProjectScenePointField.cpp:18: error : C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const: cannot convert 'QGraphicsView *' to 'const QObject *'
The point types are unrelated; conversion needs reinterpret_cast, C style cast or function style cast.

我从文档中看到QGraphicsView继承自QObject,并且在两个子类声明中都有Q_OBJECT宏......有人有想法吗?

QGraphicsView

connect()调用中使用时是一个不完整的类型。 编译器不知道它继承自QObject,这就是为什么它报告

无法将"QGraphicsView *"转换为"const QObject *"

解决方案是在connect()调用之前的某个位置#include <QGraphicsView>实现文件中。

我通过在 QGraphicsObject 子类头的顶部包含我的 QGraphicsView sublass 标头来解决我的问题,然后清理/执行 qmake/重建。