在从QGraphicsView继承的类中使用QGraphicsSceneMouseEvent

时间:2018-07-03 10:05:02

标签: c++ qt qt5 qgraphicsview qgraphicsscene

我有一个从QGraphicsView继承的类。我使用QGraphicsScene在窗口中显示图像。

此处正常运行。 但是,我想使用QGraphicsSceneMouseEvent鼠标事件在此图像上绘制。问题是,如果我使用QGraphicsSceneMouseEvent,我将不适合mousePressEvent和mouseMoveEvent方法。

我尝试使用QMouseEvent *事件,但是例如我无权访问lastScenePos()。

这是我的显示图片的代码

DisplayImage.h:

int f(int);

DisplayImage.cpp:

/* function definition */
int functionName(int arg)
{
    /* do things */
    return arg * 2;
}

/* create the type "function pointer int->int" */
typedef int (*func_ptr_t)(int);

/* create the array */
func_ptr_t devFunctions[3];

int main(void)
{   
    /* associate the good function */
    devFunctions[0] = &functionName;

    /* call it with an arg */
    return devFunctions[0](42);
}

在这里,我想将mousePressEvent和mouseMoveEvent方法与QGraphicsSceneMouseEvent一起使用。 有人可以解决该问题吗?

1 个答案:

答案 0 :(得分:2)

覆盖方法时,不能更改参数指示的数据类型。解决方案是对场景使用事件过滤器:

*。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);
}

可以从以下link

下载完整的示例