folibis
folibis

Reputation: 12874

Select both child item and its parent in QGraphicsFramework

I've implemented a scene in my program using QGraphicsFramework. So I have a shape (parent) and its anchor point (child) both QGraphicsItem.

Shape::Shape(QGraphicsItem *parent) :
    QGraphicsItem(parent)
{
    setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
    anchorPoint = new AnchorPoint(this);
    anchorPoint->setParentItem(this);
    anchorPoint->setVisible(false);
}

The anchor point item usually invisible. But when I select my shape I have to show its anchor point and accordingly to hide it when the shape loses selection. So I've bound this action to itemChange:

QVariant Shape::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    switch(change)
    {
        case QGraphicsItem::ItemSelectedChange:
            anchorPoint->setVisible(value.toBool());
        break;
    }
    return QGraphicsItem::itemChange(change, value);
}

But the problem appears when I want to move the anchor point. If I click it the shape loses focus and so hides its anchor point so I can't move it. Is there a way to select child item when its parent stays selected?

Upvotes: 0

Views: 605

Answers (1)

dtech
dtech

Reputation: 49319

You could define your own isSelected property (and selection manager if multiple selection is needed) on top of what is provided by default, which set to true when the Shape is selected, and to false if either another Shape gets selected or you click the empty scene. The anchor point, not being a Shape, will not cause the parent Shape to get deselected when it is selected.

This way you can have selection in two contexts - what the scene considers selected, and what you consider selected. I've done this in QML and it is quite handy, however, I refer to the custom selection as focus, using handles of an object changes the selection but not the focus, which is useful for keyboard events with an extra dispatcher as well, because I assume you'd also want the Shape to be receiving keyboard events while the anchor point is being selected, rather than being received by the anchor.

Upvotes: 1

Related Questions