Qt QGraphicsSceneMouseEvent成员访问不完整类型'QMouseEvent'错误

Qt QGraphicsSceneMouseEvent member access to incomplete type 'QMouseEvent' Error

我收到一个错误,我不知道如何解决。

我创建了一个继承 QGraphicsScene 的 class CustomScene,我想覆盖此 class 中的鼠标功能。

我试图通过拖放在场景中创建一个矩形,甚至当我尝试使用 event->pos().x() 获取鼠标的位置时,我得到 QGraphicsSceneMouseEvent 成员访问不完整类型 QGraphicsSceneMouseEvent

#ifndef CUSTOMSCENE_H
#define CUSTOMSCENE_H

#include <QGraphicsScene>
#include <customrectitem.h>


class CustomScene : public QGraphicsScene
{
    Q_OBJECT
public:
    explicit CustomScene(QObject *parent = nullptr);

    QGraphicsScene* scene = new QGraphicsScene;
 protected:
     void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
     void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;


};

#endif // CUSTOMSCENE_H
#include "customscene.h"
#include <QDebug>

CustomScene::CustomScene(QObject *parent)
   : QGraphicsScene{parent}
{

}

void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

qDebug() << "the scene know that the mouse is pressed";

 QGraphicsRectItem* rect = new QGraphicsRectItem(event->pos().x(),event->pos.y(),100,100); 
 //the line of the error
 this->addItem(rect);

}

void CustomScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "the scene know that the mouse is moving";
}

void CustomScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "the scene know that the mouse is released";
}

当您尝试使用仅使用 forward declaration.

声明的类型(即调用方法)时,通常会发生“对不完整类型的成员访问”

在这种情况下 QGraphicsSceneMouseEventqgraphicsscene.h. The actual declaration is in qgraphicssceneevent.h 中向前声明。要使用它,只需将

#include <QGraphicsSceneMouseEvent>

在您的来源中。请注意,the documentation.

的第一段中也明确指出了这一点