Home > front end >  Qt QGraphicsSceneMouseEvent member access to incomplete type 'QMouseEvent' Error
Qt QGraphicsSceneMouseEvent member access to incomplete type 'QMouseEvent' Error

Time:12-26

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

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

CodePudding user response:

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 line of the documentation.

  • Related