Qt连接转换失败

Kra*_*pow 1 c++ qt

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

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

但是我收到以下错误:

D:\Project\Scene\PointField.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.
Run Code Online (Sandbox Code Playgroud)

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

Tob*_*ght 5

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

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

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

  • @Krapow“另一个例子是QThread。” 抱歉,请插话,但是如果您感到困惑,请仔细检查各种[我的相关答案](http://stackoverflow.com/search?q=user%3A1329652+QThread),我在这里力求正确。如果您仍然感到困惑,请走开。也许有一天,我可能会想写一个规范的“关于线程和Qt的所有知识”。 (2认同)