我有一个继承自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);
};
显示图像.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);
}
在这里,我想将mousePressEvent和mouseMoveEvent方法与QGraphicsSceneMouseEvent一起使用。 有人有解决方法吗?
覆盖方法时,无法更改参数指示的数据类型。解决方案是对场景使用事件过滤器:
*.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);
}
完整的示例可以从以下链接下载