PDA

View Full Version : Hiding children of QGraphicsItem



nilot
30th May 2017, 09:09
Hello,

I have a QGraphicsItem, called myitem, which has several children. When myitem is selected I want to display its children and when it is not selected, I hide them. While the children weren't selectable, it worked perfectly with something like that :



QVariant QGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if(change == QGraphicsItem::ItemSelectedChange)
{
if(value.toBool())
{
//item is selected
showChildren(true);
}else
{
//item is deselected
showChildren(false);

}
}
...
}

But now the children of myitem are selectable and the above code doesn't work anymore because if I deselect myitem to select one of its children I hide it. In this situation, I shouldn't hide the children of myitem.
So I have to know the new selected item in QGraphicItem::itemChange to correctly show/hide the children. How can I do that ?
In change == QGraphicsItem::ItemSelectedChange or change == QGraphicsItem::ItemSelectedhasChanged, scene()->selectedItems() doesn't return the newly selected items.


Thank you

Santosh Reddy
30th May 2017, 11:55
connect to
[signal] void QGraphicsScene::focusItemChanged(QGraphicsItem *newFocusItem, QGraphicsItem *oldFocusItem, Qt::FocusReason reason)

nilot
30th May 2017, 12:48
Are you sure that focus and selection are the same thing ? Only one item can have focus but more than one qgraphicsitem can be selected at the same time.

Suppose that two items are selected then unselected, do your signal will be trigger two times ?

Santosh Reddy
31st May 2017, 07:19
Focus and selection are different, this signal will be emitted only once, even if multiple items are selected. You will need to check the Item selection when focus changes.

wysota
1st June 2017, 22:49
It seems easiest to just check in itemChange() whether any of the children are selected. If so, don't hide them.