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.

2 Answers 2

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);
}
3
  • Thx Works better than what I found : add && this.isSelected() in the if. Commented Apr 23, 2012 at 21:14
  • @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 ?
    – GUI-Novice
    Commented Mar 22, 2018 at 12:11
  • @GUI-Novice I would try calling scene()->clearSelection() in the other item's mousePressEvent to ensure that this item gets deselected on a mouse press.
    – Anthony
    Commented Mar 23, 2018 at 3:28
0

QGraphicsScene::selectionChanged

1
  • You'd have to iterate over all selected items and manually set the ones in question selected.
    – ssc
    Commented Oct 14, 2014 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.