首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在继承自QGraphicsView的类中使用QGraphicsSceneMouseEvent

在继承自QGraphicsView的类中使用QGraphicsSceneMouseEvent
EN

Stack Overflow用户
提问于 2018-07-03 18:05:02
回答 1查看 1.1K关注 0票数 3

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

在这里它可以正常工作。但是,我想使用QGraphicsSceneMouseEvent鼠标事件在此图像上绘制。问题是,如果我使用QGraphicsSceneMouseEvent,我就不适合使用mousePressEvent和mouseMoveEvent方法。

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

下面是显示图像的代码

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

在这里,我想对QGraphicsSceneMouseEvent使用mousePressEvent和mouseMoveEvent方法。有没有人有解决这个问题的办法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-03 18:23:56

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

*.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下载

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51151806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档