PDA

View Full Version : Problem getting mouse events from overrider QGraphicsPixmapItem



dcole
21st January 2011, 18: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?

high_flyer
21st January 2011, 18:42
you should call accept() on the event in the event handler.

dcole
21st January 2011, 19:12
Did you mean in the ImagePixmapItem? I dont think those functions ever get called - the qDebug statements never print

high_flyer
21st January 2011, 19:22
From the docs:

An item becomes a mouse grabber when it receives and accepts a mouse press event, and it stays the mouse grabber until either of the following events occur:

* If the item receives a mouse release event when there are no other buttons pressed, it loses the mouse grab.
* If the item becomes invisible (i.e., someone calls item->setVisible(false)), or if it becomes disabled (i.e., someone calls item->setEnabled(false)), it loses the mouse grab.
* If the item is removed from the scene, it loses the mouse grab.

If the item loses its mouse grab, the scene will ignore all mouse events until a new item grabs the mouse (i.e., until a new item receives a mouse press event)

although I guess the debug message should be printed any way...
Check if your item is a mouse grabber before you click it.

dcole
21st January 2011, 19:32
So I added item->grabMouse() right after I define the item. I am still not getting those debug statements. Do i need to do anything with setting the sceneRect or item rect or anything?

Incidentally, when I do try to scroll up and down with the wheel, my graphicsview scrolls up or down. Is it possibly intercepting my mouse calls and not passing them to the item? It is just a normal QGraphicsView, but I did turn on the ScrollHandDrag mode.

Nothing at all happens when I click anywhere on the image

totem
21st January 2011, 20:23
It is just a normal QGraphicsView, but I did turn on the ScrollHandDrag mode.

Did you check the interactive property of your view ? i.e. : QGraphicsView::setInteractive(bool)

high_flyer
21st January 2011, 20:24
hmm.. what does mouseGrabberItem () return?

dcole
21st January 2011, 20:53
Turns out it was a problem with the reference to item. I had to define an item instance first, and then use the scene.addItem() method to put my custom item on there. After I did this, the problem was solved.

Thanks for the help!