What method is called when a QGraphicsItem get selected
Asked Answered
H

2

11

Probably a trick question, but I can't find the answer.

I need to know when a QGraphicsItem gets selected. There must be a method that's called.

I know QGraphicsItem::itemChange() but it's called too often.

Is there a better method ?

thx

edit : With this

if(change == ItemSelectedChange && scene()){
    cout << "haha " << i++ << endl;
}

I get two calls every selection change.

Harbert answered 23/4, 2012 at 19:44 Comment(0)
C
18

You should take value into consideration in the QGraphicsItem::itemChange method. What you want is probably something like this:

QVariant YourItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == QGraphicsItem::ItemSelectedChange)
    {
        if (value == true)
        {
            // do stuff if selected
        }
        else
        {
            // do stuff if not selected
        }
    }

    return QGraphicsItem::itemChange(change, value);
}
Cableway answered 23/4, 2012 at 20:54 Comment(3)
Thx Works better than what I found : add && this.isSelected() in the if.Harbert
@Anthony, I have a question on the else part. When I select another graphics item, the else part is getting triggered. I understand that. I am facing an issue that i need to get the else part triggered while on mouse press on another graphics item. Now it is triggering only when i press and release the mouse button on another graphics item. Is there any solution for this ?Cure
@Cure I would try calling scene()->clearSelection() in the other item's mousePressEvent to ensure that this item gets deselected on a mouse press.Cableway
M
0

QGraphicsScene::selectionChanged

Mccready answered 23/4, 2012 at 20:11 Comment(1)
You'd have to iterate over all selected items and manually set the ones in question selected.Etheleneethelin

© 2022 - 2024 — McMap. All rights reserved.