Problem getting mouse events from overrider QGraphicsPixmapItem
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:
Code:
#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);
}
And here is where I create a new ImagePixmapItem:
Code:
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?
Re: Problem getting mouse events from overrider QGraphicsPixmapItem
you should call accept() on the event in the event handler.
Re: Problem getting mouse events from overrider QGraphicsPixmapItem
Did you mean in the ImagePixmapItem? I dont think those functions ever get called - the qDebug statements never print
Re: Problem getting mouse events from overrider QGraphicsPixmapItem
From the docs:
Quote:
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.
Re: Problem getting mouse events from overrider QGraphicsPixmapItem
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
Re: Problem getting mouse events from overrider QGraphicsPixmapItem
Quote:
Originally Posted by
dcole
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)
Re: Problem getting mouse events from overrider QGraphicsPixmapItem
hmm.. what does mouseGrabberItem () return?
Re: Problem getting mouse events from overrider QGraphicsPixmapItem
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!