Hello,
I would like to use my own implementation of QGraphicsPixmapItem so that I can do things during the mouse events like zoom the image, etc.
here is my class:
#include <QPainter>
#include "imagepixmapitem.h"
#include <QGraphicsSceneWheelEvent>
{
setCacheMode(NoCache);
setAcceptHoverEvents(true);
}
ImagePixmapItem::~ImagePixmapItem()
{
}
{
painterPath.addRect(boundingRect());
return painterPath;
}
qDebug("hello");
}
qDebug("Print this line if catch a wheelEvent");//this is never printing
qreal factor = 1.2;
if (event->delta() < 0)
factor = 1.0 / factor;
scale(factor, factor);
}
#include <QPainter>
#include "imagepixmapitem.h"
#include <QGraphicsSceneWheelEvent>
ImagePixmapItem::ImagePixmapItem(const QPixmap &pixmap, QGraphicsItem *parentItem)
: QGraphicsPixmapItem(pixmap,parentItem)
{
setCacheMode(NoCache);
setAcceptHoverEvents(true);
setFlag(QGraphicsItem::ItemIsSelectable,true);
}
ImagePixmapItem::~ImagePixmapItem()
{
}
QPainterPath ImagePixmapItem::shape() const
{
QPainterPath painterPath;
painterPath.addRect(boundingRect());
return painterPath;
}
void ImagePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event){
qDebug("hello");
}
void ImagePixmapItem::wheelEvent ( QGraphicsSceneWheelEvent * event ){
qDebug("Print this line if catch a wheelEvent");//this is never printing
qreal factor = 1.2;
if (event->delta() < 0)
factor = 1.0 / factor;
scale(factor, factor);
}
To copy to clipboard, switch view to plain text mode
And here is where I create a new ImagePixmapItem:
ImagePixmapItem *item = static_cast<ImagePixmapItem *>(scene.addPixmap(p));
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));
ImagePixmapItem *item = static_cast<ImagePixmapItem *>(scene.addPixmap(p));
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
To copy to clipboard, switch view to plain text mode
where qi is my image.
This loads correctly intot he graphicsView, but it does not seem to give me any of the mouse debug outputs.
Any ideas what is wrong?
Bookmarks