dcole
21st January 2011, 17:12
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>
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(QGraphicsSceneMou seEvent *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);
}
And here is where I create a new ImagePixmapItem:
QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));
ImagePixmapItem *item = static_cast<ImagePixmapItem *>(scene.addPixmap(p));
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
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?
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>
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(QGraphicsSceneMou seEvent *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);
}
And here is where I create a new ImagePixmapItem:
QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));
ImagePixmapItem *item = static_cast<ImagePixmapItem *>(scene.addPixmap(p));
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
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?