I did make it const, but I doubt this problem has anything to do with shape().
In my case I discovered that I had an error in the boundingRect() definition. My bounding box had an offset to the item, so that the item would not report mouse events when the mouse was moving over it simply because the bounding box was somewhere else. Now that I fixed it, the mouse events do get delivered to the graphics item correctly. I can even remove the shape implementation (which does nothing else than the standard implementation anyways) and it still works.
The one thing that remains to solve is that all three, the graphics item, the graphics scene, and the graphics view, all seem to receive the mouse events simultaneously. I was expecting in this situation that the graphics item consumes the event and it doesn't get propagated all the way up to the view. Is my expectation wrong, or am I not doing something right? Here is the code for the relevant places:
{
Q_OBJECT
GraphicsViewWidget
(QWidget *parent
= 0);
~GraphicsViewWidget();
protected:
};
void GraphicsViewWidget
::mousePressEvent(QMouseEvent *event
) {
qDebug() << "view press event";
}
void GraphicsViewWidget
::mouseMoveEvent(QMouseEvent *event
) {
qDebug() << "view mouse move";
}
void GraphicsViewWidget
::mouseReleaseEvent(QMouseEvent *event
) {
qDebug() << "view mouse release";
}
{
Q_OBJECT
ComState comState;
public:
~MyScene (){};
};
{
setSceneRect(-100, -100, 200, 200);
addItem(&comState);
}
{
qDebug() << "scene mouse press";
}
{
qDebug() << "scene mouse move" << mouseGrabberItem();
}
{
qDebug() << "scene mouse release";
}
{
public:
double vx;
double vy;
public:
~ComState(){};
};
{
vx = 0;
vy = 0;
}
QRectF ComState
::boundingRect() const {
return QRectF(-0.1,
-0.1,
0.2,
0.2);
}
{
qDebug() << "item mouse press";
}
{
qDebug() << "item mouse move";
}
{
qDebug() << "item mouse release";
}
class GraphicsViewWidget : public QGraphicsView
{
Q_OBJECT
GraphicsViewWidget(QWidget *parent = 0);
~GraphicsViewWidget();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
};
void GraphicsViewWidget::mousePressEvent(QMouseEvent *event)
{
qDebug() << "view press event";
QGraphicsView::mousePressEvent(event);
}
void GraphicsViewWidget::mouseMoveEvent(QMouseEvent *event)
{
qDebug() << "view mouse move";
QGraphicsView::mouseMoveEvent(event);
}
void GraphicsViewWidget::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << "view mouse release";
QGraphicsView::mouseReleaseEvent(event);
}
class MyScene : public QGraphicsScene
{
Q_OBJECT
ComState comState;
public:
MyScene (QWidget *parent = 0);
~MyScene (){};
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
};
MyScene::MyScene (QWidget *parent)
: QGraphicsScene(parent)
{
setSceneRect(-100, -100, 200, 200);
addItem(&comState);
comState.setFlags(QGraphicsItem::ItemIsMovable);
}
void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "scene mouse press";
QGraphicsScene::mousePressEvent(event);
}
void MyScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "scene mouse move" << mouseGrabberItem();
QGraphicsScene::mouseMoveEvent(event);
}
void MyScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "scene mouse release";
QGraphicsScene::mouseReleaseEvent(event);
}
class ComState : public QGraphicsItem
{
public:
double vx;
double vy;
public:
ComState(QGraphicsItem *parent = 0);
~ComState(){};
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
};
ComState::ComState(QGraphicsItem *parent)
: QGraphicsItem(parent)
{
vx = 0;
vy = 0;
}
QRectF ComState::boundingRect() const
{
return QRectF(-0.1, -0.1, 0.2, 0.2);
}
void ComState::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "item mouse press";
}
void ComState::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "item mouse move";
}
void ComState::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "item mouse release";
}
To copy to clipboard, switch view to plain text mode
Bookmarks