mardi 3 juillet 2018

Use a QGraphicsSceneMouseEvent in a class that inherits from QGraphicsView

I have a class that inherits from QGraphicsView. I use a QGraphicsScene to display an image in a window.

Here it works properly. However, I would like to use the QGraphicsSceneMouseEvent mouse events to draw on this image. The problem is that if I use QGraphicsSceneMouseEvent I do not fit in the mousePressEvent and mouseMoveEvent methods.

I tried with QMouseEvent * event but I do not have access to lastScenePos () for example.

Here is my code that displays the image

DisplayImage.h :

class DisplayImage : public QGraphicsView{

Q_OBJECT

public:
DisplayImage(QWidget *parent=0);
void displayImg(const QImage &image);

private:
QGraphicsScene *scene;
QPixmap pixmap;
QGraphicsPixmapItem *pixmapItem;

protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
};

DisplayImage.cpp:

DisplayImage::DisplayImage(QWidget* parent) : QGraphicsView(parent){
scene = new QGraphicsScene(this);
pixmapItem=new QGraphicsPixmapItem(pixmap);
scene->addItem(pixmapItem);
this->setScene(scene);
}
void DisplayImage::displayImg(const QImage &image){
pixmap=QPixmap::fromImage(image);
pixmapItem->setPixmap(pixmap);
this->setSceneRect(0,0,image.width(),image.height());
this->fitInView(pixmapItem, Qt::KeepAspectRatio);
this->centerOn(pixmapItem);
}

here I would like to use the mousePressEvent and mouseMoveEvent methods with QGraphicsSceneMouseEvent. Would anyone have a solution to work around the problem?

Aucun commentaire:

Enregistrer un commentaire