[SOLVED] Qt QGraphicsSceneMouseEvent member access to incomplete type 'QMouseEvent' Error


I am getting an error I don’t know how to resolve.

I created a class CustomScene that inherits QGraphicsScene and I want to override the mouse functions in this class.

I am trying to create a rectangle on the scene by dragging and dropping and when even I try to get the position of the mouse using event->pos().x() I get QGraphicsSceneMouseEvent member access to incomplete type QGraphicsSceneMouseEvent


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

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

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


#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


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";


A "member access to incomplete type" usually happens when you are trying to work with a type (i.e. call a method) that has only been declared using forward declaration.

In this case QGraphicsSceneMouseEvent is forward declared in qgraphicsscene.h. The actual declaration is in qgraphicssceneevent.h. To use that just put

#include <QGraphicsSceneMouseEvent>

in your source. Note that this is also explicitly stated in the first paragraph of the documentation.

Answered By – SebDieBln

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *