Reputation: 435
I have a matrix of QGraphicsItem's in a QGraphicsScene and when I click an element it changes it's color. This is implemented with mousePressEvent()
. I want to be able to click and hold and then move the cursor over other QGraphicsItem's and change their color too, hence trigger their mousePressEvent()
.
The problem is that due to mouse grabbing the first element I click "keeps" all the events and hoverEnterEvent()
is not triggered. I tried adding ungrabMouse()
to mousePressEvent()
but that didn't help.
I guess one solution would be to make the QGraphicsitem's drag-able and use QT drag and drop functions for that. In fact I have this:
void dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
changeColor();
}
and when I drag some text from the application over the elements it works like I want it but not when I "drag" a QGrahphicsItem.
So is the only solution to make a QGraphicsItem dragable to be able to trigger events while hovering over them with a clicked mouse button?
Upvotes: 2
Views: 1888
Reputation: 435
I found a solution myself. I added all my QGraphicsRectItem's to a QGraphicsItemGroup and implemented the events for this group. In Group::MouseMoveEvent() I check the position of the cursor and apply the event to its children. It looks like this:
void Group::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
{
if (boundingRect().adjusted(0,0,-1,-1).contains(event->pos()))
{
if (CellCoordinate(event->pos()) != lastChangedCell_) {
lastChangedCell_ = CellCoordinate(event->pos());
modifyCell(CellCoordinate(event->pos()));
}
}
}
If you have the same problem and need more info feel free to contact me.
Upvotes: 2
Reputation: 12195
I'd recommend using mouseMoveEvent()
.
Edit: The mouseMoveEvent()
of the parent QGraphicsView
.
Upvotes: 1