문제

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?

도움이 되었습니까?

해결책 2

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.

다른 팁

I'd recommend using mouseMoveEvent().

Edit: The mouseMoveEvent() of the parent QGraphicsView.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top