Hi,
I would like to reimplement mouseEvent methods in my customized QGraphicsItem (for example in Ellipse, inherited from QGraphicsEllipseItem).
Firstly, in my View, i reimplemente the mousePressEvent method :
|
Source code
|
1
2
3
4
5
6
7
8
9
|
void View::mousePressEvent(QMouseEvent *mouseEvent)
{
qDebug() << "vue mouse press event";
this->getScene()->addItem(this->getScene()->getBuilder->createItem(mouseEvent->pos()));
this->getScene()->update();
QGraphicsView::mousePressEvent(mouseEvent);
}
|
The QGraphicsView mousePressEvent enables me to send the event to my scene. In my scene, i have :
|
Source code
|
1
2
3
4
5
6
|
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
qDebug() << "scene mouse press event";
QGraphicsScene::mousePressEvent(mouseEvent);
}
|
This is works and i have the qDebug() which are displayed (qDebug() of the view before the qDebug() of the scene.. it seems normal).
But, whith my ellipse item, the mousePressEvent is not called. Here is my ellipse item :
|
Source code
|
1
2
3
4
5
6
7
8
|
TEllipse::TEllipse(QGraphicsItem *parent, QGraphicsScene *scene, QPoint center, int radius) : QGraphicsEllipseItem(parent, scene)
{
this->center = center;
this->radius = radius;
this->setFlag(QGraphicsItem::ItemIsSelectable);
this->setAcceptHoverEvents(true);
this->setAcceptedMouseButtons(Qt::LeftButton);
}
|
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
void TEllipse::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
const QPoint c = center;
painter->drawEllipse(c, this->radius, this->radius);
}
QRectF TEllipse::boundingRect()
{
QPointF topLeft(this->center.x()-this->radius, this->center.y()-this->radius);
QPointF bottomRight(this->center.x()+this->radius, this->center.y()+this->radius);
return QRectF(topLeft, bottomRight);
}
|
|
Source code
|
1
2
3
4
5
6
7
8
9
10
|
void TEllipse::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
qDebug() << "mouseEvent is accepted ? " << mouseEvent->isAccepted();
mouseEvent->accept();
QGraphicsEllipseItem::mousePressEvent(mouseEvent);
qDebug() << "ELLIPSE MOUSE EVENT";
}
|
But when i click in my view, after the qDebug() of my view and my scene, nothing is displayed :s I don't understand why.
Also, I tried to reimplement shape in my item :
|
Source code
|
1
2
3
4
5
6
7
|
QPainterPath TEllipse::shape() const
{
QPainterPath path;
path.addRect(this->rect());
return path;
}
|
But the same result.
Any ideas ?
Thank you a lot :-)
EDIT : Apparently, when i create an Ellipse item in my scene constructor, the qDebug() "sceneEvent" for the sceneEvent method reimplemented in Ellipse class is displayed.
So i can perahps get from it the mousePressEvent...
Nevertheless, when i tried to click on it after the creation, the sceneEvent doesn't appeared anymore :s
But when i only click in the view and the mousePressEvent of my view is called (creation through my builder class of the ellipse item which is added to the scene), the sceneEvent is not called.
So, i have two questions :
- Why i need to use the sceneEvent to get the mousePressEvent ? Why the mousePressEvent of my ellipse item is not automatically called by my scene ?
- Why when i only click in my view, the sceneEvent doesn't appeared ?
Thank you :-)