3
votes

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?

1

1 Answers

3
votes

When you overwrite a method, you can not change the type of data indicated by the arguments. The solution is to use an eventfilter for the scene:

*.h

#ifndef DISPLAYIMAGE_H
#define DISPLAYIMAGE_H

#include <QGraphicsView>

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

    bool eventFilter(QObject *watched, QEvent *event);
private:
    QGraphicsScene *scene;
    QPixmap pixmap;
    QGraphicsPixmapItem *pixmapItem;
};
#endif // DISPLAYIMAGE_H

*.cpp

#include "displayimage.h"

#include <QEvent>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>

#include <QDebug>

DisplayImage::DisplayImage(QWidget *parent):QGraphicsView(parent)
{
    scene = new QGraphicsScene(this);
    pixmapItem=new QGraphicsPixmapItem(pixmap);
    scene->addItem(pixmapItem);
    setScene(scene);
    scene->installEventFilter(this);
}

void DisplayImage::displayImg(const QImage &image){
    pixmap=QPixmap::fromImage(image);
    pixmapItem->setPixmap(pixmap);
    setSceneRect(image.rect());
    fitInView(pixmapItem, Qt::KeepAspectRatio);
    centerOn(pixmapItem);
}

bool DisplayImage::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == scene){
        // press event
        QGraphicsSceneMouseEvent *mouseSceneEvent;
        if(event->type() == QEvent::GraphicsSceneMousePress){
            mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
            qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
           // your logic here
        }
        // move event
        else if (event->type() == QEvent::GraphicsSceneMouseMove) {
            mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
            qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
            // your logic here
        }
        // release event
        else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
            mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
            qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos();
            // your logic here
        }
    }
    return QGraphicsView::eventFilter(watched, event);
}

The complete example can be downloaded from the following link